Hdu 2896 Virus Invasion (AC Automata)

Keywords: ascii

Virus invasion
Time Limit: 2000 Memory Limit: 32768
Total Submission(s): 24853 Accepted Submission(s): 5930
Problem Description
When the sun's glory is gradually obscured by the moon, the world loses its light, and the earth ushers in its darkest hour... At such moments, people are very excited - how happy it is that we can see the once-in-500-year wonder of the world in our lifetime.~~
But there are always some websites on the internet, which began to spread viruses with curiosity and under the banner of introducing solar eclipses. Little t unfortunately became one of the victims. Xiao t was so angry that he decided to find out all the viral websites in the world. Of course, everyone knows it's impossible. Little T insisted on accomplishing the impossible task. He said, "There is an infinite shortage of children and grandchildren!" (Yu Gong has a successor.)
Everything is difficult at the beginning. Xiaot has collected a lot of virus signatures and a lot of source codes of weird websites. He wants to know which of these websites are viruses and what kind of viruses are they carrying. By the way, I also want to know how many viral websites he collects. At this time, he did not know where to start. So I would like to ask for your help. Little t is an acute person, so the sooner the problem is solved, the better.~~
Input
The first line, an integer N (1<=N<=500), represents the number of virus signatures.
Next, N lines, each representing a virus signature, the length of the signature string is between 20 and 200.
Each virus has a number, which is 1-N.
Virus signatures of different numbers will not be the same.
In the next line, there is an integer M (1<=M<=1000), which represents the number of websites.
Next, M lines, each representing a website source code, source string length between 7000 - 10000.
Each website has a number, which is 1-M.
All the characters in the above strings are ASCII code visible characters (excluding carriage return).
Output
The output format is as follows: output from small to large according to the website number, virus-carrying website number and virus number, one poisonous website information per line.
web Site Number: Virus Number, Virus Number.
There is a space after the colon. Virus numbers are arranged from small to large. The two viruses are separated by a space. If a website contains viruses, the number of viruses will not exceed three.
The last line outputs statistics in the following format
total: Number of viral websites
There is a space after the colon.
Sample Input
3
aaa
bbb
ccc
2
aaabbbccc
bbaacc
Sample Output
web 1: 1 2 3
total: 1
Source
2009 Multi-University Training Contest 10 - Host by NIT

/*
The title does not say only lowercase letters
 Then I checked the ASCII code 2333. 
So gg.
There's a bit of space cards.
Finally, don't forget to mark it again.
All that's left is the naked question. 
*/
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define MAXN 100010
using namespace std;
int n,m,l,tot=1,ans,cut,a[501],p[5],cnt,fail[MAXN];
char s[10010];
bool mark[MAXN];
struct data{int x[128],b;}tree[MAXN];
queue<int>q;
void add(int t)
{
    int now=1;l=strlen(s+1);
    for(int i=1;i<=l;i++)
    {
        int x=s[i];
        if(!tree[now].x[x]) tree[now].x[x]=++tot;
        now=tree[now].x[x];
    }
    tree[now].b=t,a[t]=now;
    return ;
}
void get_fail()
{
    for(int i=1;i<=127;i++) tree[0].x[i]=1;
    q.push(1);
    while(!q.empty())
    {
        int now=q.front();q.pop();
        for(int i=1;i<=127;i++)
        {
            if(!tree[now].x[i]) continue;
            int k=fail[now];
            while(!tree[k].x[i]) k=fail[k];
            k=tree[k].x[i];
            fail[tree[now].x[i]]=k;
            q.push(tree[now].x[i]);
        }
    }
    return ;
}
void Mark(int t)
{
    l=strlen(s+1);
    int now=1;cut=0;
    for(int i=1;i<=l;i++)
    {
        int x=s[i];
        mark[now]=true;
        while(!tree[now].x[x]) now=fail[now];
        now=tree[now].x[x];
        if(!mark[now])
        {
            mark[now]=true;
            int k=now;
            while(k)
            {
                if(tree[k].b)
                {
                    p[++cut]=tree[k].b;
                    tree[k].b=0;
                }
                k=fail[k];
            }
        }
    }
    if(cut)
    {
        ans++;printf("web %d:",t);
        sort(p+1,p+cut+1);
        for(int i=1;i<=cut;i++) printf(" %d",p[i]);
        printf("\n");
    }
    memset(mark,0,sizeof mark);
    for(int i=1;i<=n;i++) tree[a[i]].b=i;
    return ;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%s",s+1),add(i);
    get_fail();
    scanf("%d",&m);
    for(int i=1;i<=m;i++) scanf("%s",s+1),Mark(i);
    printf("total: %d\n",ans);
    return 0;
}

Posted by gio2k on Wed, 17 Apr 2019 14:42:32 -0700