Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

高手帮小弟看一下吧。。re了一晚了。。KMP觉得没什么问题啊。。55

Posted by chenglijing at 2008-08-03 06:12:26 on Problem 2406
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void  kmp(char *a,int *next)
{
   int i=1,j=0;
   next[1]=0;
   while(i<=next[0])
   {
	   if(j==0||a[i]==a[j])
	   {
		   ++i;++j;
	       next[i]=j;
	   }
	   else
		   j=next[j];
   }
}
int main()
{

	char cha[100];
	int next[100];
    int i,num,k;

	while(1)
	{
	   	i=1;num=0; k=0;
	  scanf("%c",&cha[i]);
      if(cha[i]=='.')
		  break;
	  while(cha[i]!='\n')
	  {
		  scanf("%c",&cha[++i]);
      }

	  i=i-1;
      next[0]=i;
	  kmp(cha,next);
	  k=i-next[i];
      
	  if(i%k!=0||cha[next[i]]!=cha[i])
		  printf("1\n");
	  else
		 printf("%d\n",i/k);
	}
	return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator