【HDU 3065】 病毒侵袭持续中 AC自动机

4 篇文章 0 订阅
1 篇文章 0 订阅

Problem Description
小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?

Input
第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。

Output
按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。

Sample Input
3
AA
BB
CC
ooxxCC%dAAAoen…END

Sample Output
AA: 2
CC: 1
Hint
Hit:
题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。
计数策略也可一定程度上从Sample中推测。

题意:统计每个模式串在文本串中以子串形式出现的个数

思路(AC自动机):

比较裸的AC自动机模板题,既然要找子串,就把原来模板中的标记清除,使其多次遍历一个模式串,每次遍历完就对应字符串计数+1,同时在插入的时候就要对每个字符串记录位置。至于为什么删除标记就行了,因为我们fail指针对失配的字符的作用就是找到最长后缀,然后继续匹配,比如文本串AAAA匹配模式串AAA,文本串第四个A的时候,由于getfail函数的预处理,我们模式串的第三个A的A子树,又指回自身,然后第四个A的时候,又停在了第三个A的位置上,计数+1,如图:
在这里插入图片描述坑点在于文本串可能有空格,要用gets或者getline,同时还有其他字符的影响,有其他字符就将当前匹配位置回到根就行了。最后注意一下多case,因此wa了几次

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define maxn 100000+500
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxm = 100000+5;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') ch = getchar();while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x; }


//const int maxn =  2*1e6+9;
int n;
int trie[maxn][27]; //字典树
int cntword[maxn];  //记录该单词出现次数
int fail[maxn];     //失败时的回溯指针
int cnt = 0;
int vis[maxn];
int curlen[maxn];
void init()
{
    cnt = 0;
    memset(trie,0,sizeof(trie));
    memset(cntword,0,sizeof(cntword));
    memset(fail,0,sizeof(fail));
}

void insertWords(string s,int pos)
{
    int root = 0;
    for(int i=0; i<s.size(); i++)
    {
        int next = s[i] - 'A';
        if(!trie[root][next])
            trie[root][next] = ++cnt;
        root = trie[root][next];
    }
    cntword[root]++;
    vis[root] = pos;
    curlen[root] = s.size();
  //  cout<<root<<' '<<s<<endl;
}
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 now = q.front();
        q.pop();
        for(int i=0; i<26; i++)     //查询26个字母
        {
            if(trie[now][i])
            {
                fail[trie[now][i]] = trie[fail[now]][i];
                q.push(trie[now][i]);
            }
            else
                trie[now][i] = trie[fail[now]][i];
        }
    }
}

int res[1005];
int ans  = 0;
void query(string s)
{
    int now = 0;
    int start = 0;
    memset(res,0,sizeof(res));
    for(int i=0; i<s.size(); i++)
    {
        if(s[i]>'Z'||s[i]<'A')
        {
           now = 0;
           continue;
        }
        now = trie[now][s[i]-'A'];
        for(int j=now; j ; j=fail[j])
        {
//cout<<i<<' '<<j<<endl;
            if(cntword[j])
            {
                ans += cntword[j];
                res[vis[j]] += cntword[j];
            }
        }
    }

}

int main()
{
    while(~scanf("%d",&n))
    {
        memset(res,0,sizeof(res));
        init();

        vector<string> item;
        for(int i=1; i<=n; i++)
        {
            string s;
            cin>>s;
            item.pb(s);
            insertWords(s,i);
        }
        string s;
        char ch = getchar();
        getline(cin,s);
        getFail();
        query(s);
        for(int i=1; i<1001; i++)
        {
            if(res[i]!=0)
            {
                cout<<item[i-1]<<':'<<' '<<res[i]<<'\n';
            }
        }
    }

    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值