Dominating patternstime limit:3000msmemory limit:131072kbthis problem would be judged onuvalive. Original id:4670
64-bit integer IO format: %lld Java class name: Main
The archaeologists is going to decipher a very mysterious ' language '. Now, they know many language patterns; Each pattern can is treated as a string on 中文版 letters (only lower case). As a sub string, these patterns may appear more than one times of a large text string (also only lower case 中文版 s).
What's matters most was that which patterns was the dominating patterns. Dominating pattern is the pattern whose appearing times isn't less than other patterns.
It is your job to find the dominating pattern (s) and their appearing times.
Input
The entire input contains multi cases. The first line of all case was an integer, which is the number of patterns N, 1N150. Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case was one line contains a large string as the text to lookup and whose length is up to 106.
At the end of the input file, number '0' indicates the end of the input file.
Output
For each of the input cases, output the appearing times of the dominating pattern (s). If there is more than one dominating pattern, output them in separate lines; and keep their input order to the output.
Sample Input
2 ABA Bab Ababababac 6 beta alpha haha Delta dede Tata Dedeltalphahahahototatalpha 0
Sample Output
4 ABA 2 alpha haha
SourceRegionals, Asia-hefei problem solving: trie diagram or AC automata
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = -;4 CharSTR[MAXN][MAXN];5map<string,int>MP;6 structtrie{7 intch[maxn*maxn][ -],fail[maxn*maxn],cnt[maxn*Maxn],tot,ret;8 BOOLflag[maxn*MAXN];9 voidinit () {Tenret = tot =0; One NewNode (); A } - intNewNode () { -memset (Ch[tot],0,sizeofCh[tot]); theFail[tot] = Cnt[tot] =0; -Flag[tot] =false; - returntot++; - } + voidInsertChar*STR,intRoot =0){ - for(inti =0; Str[i]; ++i) { + int&x = ch[root][str[i]-'a']; A if(!x) x =NewNode (); atRoot =x; - } -MP[STR] =Root; -Flag[root] =true; - } - voidBuildintRT =0){ inqueue<int>Q; - for(inti =0; I < -; ++i) to if(Ch[rt][i]) Q.push (Ch[rt][i]); + while(!Q.empty ()) { -RT =Q.front (); the Q.pop (); * for(inti =0; I < -; ++i) { $ int&x = Ch[rt][i],y =Ch[fail[rt]][i];Panax Notoginseng if(x) { -FAIL[X] =y; the Q.push (x); +}Elsex =y; A } the } + } - voidCountChar*STR,intRT =0){ $ for(inti =0; Str[i]; ++i) { $ intx = RT = ch[rt][str[i]-'a']; - while(x &&Flag[x]) { -ret = max (ret,++cnt[x]); thex =Fail[x]; - }Wuyi } the } - }ac; Wu Charss[1000100]; - intMain () { About intN; $ while(SCANF ("%d",&N), N) { - ac.init (); - mp.clear (); - for(inti =0; I < n; ++i) { Ascanf"%s", Str[i]); + Ac.insert (Str[i]); the } - ac.build (); $scanf"%s", SS); the Ac.count (ss); theprintf"%d\n", Ac.ret); the for(inti =0; I < n; ++i) the if(Ac.cnt[mp[str[i]] = =Ac.ret) puts (str[i]); - } in return 0; the}
View Code
Uvalive 4670 Dominating Patterns