日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu 2222:Keywords Search

發布時間:2025/5/22 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 2222:Keywords Search 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

        Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 131072/131072 K (Java/Others)
              Total Submission(s): 48104????Accepted Submission(s): 15333

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. 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自動機判斷一個字符串中有幾次出現了上面給的單詞。   AC自動機的講解: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];//存儲節點信息: 9 //next[i][j]=k 表示第 i個節點,在第 j個字母的位置上有個節點 k 10 //end[i]=k 表示以 i節點結尾的單詞有 k個 11 int fail[500010]; 12 int root,tot;//root表示根 tot表示總節點數 13 int newnode(){//建立新節點 14 for(int i=0;i<26;i++) next[tot][i]=-1;//新節點的兒子都是空 15 end[tot++]=0;//下一個新節點的編號 16 return tot-1;//返回當前新節點的編號 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表示應當插入位置的父親 25 for(int i=0;i<len;i++){//算上原點,新單詞插入深度為 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]++;//以該節點為結尾的單詞數量+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肯定指向根,并加入隊列 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++){//利用已經確定fail指針的節點來更新 45 if(next[now][i]==-1)//now的某孩子為空,讓這個孩子的編號改為 now的fail指向的節點的孩子,依次類推,如果都沒子節點就會指向根 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[]){//詢問該串中出現了幾個單詞 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 }

轉載于:https://www.cnblogs.com/CXCXCXC/p/5170703.html

總結

以上是生活随笔為你收集整理的hdu 2222:Keywords Search的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。