Power Strings

Time Limit: 3000MS Memory Limit: 65536K

Description

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.


就是一个寻找字符串的循环节,用KMP的思想,其实很简单就是len/(len-next[n])。(当然是在没有余数的情况下,不然就是1)


#include<stdio.h>
#include<cstring>
#include<set>
using namespace std;
const int maxn = 1e6+100;
char s[maxn];
int next[maxn]; void cal_next()
{
int k;
next[0] = k = -1;
int len = strlen(s);
for(int i=1;i<len;i++)
{
while(k>-1 && s[i] != s[k+1])
k = next[k];
if(s[i] == s[k+1])
k++;
next[i] = k;
}
} int main()
{
while(scanf("%s",s))
{
if(s[0] == '.' && s[1] == 0)
break;
cal_next();
int len = strlen(s);
if(len%(len - next[len-1] - 1) == 0)
printf("%d\n",len/(len - next[len-1] - 1));
else
printf("1\n");
memset(s,0,sizeof(s));
}
return 0;
}

最新文章

  1. fir.im Weekly - 从 iOS 10 SDK 新特性说起
  2. Xcode 自定义代码段
  3. 自定义流程gooflow.08 demo在线演示
  4. 关于uploadify 没有显示进度条!!!!
  5. Watchcow
  6. Kattis - Aaah!
  7. getReadableDatabase 和 getWritableDatabase的区别
  8. MongoDB、Hbase、Redis等NoSQL优劣势、应用场景
  9. QThread的一些使用心得
  10. python网页爬虫开发之三
  11. 【JVM】jvm虚拟机参数解析
  12. c#调用dll接口传递utf-8字串方法
  13. 整理Lua和Unity和Lua交互文章链接
  14. 2016NOI冬令营day4
  15. grafana查询中的变量templating
  16. Ubuntu下的网络服务
  17. 远程连接mysql root账号报错:2003-can&#39;t connect to MYSQL serve
  18. E470 外放没声音问题解决
  19. 利用js动态创建&lt;style&gt;
  20. php连接数据库(一)

热门文章

  1. Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值
  2. Java微信公众平台开发(九)--微信自定义菜单的创建实现
  3. AJPFX关于延迟加载的单例模式的安全问题解决
  4. 搭建高可用mongodb集群(一)——mongodb配置主从模式
  5. 初探ant design pro
  6. Sublime Text 3安装AngularJS插件
  7. Spring MVC系列[2]——参数传递及重定向
  8. jni ndk 入门
  9. JAVA 配置
  10. 特别困的学生 UVa12108(模拟题)