Description
The scientists ' adventures on the Samuel Planet are still going on. Fortunately, near the South pole of the Samuel Planet, adventure droids have discovered a huge glacial lake! The robot collects a lot of RNA fragments from this glacial lake and returns it to its experimental base. After several days and nights of research, scientists have found that many of these RNA fragments are unknown viruses! Each RNA fragment is a sequence consisting of a, C, T, and G. Scientists have also summed up the "viral template fragments" on the planet Samuel. A template fragment is the sequence of a, C, T, G plus a wildcard * and? To express. where * means can match on 0 or any number of characters, and? Matches any one of the letters. If an RNA fragment can match a "viral template fragment," The RNA fragment is an unknown virus. For example, suppose "virus template fragment" is A*g? C. RNA fragments: AGTC,AGTGTC are unknown viruses, while RNA fragment AGTGC is not a virus. Because the robot collects these RNA fragments to remove the other parts of the virus has a very high research value. So scientists want to be able to identify which RNA fragments are not viruses and send RNA fragments that are not viruses back to the space station for further research. The scientist gave the task to Xiao Lian. Now you are asked to compile a program for the gadget to count which RNA fragments are not viruses.
Input
The first line has a string that consists of a, C, T, G, *,? Composition Represents a "viral template fragment". The "viral template fragment" length does not exceed 1000. The second line has an integer N (0<n<500) that represents the number of RNA fragments that the robot collects. The next n rows, each line has a string consisting of a, C, T, G, which represents an RNA fragment. The length of each RNA fragment does not exceed 500. Note: "Viral template fragments" and RNA fragments have a minimum length of 1.
Output
only one row of output is an integer m, that is, the number of RNA fragments that are not viruses.
The data range is small, so N3 can live.
The template is obviously a simple regular expression, so an NFA can then be N2 to determine whether a string matches, the total complexity or O (N3)
#include <cstdio>#include<vector>#include<cstring>intn,ans=0;Chars[ +];intcid[ the],p=1; std::vector<int>nx[ +][5];BOOLd[ +],d1[ +];voidAdde (intAintBintc) {Nx[a][b].push_back (c);}intMain () { for(intI=0;i<5; i++) cid["atgc#"[I]] =i; scanf ("%s", s+1); intL=strlen (s+1), w=1, w2=0; s[0]=s[++l]='#'; for(intI=0; i<=l;i++){ intc=s[i],u=++p; if(c=='*'){ if(!W2) { for(c=0;c<5; C + +) Adde (w,c,u), Adde (U,c,u); W2=w;w=u; } }Else if(c=='?'){ for(c=0;c<5; C + +) Adde (w,c,u); if(W2) { for(c=0;c<5; C + +) Adde (w2,c,u); W2=0; } W=u; }Else{C=Cid[c]; Adde (W,c,u); if(W2) Adde (w2,c,u), w2=0; W=u; }} scanf ("%d",&N); while(n--) {scanf ("%s", s+1); intLen=strlen (s+1); s[0]=s[++len]='#'; memset (D1,0, +); d1[1]=1; for(intI=0; i<=len;i++) {memcpy (d,d1,p+1); memset (D1,0, p+1); intC=Cid[s[i]]; for(intj=1; j<=p;j++)if(D[j]) { for(intk=0; K<nx[j][c].size (); k++) d1[nx[j][c][k]]=1; } } if(!d1[p]) + +ans; } printf ("%d", ans); return 0;}
bzoj1966: [Ahoi2005]virus Virus detection