【每日一题(30)】Keywords Search(AC自动机模版题) HDU-2222

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 72957 Accepted Submission(s): 24969

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

solution

这道题是AC自动机的模版题,就是trie+KMP的AC自动机
后面我会出一个专题描述AC自动机
(有同学可能会问为什么没有29,那是因为我之前想写SUST的校赛题解,但是还没写,先空着吧=-=)

code

#include<bits/stdc++.h>
using namespace std;
typedef struct node {
  int sum;
  struct node* next[26];
  struct node* fail;
}trie;
trie *root;
int cnt;
void init(struct node* temp) {
  temp->sum = 0;
  temp->fail = NULL;
  for(int i = 0;i < 26; i++) {
    temp->next[i] = NULL;
  }
}
void insert(string &str) {
  int len = str.size();
  trie* now = root;
  for(int i = 0;i < len; i++) {
    int x = str[i] - 'a';
    if(now->next[x] == NULL) {
      trie* node = new trie;
      init(node);
    }
    now = now->next[x];
  }
  now->sum++;
}
void buildfail(trie* node) {
  queue<trie*> q;
  q.push(node);
  while(!q.empty()) {
    node = q.front();q.pop();
    for(int i = 0;i < 26; i++){
      if(node->next[i] != NULL){
        if(node == root){
          node->next[i]->fail = root;
        }else{
          trie* temp = node->fail;
          while(temp != NULL){
            if(temp->next[i] != NULL){
              node->next[i]->fail = temp->next[i];
              break;
            }
            temp = temp->fail;
          }
          if(temp == NULL) node->next[i]->fail = root;
        }
        q.push(node->next[i]);
      }
    }
  }
}
void searchstr(string &str){
  trie *node = root;
  int len = str.size();
  for(int i = 0;i < len; i++){
    int x = str[i] - 'a';
    while(node->next[x] == NULL && node != root) node = node->fail;
    node = node->next[x];
    if(node == NULL) node = root;
    trie* temp = node;
    while(temp != root){
      if(temp->sum >= 0){
        cnt += temp->sum;
        temp->sum = -1;
      }else
        break;
        temp = temp->fail;
    }
  }
}
int main() {
  int t,n;
  cin >> t;
  root = new trie;
  init(root);
  getchar();
  while(t--) {
    cin >> n;
    getchar();
    string str;
    while(n--) {
      getline(cin,str);
      insert(str);
    }
    getline(cin,str);
    buildfail(root);
    searchstr(str);
    cout << cnt << endl;
  }

  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40861916/article/details/79837853