Hdoj 2896 virus attacks "AC automata basics"

Source: Internet
Author: User

Virus attack

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 15893 Accepted Submission (s): 4055


Problem description When the sun's glow is gradually obscured by the moon, the world loses its light, the Earth ushered in the darkest moment .... At such times, people are very excited-we can live to see 500 years of the world's wonders, that is how happy things ah ~ ~
But there are always some websites on the internet, beginning with the curiosity of the people, introducing the banner of the solar eclipse, spreading the virus in a big way. Little T unfortunately became one of the victims. Little T was so angry that he decided to find all the virus-infected websites in the world. Of course, everyone knows it's impossible. Small T but insisted to complete this cannot task, he said: "Descendants infinite chamber also!" "(The Foolish Gong successors).
Everything at the beginning of the difficult, small t collected a lot of virus signatures, but also collected a number of strange web site source, he would like to know which of these sites are virus, and what kind of virus? By the way, I wonder how many websites he collects with viruses. At this time he did not know where to start. So I would like to ask you to help. Small T is also a short-tempered oh, so the faster the better to solve the problem Oh ~ ~

Input first line, an integer N (1<=n<=500) that represents the number of virus signatures.
Next n rows, each line represents a virus signature, and the signature string length is between 20-200.
Each virus has a number, according to 1-n.
Virus signatures of different numbers will not be the same.
After this line, there is an integer M (1<=m<=1000) that represents the number of sites.
Next m line, each line represents a website source code, the source string length between 7000-10000.
Each site has a number, depending on the 1-m.
The characters in the above string are ASCII visible characters (not including carriage returns).

Output is exported sequentially by site number from small to large output, with the virus's website number and with the virus number, each line of a poison site information.
Web site number: Virus ID virus number ...
There is a space after the colon, the virus number is arranged from small to large, two virus numbers separated by a space, if a website contains a virus, the number of viruses will not exceed 3.
The last line of output statistics, the following format
Total: Number of sites with viruses
There is a space after the colon.

Sample Input
3aaabbbccc2aaabbbcccbbaacc

Sample Output
Web 1:1 2 3total:1


Pit Ah, visible character 32-126 ...

Idea: Use the word[] array to record the number of the virus string.

AC Code:

#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 100000+    10using namespace Std;char str[maxn];struct trie{int next[maxn][100], FAIL[MAXN], WORD[MAXN];    int root, L;        int NewNode () {for (int i = 0; i <; i++)//Drunk not just lowercase letters next[l][i] = 1;        word[l++] =-1;    return L-1;        } void Init () {L = 0;    root = NewNode ();        } void Insert (char *buf, int id) {int len = strlen (BUF);        int now = root;  for (int i = 0; i < len; i++) {if (next[now][buf[i]-"] = = 1) next[now][buf[i]-"] =            NewNode ();        now = next[now][buf[i]-"];        } Word[now] = id;//record virus ID}//construct mismatch pointer void build () {queue<int> Q;        Fail[root] = root;            for (int i = 0; i < i++) {if (next[root][i] = = 1) next[root][i] = root;       else {         Fail[next[root][i]] = root;            Q.push (Next[root][i]); }} while (!            Q.empty ()) {int now = Q.front ();            Q.pop (); for (int i = 0; i < i++) {if (next[now][i] = = 1) next[now][i]= next[f                Ail[now]][i];                    else {Fail[next[now][i]] = next[fail[now]][i];                Q.push (Next[now][i]);        }}}} bool Mark[510];//mark[i] tag virus I has not appeared on the website int Query (char *buf, int n, int id) {        int len = strlen (BUF);        int now = root;        Memset (Mark, False, sizeof (mark));            BOOL flag = false;//tag on the site there is no virus found for (int i = 0; i < len; i++) {now = next[now][buf[i]-'];            int temp = now; while (temp! = root) {if (word[temp]! =-1)//non-initialized value {mark[word[   Temp]] = true;                 Flag = true;//Found virus} temp = fail[temp];        }} if (!flag)//did not find the virus return false;            else {printf ("Web%d:", id);//website has virus int t = 0; for (int i = 1; I <= n; i++)//output {if (Mark[i]) {if (T > 0                    ) printf ("");                    printf ("%d", I);                t++;            }} printf ("\ n");        return true; }    }};    Trie Ac;int Main () {int N, M;        while (scanf ("%d", &n)! = EOF) {ac.init ();            for (int i = 1; I <= N; i++) {scanf ("%s", str); Ac.        Insert (str, i);        } ac.build ();        int ans = 0;//records the site with the virus scanf ("%d", &m);            for (int i = 1; I <= M; i++) {scanf ("%s", str); if (AC.        Query (str, N, i))//The website has virus ans++; } printf ("TotaL:%d\n ", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 2896 virus attacks "AC automata basics"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.