LA 4670 出现次数最多的子串 (AC自动机模板题)
Posted HARD_UNDERSTAND
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LA 4670 出现次数最多的子串 (AC自动机模板题)相关的知识,希望对你有一定的参考价值。
Time Limit:3000MS | Memory Limit:Unknown | 64bit IO Format:%lld & %llu |
Description
The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these patterns may appear more than one times in a large text string (also only lower case English letters).
What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose appearing times is not 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 each case is an integer, which is the number of patternsN, 1N150. Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case is one line contains a large string as the text to lookup, whose length is up to106.
At the end of the input file, number `0‘ indicates the end of input file.
Output
For each of the input cases, output the appearing times of the dominating pattern(s). If there are 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 题意: 有n个小写字母组成的字符床和一个文本串 你的任务是找出哪些字符串在文本中出现的次数最多 例如 aba 在ababa中出现2次 但是bab只出现了一次 输入n 之后n个字符串 长度为1-70 n小于等于150 之后一个文本串 长度最长为10的6次方 输出出现最多的次数 以及出现最多的字符串为什么 如果存在多个 按输入顺序排列
分析:
AC自动机模板题
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,t) for(int i=(s);i<(t);i++) const int INF = 1e9 + 9; const int N = 150 * 70 + 9; const int M = 26; char str[155][77]; struct Trie { int nex[N][M], fail[N], val[N]; int rt, L; int newnode() { memset (nex[L], -1, sizeof (nex[L]) ); val[L++] = -1; return L - 1; } void init() { L = 0; rt = newnode(); } int idx (char c) { return c - ‘a‘; } void insert (char s[], int id) { int len = strlen (s); int now = rt; for (int i = 0; i < len; i++) { if (nex[now][idx (s[i])] == -1) nex[now][idx (s[i])] = newnode(); now = nex[now][idx (s[i])]; } val[now] = id; } void getFail() { queue<int>q; fail[rt] = rt; for (int i = 0; i < M; i++) if (nex[rt][i] == -1) nex[rt][i] = rt; else { fail[nex[rt][i]] = rt; q.push (nex[rt][i]); } while (!q.empty() ) { int now = q.front(); q.pop(); for (int i = 0; i < M; i++) if (nex[now][i] == -1) nex[now][i] = nex[fail[now]][i]; else { fail[nex[now][i]] = nex[fail[now]][i]; q.push (nex[now][i]); } } } int num[155]; void quert (char s[], int n) { memset (num, 0, sizeof (num) ); int len = strlen (s); int now = rt; for (int i = 0; i < len; i++) { now = nex[now][idx(s[i])]; int tmp = now; while (tmp != rt) { if (val[tmp] != -1) num[val[tmp]]++; tmp = fail[tmp]; } } int maxn=0; for(int i=0;i<n;i++)maxn=max(num[i],maxn); printf("%d\n",maxn); for (int i = 0; i < n; i++) if (num[i]==maxn) printf ("%s\n", str[i]); } }; char buf[1000009]; Trie ac; int main() { // freopen("f.txt","r",stdin); int n; while (~scanf ("%d", &n)&&n ) { ac.init(); for (int i = 0; i < n; i++) { scanf ("%s", str[i]); ac.insert (str[i], i); } ac.getFail(); scanf ("%s", buf); ac.quert (buf, n); } return 0; }