當(dāng)前位置:
首頁 >
hdu 2222:Keywords Search
發(fā)布時(shí)間:2025/5/22
37
豆豆
生活随笔
收集整理的這篇文章主要介紹了
hdu 2222:Keywords Search
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 48104????Accepted Submission(s): 15333
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. Output Print how many keywords are contained in the description. Sample Input 1 5 she he say shr her yasherhs Sample Output 3 題解: 用AC自動(dòng)機(jī)判斷一個(gè)字符串中有幾次出現(xiàn)了上面給的單詞。 AC自動(dòng)機(jī)的講解:http://blog.csdn.net/mobius_strip/article/details/22549517 ?http://blog.csdn.net/niushuai666/article/details/7002736 ?http://www.cnblogs.com/kuangbin/p/3164106.html
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<string.h> 5 #include<queue> 6 using namespace std; 7 struct Trie{ 8 int next[500010][26],end[500010];//存儲(chǔ)節(jié)點(diǎn)信息: 9 //next[i][j]=k 表示第 i個(gè)節(jié)點(diǎn),在第 j個(gè)字母的位置上有個(gè)節(jié)點(diǎn) k 10 //end[i]=k 表示以 i節(jié)點(diǎn)結(jié)尾的單詞有 k個(gè) 11 int fail[500010]; 12 int root,tot;//root表示根 tot表示總節(jié)點(diǎn)數(shù) 13 int newnode(){//建立新節(jié)點(diǎn) 14 for(int i=0;i<26;i++) next[tot][i]=-1;//新節(jié)點(diǎn)的兒子都是空 15 end[tot++]=0;//下一個(gè)新節(jié)點(diǎn)的編號 16 return tot-1;//返回當(dāng)前新節(jié)點(diǎn)的編號 17 } 18 void init(){//初始化,建立Trie的入口 19 tot=0; 20 root=newnode(); 21 } 22 void insert(char buf[]){//插入單詞 23 int len=strlen(buf); 24 int now=root;//now表示應(yīng)當(dāng)插入位置的父親 25 for(int i=0;i<len;i++){//算上原點(diǎn),新單詞插入深度為 1 ~ len 26 if(next[now][buf[i]-'a']==-1) next[now][buf[i]-'a']=newnode(); 27 now=next[now][buf[i]-'a']; 28 } 29 end[now]++;//以該節(jié)點(diǎn)為結(jié)尾的單詞數(shù)量+1 30 } 31 void build(){//建立fail指針 32 queue<int> Q; 33 fail[root]=root;//根的fail指向根 34 for(int i=0;i<26;i++){ 35 if(next[root][i]==-1)//如果根的某些孩子為空,就把這些孩子看做根 36 next[root][i]=root; 37 else{//如果不為空,這些孩子的fail肯定指向根,并加入隊(duì)列 38 fail[next[root][i]]=root; 39 Q.push(next[root][i]); 40 } 41 } 42 while(!Q.empty()){ 43 int now=Q.front(); Q.pop(); 44 for(int i=0;i<26;i++){//利用已經(jīng)確定fail指針的節(jié)點(diǎn)來更新 45 if(next[now][i]==-1)//now的某孩子為空,讓這個(gè)孩子的編號改為 now的fail指向的節(jié)點(diǎn)的孩子,依次類推,如果都沒子節(jié)點(diǎn)就會(huì)指向根 46 next[now][i]=next[fail[now]][i]; 47 else{ 48 fail[next[now][i]]=next[fail[now]][i]; 49 Q.push(next[now][i]); 50 } 51 } 52 } 53 } 54 int query(char buf[]){//詢問該串中出現(xiàn)了幾個(gè)單詞 55 int len=strlen(buf),now=root; 56 int res=0; 57 for(int i=0;i<len;i++){ 58 now=next[now][buf[i]-'a']; 59 int temp=now; 60 while(temp!=root){ 61 res+=end[temp]; 62 end[temp]=0; 63 temp=fail[temp]; 64 } 65 } 66 return res; 67 } 68 }ac; 69 char buf[1000010]; 70 int T,n; 71 int main(){ 72 scanf("%d",&T); 73 while(T--){ 74 scanf("%d",&n); 75 ac.init(); 76 for(int i=0;i<n;i++){ 77 scanf("%s",buf); 78 ac.insert(buf); 79 } 80 ac.build(); 81 scanf("%s",buf); 82 printf("%d\n",ac.query(buf)); 83 } 84 return 0; 85 }
轉(zhuǎn)載于:https://www.cnblogs.com/CXCXCXC/p/5170703.html
總結(jié)
以上是生活随笔為你收集整理的hdu 2222:Keywords Search的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 押车贷款抵押吗
- 下一篇: BZOJ 3870: Our happy