HDU3065

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n;
char s[1005][55];
char str[2000005];

struct AC_Automaton
{
    int ch[50005][26], fail[50005], id[50005];
    int tot;

    void init(){
        tot = 0;
        memset(ch, -1, sizeof(ch));
        memset(fail, 0, sizeof(fail));
        memset(id, 0, sizeof(id));
    }

    void insert(char *ss, int cnt){
        int p = 0, len = strlen(ss);
        for (int i = 0; i < len; i++){
            if(ch[p][ss[i] - 'A'] == -1){
                ch[p][ss[i] - 'A'] = ++tot;
            }
            p = ch[p][ss[i] - 'A'];
        }
        id[p] = cnt;
    }

    void build(){
        int l = 0, r = 0, q[50005];
        for (int i = 0; i < 26; i++){
            if(ch[0][i] == -1){
                ch[0][i] = 0;
            }else{
                q[r++] = ch[0][i];
            }
        }
        while(l < r){
            int p = q[l++];
            for (int i = 0; i < 26; i++){
                if(ch[p][i] == -1){
                    ch[p][i] = ch[fail[p]][i];
                }else{
                    fail[ch[p][i]] = ch[fail[p]][i];
                    q[r++] = ch[p][i];
                }
            }
        }
    }

    void count(char *ss){
        int p = 0, ans[1005];
        int len = strlen(ss);
        memset(ans, 0, sizeof(ans));
        //memset(vis, 0, sizeof(vis));
        for (int i = 0; i < len; i++){
            if(ss[i] < 'A' || ss[i] > 'Z'){
                p = 0;
                continue;
            }
            p = ch[p][ss[i] - 'A'];
            int temp = p;
            while(temp && id[temp]){
                ans[id[temp]]++;
                temp = fail[temp];
            }
        }
        for (int i = 1; i <= n; i++){
            if(ans[i] != 0){
                printf("%s: %d\n", s[i], ans[i]);
            }
        }
    }

}AC;

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    while(scanf("%d", &n) == 1){
        AC.init();
        for (int i = 1; i <= n; i++){
            scanf("%s", s[i]);
            AC.insert(s[i], i);
        }
        AC.build();
        scanf("%s", str);
        AC.count(str);
    }

    return 0;
}
/**/

测一下这种数组:

2
AA
AAA
ooxxCC%dAAAoen....ENDooxxCC%dAAAoen....ENDooxxCC%dAAAoen....ENDooxxCC%dAAAoen....END

AA: 8

AAA: 4

 


ABA
AAA
diasifsAojfsdBAdfinasAA

没有输出

全部评论

相关推荐

10-07 23:57
已编辑
电子科技大学 Java
八街九陌:博士?客户端?开发?啊?
点赞 评论 收藏
分享
10-28 11:04
已编辑
美团_后端实习生(实习员工)
一个2人:我说几个点吧,你的实习经历写的让人觉得毫无含金量,你没有挖掘你需求里的 亮点, 让人觉得你不仅打杂还摆烂。然后你的简历太长了🤣你这个实习经历看完,估计没几个人愿意接着看下去, sdk, 索引这种东西单拎出来说太顶真了兄弟,好好优化下简历吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务