| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
网上各种AC代码各种错,只能说明数据弱,举例说明网上各种AC代码各种错,只能说明数据弱,举例说明。希望能够加强数据
Input
2 8
ABCDEFAB
ABCDEABC
2 8
ABCDEFAB
AAAABAAA
Output
16
12
以下两个AC代码的来源的AC代码:
第一个代码来源:http://hi.baidu.com/%C1%D9%CA%B1%B1%B8%D3%C3%D5%CB%BA%C5/blog/item/4a047c6e417265f4421694e2.html(临时备用账号的空间),这个代码的输出如下:
12
12
第二个正确,第一个就过不了。
第二代码来源:http://hi.baidu.com/liwang112358/blog/item/828f99f19a04195a342acc44.html(LiWang112358的空间),我发现网上和这个代码的思路一样的代码非常多,这个代码过不了第二个样例,不过还能过第一个样例,输出如下:
16
16
正解思路:见博文:http://blog.sina.com.cn/s/blog_69c3f0410100tyjl.html
正解代码如下(zzuli):
#include<stdio.h>
#include<string.h>
char s[10010][80];
int next[10010];
int main()
{
int i,j,x,y,r,c,f[80];
char a[80];
scanf("%d%d",&r,&c);
for(i=0;i<c;i++)f[i]=0;
for(i=0;i<r;i++){
scanf("%s",s[i]);
strcpy(a,s[i]);
for(j=c-1;j>0;j--){
a[j]=0;
for(x=0,y=0;s[i][y];x++,y++){
if(!a[x])x=0;
if(a[x]!=s[i][y])break;
}
if(!s[i][y])f[j]++;
}
}
for(i=0;i<c;i++)
if(f[i]==r)break;
x=i;
for(i=0;i<r;i++)s[i][x]=0;
next[0]=-1;
for(i=1,j=-1;i<r;i++){
while(j!=-1&&strcmp(s[j+1],s[i]))j=next[j];
if(!strcmp(s[j+1],s[i]))j++;
next[i]=j;
}
printf("%d\n",(r-1-next[r-1])*x);
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator