HDU - 4763 找出特殊规律的串

Posted The Azure Arbitrator

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 4763 找出特殊规律的串相关的知识,希望对你有一定的参考价值。

在一个Text串中找出Pattern串E以满足EAEBE,A B可以是任意串
看一眼觉得woc好难,后来看一下样例AB为空也可以
找观察一下E既是前缀也是后缀,很容易联想到next
最优情况EEE,那就是3 × next[j] == j
A,B任取,那只要进一步保证 3 × next[j] <= j 即可
听说有dalao用SA数组来解...
AC代码

/*H E A D*/
int nxt[maxn];
char P[maxn];
void buildNext(){
    int m=strlen(P+1);
    int j=0;
    nxt[1]=0;
    rep(i,2,m){
        while(j>0&&P[i]!=P[j+1]) j=nxt[j];
        if(P[i]==P[j+1])j++;
        nxt[i]=j;
    }
}
int main(){
    int t=read();
    while(t--){
        s1(P);buildNext();
        int m=strlen(P+1);
        int tmp=nxt[m];
        while(3*tmp>m&&tmp>0){
            tmp=nxt[tmp];
        }
        println(tmp);
    }
    return 0;
}

以上是关于HDU - 4763 找出特殊规律的串的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4763 Theme Section(KMP+枚举公共前后缀)

hdu-4763(kmp+拓展kmp)

hdu 4763 Theme Section

HDU 4763 Theme Section

hdu4763 Theme Section

hdu4763Theme Section