【模板】AC自动机(加强版)
生活随笔
收集整理的這篇文章主要介紹了
【模板】AC自动机(加强版)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目描述
有NNN個(gè)由小寫字母組成的模式串以及一個(gè)文本串TTT。每個(gè)模式串可能會(huì)在文本串中出現(xiàn)多次。你需要找出哪些模式串在文本串TTT中出現(xiàn)的次數(shù)最多。
輸入輸出格式
輸入格式:輸入含多組數(shù)據(jù)。
每組數(shù)據(jù)的第一行為一個(gè)正整數(shù)NNN,表示共有NNN個(gè)模式串,1≤N≤1501 \leq N \leq 1501≤N≤150。
接下去NNN行,每行一個(gè)長度小于等于707070的模式串。下一行是一個(gè)長度小于等于10610^6106的文本串TTT。
輸入結(jié)束標(biāo)志為N=0N=0N=0。
輸出格式:對(duì)于每組數(shù)據(jù),第一行輸出模式串最多出現(xiàn)的次數(shù),接下去若干行每行輸出一個(gè)出現(xiàn)次數(shù)最多的模式串,按輸入順序排列。
輸入輸出樣例
輸入樣例#1: 復(fù)制 2 aba bab ababababac 6 beta alpha haha delta dede tata dedeltalphahahahototatalpha 0 輸出樣例#1: 復(fù)制 4 aba 2 alpha haha模板;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 200005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-3 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline ll rd() {ll x = 0;char c = getchar();bool f = false;while (!isdigit(c)) {if (c == '-') f = true;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f ? -x : x; }ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; }/*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x = 1; y = 0; return a;}ans = exgcd(b, a%b, x, y);ll t = x; x = y; y = t - a / b * y;return ans; } */string mode[maxn]; struct tree {int fail;int vis[30];int num; }ac[maxn]; int ans[maxn]; int n, siz;void ins(string s, int v) {int now = 0;for (int i = 0; i < s.length(); i++) {int o = s[i] - 'a';if (!ac[now].vis[o])ac[now].vis[o] = ++siz;now = ac[now].vis[o];}ac[now].num = v; }void getfail() {int now = 0;queue<int>q;for (int i = 0; i < 26; i++) {if (ac[0].vis[i]) {q.push(ac[0].vis[i]);ac[ac[0].vis[i]].fail = 0;}}while (!q.empty()) {int u = q.front(); q.pop();for (int i = 0; i < 26; i++) {if (ac[u].vis[i]) {ac[ac[u].vis[i]].fail = ac[ac[u].fail].vis[i];q.push(ac[u].vis[i]);}else {ac[u].vis[i] = ac[ac[u].fail].vis[i];}}} }void query(string s) {int now = 0;for (int i = 0; i < s.length(); i++) {now = ac[now].vis[s[i] - 'a'];for (int j = now; j; j = ac[j].fail)ans[ac[j].num]++;} }int main() {ios::sync_with_stdio(0);while (cin >> n && n) {ms(ac); siz = 0; ms(ans);for (int i = 1; i <= n; i++) {cin >> mode[i];ins(mode[i], i);}getfail();string k; cin >> k;query(k);int tmp = 0;for (int i = 1; i <= n; i++) {tmp = max(tmp, ans[i]);}cout << tmp << endl;for (int i = 1; i <= n; i++) {if (ans[i] == tmp) {cout << mode[i] << endl;}}}return 0; }
?
轉(zhuǎn)載于:https://www.cnblogs.com/zxyqzy/p/10252380.html
總結(jié)
以上是生活随笔為你收集整理的【模板】AC自动机(加强版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CAP理论与分布式事务解决方案
- 下一篇: POJ 3164 Command