标签:des style blog http java color
3 2 AB DCB DACB 3 ABC CDE GHI ABCCDEFIHG 4 ABB ACDEE BBB FEEE A[2B]CD[4E]F
0 3 2HintIn the second case in the sample input, the reverse of the program is ‘GHIFEDCCBA’, and ‘GHI’ is a substring of the reverse, so the program is infected by virus ‘GHI’.
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define N 5555555 using namespace std; struct node { node *fail; node *next[26]; int cnt; node () { fail=0; cnt=0; memset(next,0,sizeof(next)); } }; node *root=NULL; void insert(char *str) { int i=0,index; node *p=root; while(str[i]!='\0') { index=str[i]-'A'; if(p->next[index]==NULL) p->next[index]=new node; p=p->next[index]; i++; } p->cnt++; } void build() { int i; root->fail=NULL; queue<node*> q; q.push(root); while(!q.empty()) { node *tmp=q.front(); q.pop(); node *p=NULL; for(i=0;i<26;i++) { if(tmp->next[i]) { if(tmp==root) tmp->next[i]->fail=root; else { p=tmp->fail; while(p) { if(p->next[i]) { tmp->next[i]->fail=p->next[i]; break; } p=p->fail; } if(p==NULL) tmp->next[i]->fail=root; } q.push(tmp->next[i]); } } } } int query(char *str) { int i=0,cnt=0,index; node *p=root; while(str[i]!='\0') { //cout<<i<<endl; index=str[i]-'A'; while(p->next[index]==NULL&&p!=root) p=p->fail; p=p->next[index]; p=(p==NULL)?root:p; node *tmp=p; while(tmp!=root&&tmp->cnt!=-1) { cnt+=tmp->cnt; tmp->cnt=-1; tmp=tmp->fail; } i++; } return cnt; } char s1[N],s2[N]; void input() { int p=0; char c; getchar(); while(c=getchar(),c!='\n') { if(c!='[') s1[p++]=c; else { int x; scanf("%d",&x); c=getchar(); while(x--) s1[p++]=c; getchar(); } } for(int q=0;p>0;q++) s2[q]=s1[--p]; //cout<<s1<<" "<<s2<<endl; } void del(node *root) { for(int i=0;i<26;i++) if(root->next[i]) del(root->next[i]); delete root; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int t,n; scanf("%d",&t); char str[1005]; while(t--) { root=new node; memset(s1,'\0',sizeof(s1)); memset(s2,'\0',sizeof(s2)); scanf("%d",&n); while(n--) scanf("%s",str),insert(str); build(); input(); printf("%d\n",query(s1)+query(s2)); del(root); } return 0; }
HDU 3695 Computer Virus on Planet Pandora,布布扣,bubuko.com
HDU 3695 Computer Virus on Planet Pandora
标签:des style blog http java color
原文地址:http://blog.csdn.net/hqu_fritz/article/details/36448859