HDOJ 3065 virus attacks continue in "AC Automata Fundamentals" "Output number of occurrences per pattern string"

Source: Internet
Author: User

The virus continues to attack

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


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.


Idea: Directly in the query process with an array to record the number of occurrences of the current string is OK.

#include <cstdio> #include <cstring> #include <queue> #define MAXN 2000000+10using namespace Std;char    Str[maxn];char Dd[1001][50];int rec[1001];//record number of occurrences struct trie{int next[50010][130], fail[50010], id[50010];    int root, L;        int NewNode () {for (int i = 0; i <; i++) next[l][i] = 1;    id[l++] = -1;//record ID 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]];    } Id[now] = ID;        } void Build () {queue<int> Q;            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) {memset (rec, 0, sizeof (REC));//The number of occurrences is 0 int len = s        Trlen (str);        int now = root;            for (int i = 0; i < len; i++) {now = Next[now][buf[i]];            int temp = now;                while (temp! = root) {if (id[temp]! =-1) rec[id[temp]]++;            temp = fail[temp]; }        }    }};    Trie Ac;int Main () {int N;        while (scanf ("%d", &n)! = EOF) {ac.init (); for (int i = 1; I <= N; i+ +) {scanf ("%s", Dd[i]); Ac.        Insert (Dd[i], i);        } ac.build ();        scanf ("%s", str); Ac.        Query (str);        for (int i = 1; I <= N; i++) {if (Rec[i]) printf ("%s:%d\n", Dd[i], rec[i]); }} return 0;}


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

HDOJ 3065 virus attacks continue in "AC Automata Fundamentals" "Output number of occurrences per pattern string"

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.