hdu 2222 AC自动机模板题(指针版+数组版)

Keywords Search

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


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
 

指针版
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

#pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan
#define clr(s,x) memset(s,x,sizeof(s))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) (x&(-x))
#define PB push_back
#define For(i,a,b) for(int i=a;i<b;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)

typedef long long               LL;
typedef unsigned int            uint;
typedef unsigned long long      ULL;
typedef vector<int>             vint;
typedef vector<string>          vstring;

void RI (int& x){
    x = 0;
    char c = getchar ();
    while (c == ' '||c == '\n')    c = getchar ();
    bool flag = 1;
    if (c == '-'){
        flag = 0;
        c = getchar ();
    }
    while (c >= '0' && c <= '9'){
        x = x * 10 + c - '0';
        c = getchar ();
    }
    if (!flag)    x = -x;
}
void RII (int& x, int& y){RI (x), RI (y);}
void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}


const double PI  = acos(-1.0);
const int maxn   = 1e6 + 100;
const int maxm   = 55;
const int kind   = 26;
const LL mod     = 1e9 + 7;
const double eps = 1e-9;
const int INF    = 0x7fffffff;

/************************************END DEFINE*********************************************/

struct node{
    int cnt;
    node *nxt[kind];
    node *fail;
};

node *q[maxn];
char kw[maxm];
char s[maxn];
int head,tail;
node *root;

void add_word(char *word,node *root){
    int idx,len;
    node *p = root,*newnode;
    len = strlen(word);
    For(i,0,len){
        idx = word[i] - 'a';
        if(!p->nxt[idx]){
            newnode = (struct node *)malloc(sizeof(struct node));
            For(j,0,kind)newnode->nxt[j] = 0;
            newnode->cnt = 0;
            newnode->fail = 0;
            p->nxt[idx] = newnode;
        }
        p = p->nxt[idx];
    }
    p->cnt++;
}

void build(node *root){
    head = 0,tail = 1;
    q[head] = root;
    node *tmp,*p;
    while(head<tail){
        tmp = q[head++];
        For(i,0,kind){
            if(tmp->nxt[i]){
                if(tmp == root)tmp->nxt[i]->fail = root;
                else{
                    p = tmp->fail;
                    while(p){
                        if(p->nxt[i]){
                            tmp->nxt[i]->fail = p->nxt[i];
                            break;
                        }
                        p = p->fail;
                    }
                    if(!p)tmp->nxt[i]->fail = root;
                }
                q[tail++] = tmp->nxt[i];
            }
        }
    }
}

int query(node *root){
    int ct = 0,idx = 0,len = strlen(s);
    node *p = root;
    For(i,0,len){
        idx = s[i]-'a';
        while(!p->nxt[idx]&&p!=root)p = p->fail;
        p=p->nxt[idx];
        if(!p)p=root;
        node *tmp = p;
        while(tmp!=root){
            if(tmp->cnt>=0){
                ct+=tmp->cnt;
                tmp->cnt=-1;
            }
            else break;
            tmp = tmp->fail;
        }
    }
    return ct;
}

int main()
{
    int T;
    RI(T);
    while(T--){
        int n;
        RI(n);
        node *root = (struct node *)malloc(sizeof(struct node));
        For(i,0,kind)root->nxt[i] = 0;
        root->fail = 0;
        root->cnt = 0;
        while(n--){
            scanf("%s",kw);
            add_word(kw,root);
        }
        build(root);
        scanf("%s",s);
        int ans = query(root);
        printf("%d\n",ans);
    }
    return 0;
}
数组版
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

#pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan
#define clr(s,x) memset(s,x,sizeof(s))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) (x&(-x))
#define PB push_back
#define For(i,a,b) for(int i=a;i<b;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)

typedef long long               LL;
typedef unsigned int            uint;
typedef unsigned long long      ULL;
typedef vector<int>             vint;
typedef vector<string>          vstring;

void RI (int& x){
    x = 0;
    char c = getchar ();
    while (c == ' '||c == '\n')    c = getchar ();
    bool flag = 1;
    if (c == '-'){
        flag = 0;
        c = getchar ();
    }
    while (c >= '0' && c <= '9'){
        x = x * 10 + c - '0';
        c = getchar ();
    }
    if (!flag)    x = -x;
}
void RII (int& x, int& y){RI (x), RI (y);}
void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}

/************************************END DEFINE*********************************************/

const int maxn = 500010;
const int kind = 26;
int idx(char c){return c-'a';}
int chd[maxn][kind],sz,val[maxn],fail[maxn],last[maxn];
void init(){
    sz=1;
    memset(chd[0],0,sizeof(chd[0]));
    memset(val,0,sizeof(val));
    memset(fail,0,sizeof(fail));
    memset(last,0,sizeof(last));
}
void add(char *p){
    int u=0;
    int len = strlen(p);
    for(int i=0;i<len;i++){
        int c = idx(p[i]);
        if(chd[u][c]==0){
            memset(chd[sz],0,sizeof(chd[sz]));
            chd[u][c]=sz++;
        }
        u=chd[u][c];
    }
    val[u]++;
}
int getFail(){
    queue<int> q;
    fail[0]=0;
    for(int c=0;c<kind;c++){
        int u=chd[0][c];
        if(u){
            fail[u]=0;q.push(u);last[u]=0;
        }
    }
    while(!q.empty()){
        int r=q.front(); q.pop();
        for(int c=0;c<kind;c++){
            int u=chd[r][c];
            if(!u){
                chd[r][c]=chd[fail[r]][c];
                continue;
            }
            q.push(u);
            int v=fail[r];
            while(v&&!chd[v][c]) v=fail[v];
            fail[u]=chd[v][c];
            last[u]=(val[fail[u]])?fail[u]:last[fail[u]];
        }
    }
}

int query(char *T){
    int len = strlen(T), j = 0,ans = 0;
    for(int i = 0; i < len; i++){
        int c = idx(T[i]);
        while(j && chd[j][c]==0) j = fail[j];
        j = chd[j][c];
        int temp = j;
        while(temp && val[temp]){
            ans+=val[temp];
            val[temp]=0;
            temp = fail[temp];
        }
    }
    return ans;
}

char t[55];
char s[1000005];

int main()
{
    int T;
    RI(T);
    while(T--){
        int n;
        RI(n);
        init();
        while(n--){
            scanf("%s",t);
            add(t);
        }
        getFail();
        scanf("%s",s);
        printf("%d\n",query(s));
    }
    return 0;
}



Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标、文本、图片、视频链接(保留排) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值