LA 4670 Dominating Patterns——AC自动机

网友投稿 576 2022-09-04

LA 4670 Dominating Patterns——AC自动机

LA 4670 Dominating Patterns——AC自动机

The archaeologists are going to decipher a very mysterious “language”. Now, they know many languagepatterns; 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 Englishletters).What matters most is that which patterns are the dominating patterns. Dominating pattern is thepattern whose appearing times is not less than other patterns.It is your job to find the dominating pattern(s) and their appearing times

之前做了HDU3065,和这道题差不多,所以基本套上板子就a了

#include #include #include #include #include using namespace std;const int MAXL = 1e6 + 10;char str1[200][100], str2[MAXL];int n, maxv, ans[200];struct AC { int tot, val[20000], fail[20000], child[20000][30]; void init() { tot = 1; memset(val, 0, sizeof(val)); memset(fail, 0, sizeof(fail)); memset(child[0], 0, sizeof(child[0])); } void Insert(char *P, int t) { int root = 0; for (int i = 0; P[i]; i++) { if (!child[root][P[i] - 'a']) { memset(child[tot], 0, sizeof(child[tot])); child[root][P[i] - 'a'] = tot++; } root = child[root][P[i] - 'a']; } if (!val[root]) val[root] = t; } void Getfail() { queue q; for (int i = 0; i < 26; i++) { if (child[0][i]) q.push(child[0][i]); } while (!q.empty()) { int root = q.front(); q.pop(); for (int i = 0; i < 26; i++) { int u = child[root][i]; if (!u) { child[root][i] = child[fail[root]][i]; continue; } q.push(u); int v = fail[root]; while (v && !child[v][i]) v = fail[v]; fail[u] = child[v][i]; } } } void Search(char *T) { Getfail(); int root = 0, temp = 0; for (int i = 0; T[i]; i++) { temp = root = child[root][T[i] - 'a']; while (temp && val[temp]) { ans[val[temp]]++; maxv = max(maxv, ans[val[temp]]); temp = fail[temp]; } } }}ac;int main() { while (~scanf("%d", &n) && n) { ac.init(); for (int i = 1; i <= n; i++) { scanf("%s", str1[i]); ac.Insert(str1[i], i); } memset(ans, 0, sizeof(ans)); maxv = 0; scanf("%s", str2); ac.Search(str2); printf("%d\n", maxv); for (int i = 1; i <= n; i++) { if (ans[i] == maxv) { printf("%s\n", str1[i]); } } }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LA 3026 Period——kmp求n次最小循环节
下一篇:Redis实战之限制操作频率(redis的持久方式)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~