Sequence automaton [template]

Nanchang Network Invitational tournament title Subsequence M 

Meaning of the questions:

You give a string S, is less than the length 1e5,

N times then asked, n is less than 1e5,

That each input string T, Q T S is the sub-sequence is not. Length is less than 1000 T

input:

abcdefg
3
abc
adg
cba

output:

YES
YES
NO

Ideas:

Automaton first sequence is actually a pre-out array, Next [i] [j] denotes a position behind the first character position i j where, the pretreatment Next array complicated than log (N * 26)
per complexity is log interrogation times (M) a (M is the length of the string each time the interrogation).

 

AC Code:

#include<bits/stdc++.h>

using namespace std;
 
const int maxn=1e5+7;
char s[maxn];
char t[maxn];
 
int Next[maxn][27];

void init(int n){
    for(int i=n-1;i>=0;--i){
        for(int j=0;j<26;++j)
            Next[i][j]=Next[i+1][j];
        Next[i][s[i+1]-'a']=i+1;
    }
}
 
int main(){
    scanf("%s",s+1);
    int n=strlen(s+1);
    init(n);
    int q;
    scanf("%d",&q);
    while(q--){
        scanf("%s",t);
        int l=strlen(t);
        int f=1;
        int now=0;
        for(int i=0;i<l;++i){
            now=Next[now][t[i]-'a'];
            if(now==0){
                f=0;
                break;
            }
        }
        printf("%s\n",(f?"YES":"NO"));
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/pengge666/p/11600338.html