Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

思路:题意是让你求能构成循环的最多次数,关键是利用next数组的构建,其是KMP算法的精髓。

对于代码中i-next[i]代表了字符串最小前缀且满足能不但的复制得到

原字符串;len%(i-next[i])==0时代表字符串刚刚是子串的整数倍;

若len%(i-next[i])==0匹配时每一次移动的距离i-next[i]是相等的,若不等则只有最后一次不等;

#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std; char str[1000010],pat[1000010];//pat为模式串,str为主串
int Next[1000010]; //Next[x]下标x表示匹配失败处字符下标
//模式串pat的前缀与x位置的后缀的最大匹配字符个数-1
void GetNext(char *pat)
{
int LenPat = strlen(pat);
int i = 0,j = -1;
Next[0] = -1;
while(i < LenPat)
{
if(j == -1 || pat[i] == pat[j])
{
i++,j++;
Next[i] = j;
}
else
j = Next[j];
} }
/*
int KMP()//返回模式串pat在str中第一次出现的位置
{
int LenStr = strlen(str);
int LenPat = strlen(pat);
//GetNext(pat);
int i = 0,j = 0;
int ans = 0;//计算模式串在主串匹配次数
while(i < LenStr)
{
if(j == -1 || str[i] == pat[j])
i++,j++;
else
j = Next[j];
if(j == LenPat)
{
//ans++; ans存放匹配次数,去掉return,最后返回ans
return i - LenPat + 1;
}
}
return -1;//没找到匹配位置
//return ans;//返回匹配次数。
} */
int main()
{
while(scanf("%s",str))
{
if(str[0]=='.')
break;
int len=strlen(str);
GetNext(str);
int k=len-Next[len];
int ans;
if(len%k==0)
ans=len/k;
else
ans=1;
printf("%d\n",ans);
}
return 0;
}

POJ2406A- Power Strings的更多相关文章

  1. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  2. POJ 2406 Power Strings

    F - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  3. poj 2406:Power Strings(KMP算法,next[]数组的理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30069   Accepted: 12553 D ...

  4. POJ 2406:Power Strings

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 41252   Accepted: 17152 D ...

  5. E - Power Strings,求最小周期串

    E - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  6. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  7. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  8. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  9. Power Strings

    Power Strings TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Totalsubmit: 1791   Accepted: 528 Descr ...

  10. poj 2406 Power Strings【最小循环节】

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36926   Accepted: 15254 D ...

随机推荐

  1. 在Map对象中获取属性,注意判断为空

    在Map对象中获取属性,注意判断为空 public static void main(String[] args) { Map map = new HashMap(); Integer i = (In ...

  2. Eclipseproject出现红叉

    通过import导入包的时候往往会出现红叉的情况.但又没有错:这样的情况能够这样解决 1.选中项目.按Alt键和Enter键 2.选Andriod,在右側的版本号信息里选择一个不同的版本号,如原来默认 ...

  3. DataGridView导出数据到Excel及单元格格式的改动

    在软件开发过程中,时常会遇到把一些数据信息从DataGridView中导出到Excel表格中的情况.假设写的多了就会发现挺简单的,我们最好还是来写一写,留作备用,毕竟有时候Ctrl+C和Ctrl+V还 ...

  4. 图像处理中的数学原理具体解释20——主成分变换(PCA)

    欢迎关注我的博客专栏"图像处理中的数学原理具体解释" 全文文件夹请见 图像处理中的数学原理具体解释(总纲) http://blog.csdn.net/baimafujinji/ar ...

  5. Linux下用ImageMagick玩图像魔术【转】

    本文转载自:http://www.linuxdiyf.com/linux/11680.html 不管你知不知道,现在是一个用ImageMagick的好机会,至少,如果你是一个Linux用户的话.这是一 ...

  6. identity in sql server 批量插入history

    https://stackoverflow.com/questions/1920558/what-is-the-difference-between-scope-identity-identity-i ...

  7. php中全局变量global和超全局变量$GLOBALS

    php中全局变量global和超全局变量$GLOBALS 1.global Global的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require ...

  8. [Oracle] Oracle终极解锁

    一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...

  9. JS——BOM操作(基本用法与实现:open()、close()、scrollTop等了解)

    (1)window.open() 定义和用法 open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口 语法 window.open(URL,name,specs,replace) [默认填 ...

  10. 44.Qt通过子类化qstyle实现自定义外观

    main.cpp #include <QtGui> #include "brozedialog.h" #include "bronzestyle.h" ...