[2020多校联考]进化

Solution

可以发现合法的序列一定由若干个 (l),每对相邻的 (l) 中间至少夹一个 (t) 组成。而对于 (Lao),其第一个字符必须为 (l)。那么就容易建立出自动机。

对于 (Lao),从 (1) 号点开始转移;对于 (Tui),从 (3) 号点开始转移。如果没有转移边就一定不为合法的序列。还要特判一下字串是否包含 (l)

#include<stdio.h>
#include<string.h>
#define N 10000007

int T,to[5][30];
char c[N];
int main(){
    freopen("laotui.in","r",stdin);
    freopen("laotui.out","w",stdout);
    to[1]['l'-'a']=2,to[2]['t'-'a']=3;
    to[3]['t'-'a']=3,to[4]['t'-'a']=3,to[3]['l'-'a']=4;
    scanf("%d",&T);
    while(T--){
        scanf("%s",c+1);
        int p1=1,p2=3,len=strlen(c+1);
        bool tag=0;
        for(int i=1;i<=len;i++){
            tag|=(c[i]=='l');
            int v=c[i]-'a';
            p1=to[p1][v],p2=to[p2][v];
        }
        printf("%d %d
",tag&(p1!=0),tag&(p2!=0));
    }
}

Tips

要读入一亿个字符,注意卡常。

原文地址:https://www.cnblogs.com/wwlwQWQ/p/14043969.html