Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 56162   Accepted: 23370

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.

Source

 
 
kmp的经典应用
设$len$表示字符串的长度,$next[i]$表示$i$号字符串的最长公共前后缀的长度
如果$len \ mod  \  next[len] == 0$,那么循环节的长度为$n / next[len]$
 
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 1e6 + ;
char s[MAXN];
int fail[MAXN];
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
#endif
while(scanf("%s", s + ) && s[] != '.') {
int N = strlen(s + ), now = ;
for(int i = ; i <= N; i++) {
while(now && s[i] != s[now + ]) now = fail[now];
if(s[i] == s[now + ]) now++;
fail[i] = now;
}
if(N % (N - fail[N]) == ) printf("%d\n", N / (N - fail[N]));
else printf("1\n");
}
return ;
}
 
 

POJ2406 Power Strings(KMP)的更多相关文章

  1. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  2. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

  3. POJ2406 Power Strings KMP算法

    给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的  譬 ...

  4. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

  5. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  6. poj2406 Power Strings(kmp失配函数)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...

  7. POJ 2406 Power Strings (KMP)

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

  8. poj 2406 Power Strings kmp算法

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

  9. Power Strings(kmp妙解)

    Power Strings Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

随机推荐

  1. typeScript入门(一)构建环境和数据类型

    最近入坑v-cli 3.0,发现ts越来越常用了,于是开始入坑学习. 1.构建ts环境 npm install -g typescript Mac和vscode用户可以用以下方式构建tsdemo项目 ...

  2. flask-session总结

    一.session       session和cookie的原理和区别: cookie是保存在浏览器上的键值对             session是存在服务端的键值对(服务端的session就是 ...

  3. Android自定义之流式布局

    流式布局,好处就是父类布局可以自动的判断子孩子是不是需要换行,什么时候需要换行,可以做到网页版的标签的效果.今天就是简单的做了自定义的流式布局. 具体效果: 原理: 其实很简单,Measure  La ...

  4. IIS10搭建FTP服务

    1.首先是基本搭建 http://jingyan.baidu.com/article/0bc808fc408fa91bd585b94f.html 2.计算机—管理----本地用户和组----本地用户- ...

  5. 一对多sql

    <!-- 分页查询派货成本 --> <select id="queryCostRegionPriceBycondtion" parameterMap=" ...

  6. 服务器常用命令之 启用/禁用PING状态

    启用 echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all  (PING的通) 禁用 echo 1 > /proc/sys/net/ipv4/i ...

  7. 在Kubernetes上运行SAP UI5应用(上)

    2018年只剩最后30天了.Jerry在2017年的最后一天,曾经立下一个目标:这个微信公众号在2018年保证至少每周发布一篇SAP原创技术文章. 从Jerry在后台统计的2018全年文章数量来看,这 ...

  8. Xpath定位_1:子找父以及contains的用法

    先上xml代码,如下图,在写自动化脚本时,需要定位到数字为10334的td元素.td元素的父元素.父的父元素以及属性值都一样:只有同胞元素的元素值不同.以此可以通过先定位到同胞元素,在找到父元素下的期 ...

  9. IOS VLC (第三方音频)的使用

    使用注意 ● 注意点 ● 存放VLC的文件夹名不要有空格 ● 一旦执行过编译脚本sh,就别再修改存放VLC的文件夹名   编译 ● 打开最外层的工作空间 选择真机运行 编译 ● 一些简单的示例程序 集 ...

  10. W5100与MCU的连接方式

    W5100与MCU的连接方式 W5100与MCU的连接方式主要有直接总线连接.间接总线连接.SPI总线连接这三种连接方法,不同的连接方法适应于不同的场合,应该按需选择最恰当的连接方式. 1)直接总线连 ...