标签:algo map scanf none main stack play empty onclick
不知道为啥这个题总是WA
找了好长时间也没找出来
求大佬帮忙看看
#include<iostream> #include<iomanip> #include<cstring> #include<sstream> #include<algorithm> #include<cstdio> #include<cmath> #include<stdlib.h> #include<string> #include<queue> #include<vector> #include<map> #include<stack> //#include<bits/stdc++.h> #define _for(i,a,b) for(int i=a;i<=b;i++) using namespace std; typedef long long ll; const int mod =1e6+7; double esp=1e-6; int INF =0x3f3f3f3f; const int inf = 1<<28; const int MAXN=3e5+5; int fail[MAXN]; int ch[MAXN][27]; int cntword[MAXN]; int tot; bool f[MAXN]; map<string,int> mp; void insertword(string s) { int u=0; for(int i=0;i<s.size();i++) { int v=s[i]-‘a‘; if(!ch[u][v]) { ch[u][v]=++tot; } u=ch[u][v]; } mp[s]=u; f[u]=1; } void getfail() { queue<int> q; for(int i=0;i<26;i++) { if(ch[0][i]) { fail[ch[0][i]]=0; q.push(ch[0][i]); } } while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0;i<26;i++) { if(ch[u][i]) { fail[ch[u][i]]=ch[fail[u]][i]; q.push(ch[u][i]); } else ch[u][i]=ch[fail[u]][i]; } } //for(int i=1;i<=tot;i++) //printf("%d %d\n",i,fail[i]); } int query(string s) { int u=0,ans=-1; for(int i=0;i<s.size();i++) { u=ch[u][s[i]-‘a‘]; for(int j=u;j&&f[j];j=fail[j]) { ans=max(ans,++cntword[j]); //ans+=cntword[j]; //cntword[j]=-1; } } return ans; } string s; string p[MAXN]; int main() { int t; while(~scanf("%d",&t)&&t) { mp.clear(); memset(ch,0,sizeof(ch)); memset(fail,0,sizeof(fail)); memset(cntword,0,sizeof(cntword)); memset(f,0,sizeof(f)); tot=0; for(int i=1;i<=t;i++) { cin>>p[i]; insertword(p[i]); } getfail(); cin>>s; int ans=query(s); printf("%d\n",ans); for(int i=1;i<=t;i++) { if(ans==cntword[mp[p[i]]])cout<<p[i]<<endl; } } return 0; }
标签:algo map scanf none main stack play empty onclick
原文地址:https://www.cnblogs.com/kayiko/p/12305928.html