[HDU] virus attack continues (AC automation + map), hdu Automation

Source: Internet
Author: User

[HDU] virus attack continues (AC automation + map), hdu Automation

WAF started from the beginning and later found that there were more than one set of inputs for this question. After changing it to multiple sets of inputs, it would be over.

Map each string to its corresponding node using map.

11909467 2014-10-19 11:54:00 Accepted 3065 234 MS 16912 K 2754 B G ++ KinderRiven

# Include <queue> # include <map> # include <cstdio> # include <string> # include <vector> # include <cstring> # include <algorithm> using namespace std; const int maxn = 55555; const int max_size = 130; char ss [1111] [55]; map <string, int> vis; struct Trie {int next [maxn] [max_size]; int fail [maxn]; int val [maxn]; int sz; int root; int num [maxn]; int index (char e) {return e-31;} void init () {sz = 0; root = newnode ();} int Newnode () {val [sz] = 0; num [sz] = 0; memset (next [sz],-1, sizeof (next [sz]); sz ++; return sz-1;} void insert (char * str) {int L = strlen (str); int u = root; for (int I = 0; I <L; I ++) {int e = index (str [I]); if (next [u] [e] =-1) next [u] [e] = newnode (); u = next [u] [e];} val [u] = 1; // The word vis [string (str)] = u;} void build () {// creates a mismatched edge fail [root] = root; queue <int> q; for (int I = 0; I <max_size; 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 <max_size; I ++) {if (next [now] [I] =-1) next [now] [I] = next [fail [now] [I]; else {// This point is the end of a word fail [next [now] [I] = next [fail [now] [I]; q. push (next [now] [I]) ;}}} void count (char * str) {int now = root; int L = strlen (str ); for (int I = 0; I <L; I ++) {int e = index (str [I]); now = next [now] [e]; int temp = now; while (temp! = Root) {if (val [temp]) {// if this node is the end of a word num [temp] ++; // printf ("% d \ n ", temp) ;}temp = fail [temp] ;}}}; Trie ac; char _ str [2222222]; int main () {int n; while (scanf ("% d", & n )! = EOF) {ac. init (); vis. clear (); getchar (); for (int I = 0; I <n; I ++) {gets (ss [I]); ac. insert (ss [I]);} ac. build (); gets (_ str); ac. count (_ str); for (int I = 0; I <n; I ++) {int t = vis [string (ss [I])]; if (ac. num [t]) {printf ("% s: % d \ n", ss [I], ac. num [t]) ;}} return 0 ;}


Hangdian acm 3065

[Question analysis]
The question of the AC automatic machine. If you do not understand, you can search for the article about the AC automatic machine. Needless to say, this question is about the template of the AC automatic machine. Considering that the reader has mastered the AC automatic machine, you can solve this question to consolidate it. Note that there are multiple case questions.

[AC source code] Hope to adopt it ~ If you have any questions, please ask again ~

# Include <iostream>
# Include <algorithm>
# Include <stdio. h>
# Include <math. h>
# Include <string. h>
# Include <vector>
# Include <stack>
# Include <queue>
# Deprecision MAX 100005
# Define inf 1499999999
Using namespace std;
Const int len = 28; // only 26 letters at most
Struct Tril {
Tril * fail;
Tril * son [len];
Int count;
Tril (){
Fail = NULL;
Count = 0;
Memset (son, NULL, sizeof (son ));
}
};
Char str [2000050];
Char word [2, 1005] [55];
Int ans [1005];
Void creat (char * word, Tril * root, int num) //// // Insert the tril tree
{
Tril * p = root;
Int lenth = strlen (word );
For (int I = 0; I <lenth; I ++)
{
Int index = word [I]-'A ';
If (p-> son [index] = NULL)
P-> son [index] = new Tril ();
P = p-> son [index];
}
P-> count = num;
}
Void creat_ac_automation (Tril * root) // find the failed Node
{
Queue <Tril *> q;
Root-> fail = NULL;
Q. push (root );
While (! Q. empty ()){
Tril * p_cur = q. front ();
Q. pop ();
For (int I = 0; I <26; ++ I ){
If (p_cur-> son [I]! = NULL ){
If (p_cur = root ){
P_cur-> son [I]-> fail = root;
} Else {
Tril * temp = p_cur-> fail;
While (temp! = NULL) {... the remaining full text>


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.