标签:des style http color java os io strong
5 xy abc aaa aaaaba aaxoaaaaa
0 0 1 1 2
题意:给定一个字符串,寻找3段字符串使它们完全相等(不能重叠),一个必须从头开始,一个必须以末尾结束。
#include"stdio.h"
#include"string.h"
#define N 1000005
char str[N],s[N];
int next[N],n;
void getnext(int m)
{
int i=0,j=-1;
next[0]=-1;
while(i<m)
{
if(j==-1||s[i]==s[j])
{
i++;
j++;
if(s[i]!=s[j])
next[i]=j;
else
next[i]=next[j];
}
else
j=next[j];
}
}
int kmp(int m) //模式串长度
{
int i=0,j=0;
getnext(m);
while(i<n)
{
if(j==-1||str[i]==s[j])
{
i++;
j++;
}
else
j=next[j];
if(j==m)
return 1;
}
return 0;
}
void work(char*s,int m)
{
int i,j;
while(m) //查找第一个字符串和最后一个字符串最大的匹配
{
i=0;
for(j=n-m;j<n;j++)
{
if(s[i++]!=str[j])
break;
}
if(j==n)
{
if(kmp(m))
break;
}
m--;
}
printf("%d\n",m);
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
n=strlen(str);
for(i=0;i<n/3;i++)
s[i]=str[i];
s[i]='\0'; //第一个字符串长度为I
work(s,i);
}
return 0;
}
hdu 4763 Theme Section (简单KMP),布布扣,bubuko.com
hdu 4763 Theme Section (简单KMP)
标签:des style http color java os io strong
原文地址:http://blog.csdn.net/u011721440/article/details/38444929