HDU 3065 virus attack persists (AC automaton)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3065


Problem description Small T thank you very much for helping to solve his last problem. However, the virus continues to invade. Under the tireless efforts of little T, he discovered the "root of all evils" in the internet. This is a huge virus website, he has a lot of virus, but this site contains a very strange virus, the signatures of these viruses are very short, and only contain "English characters". Of course small T good want to people from harm, but little t never fight unprepared war. The enemy, Baizhanbudai, small T first to do is to know the virus site features: How many different viruses, each virus appears how many times. Can we help him again?
Input first line, an integer N (1<=n<=1000) that represents the number of virus signatures.
Next n rows, each line represents a virus signature, the signature string is between 1-50 and contains only the English uppercase characters. Any of the two virus signatures will not be identical.
After this line, said the "root of All Evil" website source code, the source string length within 2000000. Characters in the string are ASCII-visible characters (not including carriage returns).

Output is one per line in the following format, outputting the number of occurrences per virus. No output is required for viruses that do not appear.
Virus signature: Number of occurrences
A colon is followed by a space that is output in the order in which the virus signature is entered.

Sample Input
3aabbccooxxcc%daaaoen .... END

Sample Output
Aa:2cc:1Hinthit: All cases not mentioned in the title description should be considered. For example, two virus signatures may have feature segments that contain or overlap each other. The counting strategy can also be inferred from sample to some extent.

Source2009 multi-university Training Contest 16-host by NIT

The code is as follows:

#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include < Queue> #include <string>using namespace Std;char ss[1017][57];int ans[1017];struct trie{int next[500010][128]    , fail[500010],end[500010];    int root,l;        int NewNode () {for (int i = 0; i <; i++) next[l][i] = 1;        end[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]];    } End[now] = ID;        } 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[fail[now]][i]                ;                    else {fail[next[now][i]]=next[fail[now]][i];                Q.push (Next[now][i]);        }}} void query (char buf[], int n) {memset (ans,0,sizeof (ans));        int len = strlen (BUF);        int now = root;            for (int i = 0; i < len; i++) {now = Next[now][buf[i]];            int temp = now;                while (temp! = root) {//res + = end[temp];                printf ("temp:%d end[temp]:%d\n", temp,end[temp]);                if (end[temp]! =-1) {ans[end[temp]]++;            }//end[temp] = 0;    temp = fail[temp]; }}}};char buf[2000010];    Trie Ac;int Main () {int t, n;        while (~SCANF ("%d", &n)) {ac.init ();            for (int i = 0; i < n; i++) {scanf ("%s", Ss[i]);        Ac.insert (Ss[i],i);        } ac.build ();//fail scanf ("%s", buf);        Ac.query (buf, N);            for (int i = 0; i < n; i++) {if (Ans[i]) {printf ("%s:%d\n", ss[i],ans[i]); }}} return 0;}


HDU 3065 virus attack persists (AC automaton)

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.