hdu 4763 Theme Section (kmp next应用)
2015-02-21 16:56
621 查看
题意:
给出n个串,求每个串满足这样的形式EAEBE A和B表示当前串的两个不分,求E的最长长度,A,B可以为空。
题解:
利用next数组解决,我们只要next[len]表示最长公共前后缀,那么只要枚举中间位置net[i]如果满足等于next[len]那么久输出,不满足说明没有这样E那么答案就是0.注意这种情况ababa,最长公共前后缀重叠了,其实发现一个规律把重叠部分删去上下的部分也是满足相等的,对于这样的情况就要减去重叠部分。
给出n个串,求每个串满足这样的形式EAEBE A和B表示当前串的两个不分,求E的最长长度,A,B可以为空。
题解:
利用next数组解决,我们只要next[len]表示最长公共前后缀,那么只要枚举中间位置net[i]如果满足等于next[len]那么久输出,不满足说明没有这样E那么答案就是0.注意这种情况ababa,最长公共前后缀重叠了,其实发现一个规律把重叠部分删去上下的部分也是满足相等的,对于这样的情况就要减去重叠部分。
#include<iostream> #include<math.h> #include<stdio.h> #include<algorithm> #include<string.h> #include<vector> #include<map> #include<set> using namespace std; #define B(x) (1<<(x)) typedef long long ll; const int oo=0x3f3f3f3f; const ll OO=1LL<<61; const ll MOD=10007; const int maxn=1000000+5; char str[maxn]; int next[maxn]; void get_next(char s[],int len) { int i=0; next[i]=-1; int j=-1; while(i<len) { if(j==-1||s[i]==s[j]) { i++;j++; next[i]=j; } else j=next[j]; } } int main() { int n,s,e,len,ans; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) { scanf("%s",str); len=strlen(str); get_next(str,len); if(next[len]>=(len+1)/2) s=len-next[len]; else s=next[len]; ans=0; for(e=s;e<len;e++) { if(next[e]==s) { ans=s; break; } } printf("%d\n",ans); } } return 0; }
相关文章推荐
- hdu 4763 Theme Section (kmp, Next数组的应用)
- 2013长春网赛1005 hdu 4763 Theme Section(kmp应用)
- HDU 4763 Theme Section【KMP的next数组练习】
- HDU 4763 Theme Section ( KMP next函数应用 )
- hdu 4763 Theme Section(kmp)
- hdu 4763 Theme Section KMP
- hdu 4763 Theme Section kmp
- 【HDU】 4763 Theme Section (KMP)
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
- Theme Section - HDU 4763(KMP)
- Hdu 4763 Theme Section (kmp)
- HDU - 4763 Theme Section(拓展kmp)
- HDU 4763 Theme Section(KMP)
- HDU 4763 Theme Section(KMP)
- HDU 4763 Theme Section(KMP)
- hdu 4763 Theme Section(KMP水题)
- hdu 4763 Theme Section (扩展kmp)
- HDU 4763 Theme Section (kmp | exkmp)
- HDU 4763 Theme Section(KMP)
- HDU 4763 Theme Section(KMP)