HDU 2222 Keywords Search

http://acm.hdu.edu.cn/showproblem.php?pid=2222

 

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

 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
using namespace std;
 
struct Trie {
    int nx[500010][26], fail[500010], endd[500010];
    int root, L;
     
    int newnode() {
        for(int i = 0; i < 26; i ++)
            nx[L][i] = -1;
        endd[L ++] = 0;
        return L - 1;
    }
     
    void init() {
        L = 0;
        root = newnode();
    }
     
    void Insert(char buf[]) {
        int len = strlen(buf);
        int now = root;
        for(int i = 0; i < len; i ++) {
            if(nx[now][buf[i] - 'a'] == -1)
                nx[now][buf[i] - 'a'] = newnode();
            now = nx[now][buf[i] - 'a'];
        }
        endd[now] ++;
    }
     
    void build() {
        queue<int> Q;
        fail[root] = root;
        for(int i = 0; i < 26; i ++) {
            if(nx[root][i] == -1)
                nx[root][i] = root;
            else {
                fail[nx[root][i]] = root;
                Q.push(nx[root][i]);
            }
        }
             
        while(!Q.empty() ) {
            int now = Q.front();
            Q.pop();
            for(int i = 0; i < 26; i ++) {
                if(nx[now][i] == -1)
                    nx[now][i] = nx[fail[now]][i];
                else {
                    fail[nx[now][i]] = nx[fail[now]][i];
                    Q.push(nx[now][i]);
                }
            }
        }
    }
     
    int query(char buf[]) {
        int len = strlen(buf);
        int now = root;
        int res = 0;
        for(int i = 0; i < len; i ++) {
            now = nx[now][buf[i] - 'a'];
            int temp = now;
            while( temp != root ) {
                res += endd[temp];
                endd[temp] = 0;
                temp = fail[temp];
            }
        }
        return res;
    }
     
    void debug() {
        for(int i = 0; i < L; i ++) {
            printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], endd[i]);
            for(int j = 0; j < 26; j ++)
                printf("%2d", nx[i][j]);
            printf("]\n");
        }
    }
};
 
char buf[1000010];
Trie ac;
 
int main() {
    int T;
    int n;
    scanf("%d", &T);
    while(T --) {
        scanf("%d", &n);
        ac.init();
        for(int i = 0; i < n; i ++) {
            scanf("%s", buf);
            ac.Insert(buf);
        }
        ac.build();
        scanf("%s", buf);
        printf("%d\n", ac.query(buf));
    }
    return 0;
}

  AC 自动机 kuangbin 的板子

 

复制代码
#include <bits/stdc++.h>
using namespace std;

const int maxn = 5e5 + 10;
int N;
int trie[maxn][26];
int cntword[maxn];
int fail[maxn];
int cnt = 0;

void insertWords(string s) {
    int root = 0;
    for(int i = 0; i < s.length(); i ++) {
        int nx = s[i] - 'a';
        if(!trie[root][nx])
            trie[root][nx] = ++ cnt;
        root = trie[root][nx];
    }
    cntword[root] ++;
}

void getfail() {
    queue<int> q;
    for(int i = 0; i < 26; i ++) {
        if(trie[0][i]) {
            fail[trie[0][i]] = 0;
            q.push(trie[0][i]);
        }
    }

    while(!q.empty()) {
        int tp = q.front();
        q.pop();

        for(int i = 0; i < 26; i ++) {
            if(trie[tp][i]) {
                fail[trie[tp][i]] = trie[fail[tp]][i];
                q.push(trie[tp][i]);
            } else trie[tp][i] = trie[fail[tp]][i];
        }
    }
}

int query(string s) {
    int now = 0, ans = 0;
    for(int i = 0; i < s.length(); i ++) {
        now = trie[now][s[i] - 'a'];
        for(int j = now; j && cntword[j] != -1; j = fail[j]) {
            ans += cntword[j];
            cntword[j] = -1;
        }
    }
    return ans;
}

int main() {
    int T;
    scanf("%d", &T);
    while(T --) {
        //memset(fail, 0, sizeof(fail));
        memset(trie, 0, sizeof(trie));
        memset(cntword, 0, sizeof(cntword));
        cnt = 0;
        scanf("%d", &N);
        for(int i = 0; i < N; i ++) {
            string s;
            cin >> s;
            insertWords(s);
        }
        fail[0] = 0;
        getfail();
        string t;
        cin >> t;
        printf("%d\n", query(t));
    }
    return 0;
}

/*

4
ash
bcd
shex
sha
ashe

*/
View Code
复制代码

 

(第二份代码用的另一个板子 好不容易弄清楚结果 TLE  不知道为什么 枯了 心情差到爆炸)

 

posted @   丧心病狂工科女  阅读(159)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示