HDU 2896-Virus attack

Source: Internet
Author: User

Title: Give you some of the characteristics of the virus, and then give you some website code, statistics on each site of the virus and the number of sites attacked.

Analysis: Strings, ac automata. Large data size, using AC automata. Specific reference to this site:ac automata summary .

Description: The solution fial was merged into query at the beginning, resulting in multiple TLE, ... I'm so embarrassed ...

#include <stdio.h> #include <stdlib.h> #include <string.h>int cmp (const void* A, const void* b) {return * ((int *) a)-* ((int *) b);}    /* AC_DFA define */#define NODESIZE 60006#define dictsize 128typedef struct node1{int flag;    Node1* fail; Node1* next[dictsize];} Tnode;tnode dict[nodesize];tnode* Q[nodesize];int id[dictsize];        int Answer[10001];class ac_dfa{private:int size;    tnode* Root;        Public:ac_dfa () {Makeid (); init ();}        void Makeid () {for (int i = 0; i < dictsize; + + i) id[i] = i;            } void Init () {memset (dict, 0, sizeof (dict));        Root=null;size=0;root=newnode ();            } tnode* NewNode () {dict[size].fail = root;        return &dict[size + +];            } void Insert (char* word, int id) {tnode* now = root;           for (int i = 0; word[i]; + + i) {if (!now->next[id[word[i]])         Now->next[id[word[i]] = NewNode ();            now = now->next[id[word[i]]];        }now->flag = ID;            } void Setfail () {q[0] = root; root->fail = NULL;                for (int move = 0,save = 1; move < save; + + move) {tnode* now = Q[move]; for (int i = 0; i <; + + i) if (Now->next[i]) {tnode* p = Now->fa                        Il                        while (P &&!p->next[i]) p = p->fail;                        Now->next[i]->fail = p?p->next[i]:root;                    Q[save + +] = now->next[i]; }else Now->next[i] = now==root?root:now->fail->next[i];//build  trie diagram}} int query (CH            ar* line, int id) {int count = 0;            tnode* now = root;                for (int i = 0; line[i]; + + i) {now = now->next[id[line[i]]]; for (tnode* p = now; p;p = p->fail) if (p->flag) Answer[count + +] = p->flag;                } if (count) {qsort (answer, Count, sizeof (int), CMP);                printf ("Web%d:%d", id,answer[0]); for (int i = 1; i < count; + + i) if (answer[i]! = answer[i-1]) printf ("%d"                , Answer[i]);            printf ("\ n");        } return count > 0;    }};/* Ac_dfa End */char Word[205];char line[10005];int main () {int t,n,m,sum;    AC_DFA AC;    scanf ("%d", &n);        for (int i = 1; I <= N; + + i) {scanf ("%s", Word);    Ac.insert (Word, i);    } ac.setfail ();    scanf ("%d", &m);    Sum = 0;        for (int i = 1; I <= M; + + i) {scanf ("%s", &line);    Sum + = Ac.query (line, i);    } printf ("Total:%d\n", Sum);    System ("pause"); return 0;}


HDU 2896-Virus attack

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.