[BZOJ1030]: [JSOI2007] text generator (AC automaton + DP) AC automaton explain + [HDU2222]: Keywords Search (AC automata)

Topic Portal


Subject description:

JSOI handed players ZYX a task, called the program a "text generator" Computer Software: Users of the software are some of Infant crowd,
they are now using GW text generator v6 version. The software can generate random articles --- always generate a fixed length and completely random text
chapter - that is, the article generated each byte is completely random. If an article contains at least one user who understand the word,
then we say that this article is readable (we call article a contains the word b , if and only if the word b is the article a substring). However, even by this
standard, users can now use the GW text generator v6 version of the article also generated almost completely unreadable?. ZYX should be noted GW text generator v6
number of all the text in readable text generated, to be able to successfully v7 update. Can you help him?


Input formats:

The first line of the input file contains two positive integers, respectively, the total number of words that the user understand N , GW text generator v6 generated text fixed
predetermined length M ; the N lines, each word comprising a user to understand. Here all of the text and word length does not exceed 100 , and only the packet may
contain capital letters A-> Z


Output formats:

An integer representing the total number of possible articles. Only you need to know the results of mold 10007 values.


Example:

Sample input:

2 2
A
B

Sample output:

100


 

Data range and tips:

For all the data, 1≤N≤60 , all words and text length does not exceed 100 , and may contain only capital letters.


 

answer:

I saw a string of multi-mode, should first think is AC automaton.

If you are not AC automatic machine, you can go to this blog, personal feeling is written in very clear: AC automaton explain + [HDU2222]: Search Keywords ( AC automata) .

Then we consider how to deal with.

Burst search time complexity of 26 is m , is clearly unacceptable.

Consider the question, minus the number of all articles of the articles can make up all the words of the number is the answer.

Consider using the DP , the definition of DP [I] [j] , represents the current point number is I , the length of the matched j number of programs, pay attention to the stored article can be composed of all number words.

dp[i][j]=∑(dp[father][j-1])

Finally, by subtracting the total number of articles Σ (dp [i] [m ]) to.


 

Code time:

#include <bits / STDC ++ H.> 
the using namespace STD; 
int n-, m; 
char S [101]; 
int Trie [6001] [30], CNT =. 1, ED [6001], NXT [6001]; 
int DP [ 6001] [101]; // dp [i] [j] indicates the current point number is i, the number does match the length of the program j. 
que int [6001]; 
int = ANS. 1; 
void INSERT (char * STR) Contribution // 
{ 
	int len = strlen (STR); 
	IF (len> m) return; 
	int = P. 1; 
	for (int I = 0; I <len; I ++) 
	{ 
		int CH = STR [I] - 'A'; 
		IF (Trie [P] [CH]) Trie [P] [CH] = ++ CNT;! 
		P = Trie [P] [CH ]; 
	} 
	ED [P] =. 1; 
} 
void Build () // find Fail pointer 
{ 
	for (int I = 0; I <26 is; I ++) Trie [0] [I] =. 1; 
	que [. 1] =. 1 ; 
	int. 1 = head, tail =. 1;  
	the while (head <= tail)
	{
		for(int i=0;i<26;i++)
		{
			if(!trie[que[head]][i])trie[que[head]][i]=trie[nxt[que[head]]][i];
			else
			{
				que[++tail]=trie[que[head]][i];
				nxt[trie[que[head]][i]]=trie[nxt[que[head]]][i];
				ed[trie[que[head]][i]]|=ed[trie[nxt[que[head]]][i]];
			}
		}
		head++;
	}
}
void ask()//dp全过程
{
	dp[1][0]=1;
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=cnt;j++)
		{
			if(ed[j])continue;
			for(int k=0;k<26;k++)
			{
				if(ed[trie[j][k]])continue;
				dp[trie[j][k]][i]=(dp[j][i-1]+dp[trie[j][k]][i])%10007;//转移
			}
		}
	}
	for(int i=1;i<=m;i++)ans=(ans*26)%10007;
	for(int i=1;i<=cnt;i++)ans=(ans+10007-dp[i][m])%10007;//答案要减去
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s);
		insert(s);
	}
	build();
	ask();
	cout<<ans;
	return 0;
}

 

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11090565.html