「hdu2222」Keywords Search
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Output
Print how many keywords are contained in the description.
Sample Input
1 5 she he say shr her yasherhs
Sample Output
3
题解
裸题,注意不能用memset,strlen不能写在for里面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include<iostream> #include<cstdio> using namespace std; char s[51],m[1000001]; int T,n,sz,ans; int a[500001][27],q[500001],point[500001],danger[500001]; bool mark[500001]; void ins() { int now=1,l=strlen(s); for(int i=0;i<l;i++) { int t=s[i]-'a'+1; if(a[now][t])now=a[now][t]; else now=a[now][t]=++sz; } danger[now]++; } void acmach() { int t=0,w=1,now; q[0]=1;point[1]=0; while(t<w) { now=q[t++]; for(int i=1;i<=26;i++) { if(!a[now][i])continue; int k=point[now]; while(!a[k][i])k=point[k]; point[a[now][i]]=a[k][i]; q[w++]=a[now][i]; } } } void solve() { int k=1,l=strlen(m); for(int i=0;i<l;i++) { mark[k]=1; int t=m[i]-'a'+1; while(!a[k][t])k=point[k]; k=a[k][t]; if(!mark[k]) for(int j=k;j;j=point[j]) { ans+=danger[j]; danger[j]=0; } } printf("%d\n",ans); } int main() { scanf("%d",&T); while(T--) { sz=1;ans=0; scanf("%d",&n); for(int i=1;i<=26;i++)a[0][i]=1; while(n--) { scanf("%s",s); ins(); } acmach(); scanf("%s",m); solve(); for(int i=1;i<=sz;i++) { point[i]=danger[i]=mark[i]=0; for(int j=1;j<=26;j++) a[i][j]=0; } } return 0; } |
mark有什么用
感觉您这个是n^2的啊。。比如一串a。。
那个时候太年轻
能解释一下做法吗?OvO