HDU

来源:互联网 发布:数据备份 编辑:程序博客网 时间:2024/06/07 05:20

https://vjudge.net/contest/189927#problem/J

题意:找到EAEBE类型的最长的E的串,也就是说前缀和后缀相同,然后这个串在中间也出现过,其中A和B的长度可以为0。

思路:根据nex数组,我们可以找到最长的在前后缀的E,那么中间那个E怎么去判断的呢,暴力去找,,就好了,数据水的一p,瞎写的都能过 - -,等会再改改把

上代码把:

#include <stdio.h>#include <string.h>#include <iostream>using namespace std;const int maxn = 1e6+10;char ch[maxn],te[maxn];int nex[maxn],snex[maxn];void pre_kmp(char str[]){memset(nex , 0 , sizeof(nex));int len  = strlen(str) , i = 0 , k = -1;nex[0] = -1;while( i < len ){if( k == -1 || str[i] == str[k] ){i++,k++;nex[i] = k;}else {k = nex[k];}}}void son_kmp(char str[]){memset(snex,0,sizeof(snex)) ;int len = strlen(str),i = 0 , k = -1;snex[0] = -1 ;while( i < len ){if( k == -1 || str[i] == str[k] ){i++,k++;snex[i] = k;}else k = snex[k];}}int kmp(int s,int e ,char str[],char Str[]){int i = s,k = 0 ;int len = strlen(str);while(i < e){if( k == -1 || Str[i] == str[k] ){i++;k++;}else{k = snex[k];}if( k == len ){return 1;}}return 0;}int main(){int t;scanf("%d",&t);while(t--){scanf("%s",ch);pre_kmp(ch);int len = strlen(ch);int p = len ;if( p == 3){int cnt = 0 ;for(int i = 1 ; i < p ; i++){if(ch[i] == ch[i-1]){cnt++;}}if(cnt == 2){puts("1");}else {puts("0");}continue;}while(p){memset(te,0,sizeof(te));for(int i = 0 ; i < nex[p] ; i++){te[i] = ch[i];}son_kmp(te);if(kmp(nex[p]  , p - nex[p]  ,te,ch)){break;}else {p = nex[p];}}printf("%d\n",nex[p]);}return 0 ; }


原创粉丝点击