设为首页 加入收藏

TOP

hdu 2222 Keywords Search(AC自动机入门)
2015-11-21 00:59:54 来源: 作者: 【 】 浏览:1
Tags:hdu 2222 Keywords Search 动机 入门

Keywords Search

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42138 Accepted Submission(s): 13289

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 retrieva l 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自动机是bfs和dp的经典应用,代码很巧妙。
代码:
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        using namespace std; const int maxn=1000010; const int bsz=26; typedef long long ll; char txt[1000010]; bool vis[510]; int ans; struct Trie { int ch[maxn][bsz],val[maxn],sz,cnt[maxn]; // ch存Trie val-节点对应的单词 cnt-节点结尾单词个数 int f[maxn],last[maxn];//f-失配指针 last-后缀链接 int newnode() { val[sz]=0; cnt[sz]=0; memset(ch[sz],-1,sizeof ch[sz]); return sz++; } void init() { sz=0; newnode(); } int idx(char c) // 取c的标号 具体看字符为什么 { return c-'a'; } void Insert(char *st,int id) { int u=0,n=strlen(st),c,i; for(i=0;i
       
         q; f[0]=0; for(i=0;i
         
        
       
      
     
    
   
  
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ - 1321 棋盘问题 n皇后问题(.. 下一篇PAT Broken Keyboard (20)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: