您的位置:首页 > 其它

hoj1952 power string

2012-06-08 15:42 267 查看
/*This Code is Submitted by billforum for Problem 1952 at 2012-06-08 15:39:09*/
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <exception>
#include <cstring>
#include <stdlib.h>
using namespace std;
const int Len=1000000;
int next[Len+5];
void getNext(int next[],char str[])
{
int len=strlen(str);
int i=0,j=-1;
while(i<len)
{
if((j==-1)||(str[i]==str[j]))
{
j++;
i++;
next[i]=j;
}
else j=next[j];
}

return;
}
void print(int next[],int len)
{
for(int i=0;i<len;i++)
printf("%d ",next[i]);
}
int cmp(char s1[],char s2[])
{
int i=0,j=0;//i->s1,j->s2
int len1=strlen(s1),len2=strlen(s2);
int times=0;
while(i<len1)
{
if((j==-1)||(s1[i]==s2[j]))
{
i++;
j++;
if(j>=len2)
{
j=0;
times++;
}
}
else j=next[j];
}
return times;
}
int main(int argc, char **argv)
{

char str1[Len+5];
memset(next,0,sizeof(next));
next[0]=-1;
while(scanf("%s",str1)==1)
{
if(strcmp(str1,".")==0) break;
int len1=strlen(str1);

str1[len1]='.';
str1[len1+1]='\0';

getNext(next,str1);
len1=strlen(str1);
//print(next,len1);
//cout<<endl;
if((len1-1)%(len1-1-next[len1-1])==0)
printf("%d\n",(len1-1)/(len1-1-next[len1-1]));
else printf("1\n");

}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: