标签:ase push signed copy style har inline mode reg
有NNN个由小写字母组成的模式串以及一个文本串TTT。每个模式串可能会在文本串中出现多次。你需要找出哪些模式串在文本串TTT中出现的次数最多。
输入含多组数据。
每组数据的第一行为一个正整数NNN,表示共有NNN个模式串,1≤N≤1501 \leq N \leq 1501≤N≤150。
接下去NNN行,每行一个长度小于等于707070的模式串。下一行是一个长度小于等于10610^6106的文本串TTT。
输入结束标志为N=0N=0N=0。
输出格式:对于每组数据,第一行输出模式串最多出现的次数,接下去若干行每行输出一个出现次数最多的模式串,按输入顺序排列。
2 aba bab ababababac 6 beta alpha haha delta dede tata dedeltalphahahahototatalpha 0
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; }
标签:ase push signed copy style har inline mode reg
原文地址:https://www.cnblogs.com/zxyqzy/p/10252380.html