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

就求个next[],我怎么会WA了这么多次?好心人给看看吧,都一下午了

Posted by 123454321 at 2007-08-04 15:57:35 on Problem 2406
#include <stdio.h>
#include <string.h>
#include <iostream.h>
const int MAX = 1000000+10;

char line[MAX];
int n, next[MAX],ans;

int main() {
   
   while (gets(line) && strcmp(line, ".")) {
      n = strlen(line);
      memmove(line + 1, line, n);
      if(n==2&&line[1]!=line[2])
	  {
		  ans=1;
		  cout<<ans<<endl;
		  continue;
	  }

      int j, k;
//其实本题用的是next[]
	j=1;next[1]=0; k=0;
	while(j<n)
	{
		if(k==0 || line[j]==line[k]) 
		{
			j++;k++;
			next[j]=k;
			//if(Pj != Pk) next[j]=k;
			//else nextval[j]=next[k];
		}
		else k=next[k];
	}
      int sub = n - next[n];
      if (n % sub == 0) ans = n / sub;
      else ans = 1; 
      
      printf("%d\n", ans);
	  
   }
   return 0;         
} 

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