POJ-2406 Power Strings

版权声明:请联系[email protected] https://blog.csdn.net/wanglin007/article/details/84404458

POJ-2406 Power Strings
题解:
其实就是用next把模式串标记出来,然后用如果strlen(模式串)%(strlen(模式串)-next[strlen(模式串)])==0的话,说明这个模式串可以被几个字串组合而成,而个数等于strlen(模式串)/(strlen(模式串)-next[strlen(模式串)])

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int next[1000005];
void Next(char T[])
{
 int i=0,j=-1,l=strlen(T);
 next[0]=-1;
 while(i<l)
 {
  if(j==-1||T[j]==T[i])next[++i]=++j;
  else j=next[j];
 }
}
int main()
{
 char T[1000005];
 while(scanf("%s",&T)!=EOF)
 {
  if(T[0]=='.')break;  
  int l=strlen(T);
  Next(T);
  if(l%(l-next[l])==0)printf("%d\n",l/(l-next[l]));
  else printf("1\n");
 }
 return 0;
}

猜你喜欢

转载自blog.csdn.net/wanglin007/article/details/84404458
今日推荐