【hdu 2222】Keywords Search

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

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

【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=2222

【题解】

给你一篇文章和一些关键字;
让你在文章中匹配这些关键字;
问你能够匹配到多少个关键字;
AC自动机的模板题;
网上资料很多;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXN = 1e4+10;
const int MAXS = 1e6+10;
const int MAXS2 = 60;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n,a[MAXN*MAXS2][27],tot,cnt[MAXN*MAXS2],f[MAXN*MAXS2];
bool flag[MAXN*MAXS2];
queue <int> dl;
char s[MAXS],s1[MAXS2];

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    int T;
    rei(T);
    while (T--)
    {
        tot = 1;
        rei(n);
        rep1(i,1,n)//输入的数据创建字典树;
        {
            scanf("%s",s1);
            int len = strlen(s1),now = 1;
            rep1(j,0,len-1)
            {
                int k = s1[j]-'a'+1;
                if (!a[now][k])
                    now = a[now][k] = ++tot;
                else
                    now = a[now][k];
            }
            cnt[now]++;
        }
        //acauto
        rep1(i,1,26) //便于处理边界
            a[0][i] = 1;//在找失配边的时候,如果最后找不到了指向1号节点.
        dl.push(1);f[1] = 0;
        while (!dl.empty())
        {
            int x = dl.front();
            dl.pop();
            rep1(j,1,26)
                {
                    if (!a[x][j])
                        continue;
                    int k = f[x];
                    while (!a[k][j]) k = f[k];//沿着失配边往上走;
                    f[a[x][j]] = a[k][j];//这个节点的失配点
                    dl.push(a[x][j]);//继续沿着边搞;
                }
        }
        scanf("%s",s);
        int len = strlen(s);
        int now = 1;
        LL ans = 0;
        rep1(i,0,len-1)
        {
            flag[now] = true;//这个flag数组是用来剪枝的
            int k = s[i]-'a'+1;//不然你在前面,然后往前失配了,会一直还要往前走,但实际上那些节点已经被累加过答案了
            //cnt[x]已经变为0了,不用再管它们了
            while (!a[now][k]) now = f[now];//如果没有匹配到就沿着失配边走
            now = a[now][k];//走到那个点
            if (!flag[now])
                for (int j = now;j;j = f[j])//可能还有其他字符串也是以k字母结尾
                {//且为这个s的后缀;
                    ans += cnt[j];//可能有重复的字符;
                    cnt[j] = 0;
                }
        }
        cout << ans << endl;
        rep1(i,1,tot)
        {
            f[i] = cnt[i] = flag[i] = false;
            rep1(j,1,26)
                a[i][j] = 0;
        }
    }
    return 0;
}

↓↓模块化

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXS1 = 50+10;
const int MAXS2 = 1e6+10;
const int MAXN = 1e4+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n,a[MAXN*MAXS1][27],cnt[MAXN*MAXS1],f[MAXN*MAXS1],tot;
int flag[MAXN*MAXS1];
LL ans;
char s1[MAXS1],s2[MAXS2];
queue <int> dl;

void ins()
{
    int len = strlen(s1),now = 1;
    rep1(i,0,len-1)
    {
        int t = s1[i]-'a'+1;
        if (!a[now][t])
            a[now][t] = ++tot;
        now = a[now][t];
    }
    cnt[now]++;
}

void ac()
{
    dl.push(1);f[1] = 0;
    while (!dl.empty())
    {
        int x = dl.front();
        dl.pop();
        rep1(i,1,26)
        {
            if (!a[x][i])
                continue;
            int k = f[x];
            while (!a[k][i]) k = f[k];
            f[a[x][i]] = a[k][i];
            dl.push(a[x][i]);
        }
    }
}

void solve()
{
    int len = strlen(s2),now = 1;
    rep1(i,0,len-1)
    {
        flag[now] = true;
        int t = s2[i]-'a'+1;
        while (!a[now][t]) now = f[now];
        now = a[now][t];
        if (!flag[now])
            for (int j = now;j;j = f[j])
            {
                ans += cnt[j];
                cnt[j] = 0;
            }
    }
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    int T;
    rei(T);
    while (T--)
    {
        rep1(i,1,tot)
        {
            flag[i] = f[i] = cnt[i] = 0;
            rep1(j,1,26)
                a[i][j] = 0;
        }
        ans = 0;
        tot = 1;//**************
        rep1(i,1,26)
            a[0][i] = 1;
        rei(n);
        rep1(i,1,n)
            {
                scanf("%s",s1);
                ins();
            }
        ac();
        scanf("%s",s2);
        solve();
        cout << ans << endl;
    }
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(131)  评论(0编辑  收藏  举报