月月查华华的手机(序列自动机)

月月查华华的手机(序列自动机)

 

AC_Code

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 1e6+10;
 5 const int inf=0x3f3f3f3f;
 6 #define rep(i,first,last) for(int i=first;i<=last;i++)
 7 #define dep(i,first,last) for(int i=first;i>=last;i--)
 8 int nxt[maxn][30];
 9 int now[30];
10 char s[maxn];
11 
12 void init(){
13     memset(now,-1,sizeof(now));
14     int len=strlen(s);
15     dep(i,len-1,0){
16         rep(j,0,25){
17             nxt[i][j]=now[j];
18         }
19         now[s[i]-'a']=i;
20     }
21 }
22 
23 char ss[maxn];
24 int main()
25 {
26     scanf("%s",s);
27     int n;
28     scanf("%d",&n);
29     init();
30     while( n-- ){
31         scanf("%s",ss);
32         int loc=now[ss[0]-'a'];
33         if( !~loc ) printf("No
");//if(loc==-1)
34         else{
35             bool flag=true;
36             int len=strlen(ss);
37             rep(i,1,len-1){
38                 loc=nxt[loc][ss[i]-'a'];
39                 if( !~loc ){
40                     flag=false;
41                     break;
42                 }
43             }
44             if( flag ) printf("Yes
");
45             else printf("No
");
46         }
47     }
48     return 0;
49 }
原文地址:https://www.cnblogs.com/wsy107316/p/12317496.html