题意:
貌似差不多?....
对拍没什么问题,然而莫名RE...(这你也有脸贴?(这怎么能怪我...
/*========================================================================== # Last modified: 2016-03-02 19:00 # Filename: a.cpp # Description: ==========================================================================*/ #define me AcrossTheSky #include <cstdio> #include <cmath> #include <ctime> #include <string> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <queue> #include <vector> #define lowbit(x) (x)&(-x) #define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) #define FORP(i,a,b) for(int i=(a);i<=(b);i++) #define FORM(i,a,b) for(int i=(a);i>=(b);i--) #define ls(a,b) (((a)+(b)) << 1) #define rs(a,b) (((a)+(b)) >> 1) #define getlc(a) ch[(a)][0] #define getrc(a) ch[(a)][1] #define maxn 100200 #define maxm 100000 #define maxc 100 #define pi 3.1415926535898 #define _e 2.718281828459 #define INF 1070000000 using namespace std; typedef long long ll; typedef unsigned long long ull; template<class T> inline void read(T& num) { bool start=false,neg=false; char c; num=0; while((c=getchar())!=EOF) { if(c=='-') start=neg=true; else if(c>='0' && c<='9') { start=true; num=num*10+c-'0'; } else if(start) break; } if(neg) num=-num; } /*==================split line==================*/ char temp[maxn],s[60]; int ch[maxn][maxc],f[maxn],last[maxn]; vector<int> p[5000],val[maxn]; int sz,ans; void reset(){ sz=1; ans=0; memset(ch,0,sizeof(ch)); memset(last,0,sizeof(last)); memset(f,0,sizeof(f)); } int idx(char c){return c-' ';} void insert(char *s,int v){ int u=0,len=strlen(s); FORP(i,0,len-1){ int c=idx(s[i]); if (!ch[u][c]) { while(val[sz].size()) val[sz].pop_back(); //num[sz]=0; //val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u].push_back(v); } void add(int j,int id){ while(val[j].size()){ ans+=val[j].size(); FORP(i,0,val[j].size()-1) p[id].push_back(val[j][i]); //while (val[j].size()) { //val[j].pop_back(); //ans+=num[j]; num[j]=0; val[j]=0; j=last[j]; } } void find(char *T,int id){ int n=strlen(T); int j=0; FORP(i,0,n-1){ int c=idx(T[i]); j=ch[j][c]; if (val[j].size()) add(j,id); else if(last[j]) add(last[j],id); } } void getfail(){ queue<int>q; f[0]=0; FORP(c,0,maxc-1){ int u=ch[0][c]; if (u) { f[u]=0; last[u]=0; q.push(u); } } while (!q.empty()){ int r=q.front(); q.pop(); FORP(c,0,maxc-1){ int u=ch[r][c]; if (!u) {ch[r][c]=ch[f[r]][c]; continue;} q.push(u); int v=f[r]; while (v && !ch[v][c]) v=f[v]; f[u]=ch[v][c]; last[u]=val[f[u]].size()?f[u]:last[f[u]]; } } } int main(){ reset(); int n; read(n); FORP(i,1,n){ scanf("%s",s); insert(s,i); } getfail(); int m,tot=0; read(m); FORP(i,1,m) { p[i].clear(); ans=0; scanf("%s",temp); find(temp,i); if (ans) tot++; } FORP(i,1,m) if (p[i].size()){ printf("web %d: ",i); sort(p[i].begin(),p[i].end()); int j=0; //FOR(j,0,p[i].size()-1){ while (j<=p[i].size()-1){ while (j<p[i].size()-1 && p[i][j]==p[i][j+1]) j++; printf("%d%c",p[i][j],j==p[i].size()-1?' ':' '); //while (j<=p[i].size()-1 && p[i][j]==p[i][j-1]) j++; j++; } } printf("total: %d ",tot); }