程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 2222 Keywords Search(AC自動機入門)

hdu 2222 Keywords Search(AC自動機入門)

編輯:C++入門知識

hdu 2222 Keywords Search(AC自動機入門)


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 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自動機是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








  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved