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 |
就求个next[],我怎么会WA了这么多次?好心人给看看吧,都一下午了#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator