描述

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).

输入

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.

输出

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

样例输入

abcd
aaaa
ababab
.

样例输出

1
4
3

提示

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

思路:

就是问一个字符串写成(a)^n的形式,求最大的n.

根据KMP的next函数的性质,已知字符串t第k个字符的next[k],那么d=k-next[k],如果k%d==0,那么t[1……k]最多可均匀的分成k/d份。也就是可以生成一个长度为d的重复度为k/d的字串。

#include<bits/stdc++.h>
using namespace std;
const int M=1e6+;
char t[M];
int next[M],tlen;
void getNext()
{
int i=,j=-;
next[]=-;
while(i<tlen)
{
if(j==-||t[i]==t[j])
next[++i]=++j;
else j=next[j];
}
}
int main()
{
while(scanf("%s",t)!=EOF,t[]!='.')
{
tlen=strlen(t);
getNext();
if(tlen%(tlen-next[tlen])==)
printf("%d\n",tlen/(tlen-next[tlen]));
else printf("1\n");
}
return ;
}

最新文章

  1. C# salt+hash 加密
  2. 第二章 --- 关于Javascript 设计模式 之 策略模式
  3. 我的js函数库(持续更新)
  4. HTTP,FTP,TCP,UDP及SOCKET
  5. HTTP 协议详解
  6. Midway-ModelProxy — 轻量级的接口配置建模框架
  7. jQuery 的 json 格式的处理问题
  8. 在CentOS 6.2上安装 MemcacheQ 最新版
  9. el表达式跟ognl表达式的区别(转)
  10. [013]函数重载--int*和void*的匹配优先级
  11. dd usb 启动盘制作 成功版本
  12. Nginx配置免费SSL证书StartSSL,解决Firefox不信任问题
  13. 【测试】解决loadrunner11无法运行负载测试
  14. CSS3知识点整理(四)----布局样式及其他
  15. 转:js闭包
  16. SQL优化 MySQL版 - 索引分类、创建方式、删除索引、查看索引、SQL性能问题
  17. 内存管理-MRC与ARC详解
  18. Python#常用的模块和简单用法
  19. HDFS-HA高可用
  20. WebApi参数传递实例

热门文章

  1. 网络I/O模型--07Netty基础
  2. web 应用响应乱码问题
  3. LeetCode 之二叉树中序遍历(使用栈实现)
  4. SqlServer查询存储过程中包含指定的内容
  5. 如何在ScrollView滑动的瞬间禁用拖拽手势
  6. Difference Between Arraylist And Vector : Core Java Interview Collection Question
  7. redis几种数据类型以及使用场景
  8. 绕过CDN查找网站真实IP方法收集
  9. jenkins + k8s + lnp
  10. ZT CSDN 如何以最快的速度计算出一个二进制数中1的个数? [