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. webpack管理资源

    加载Css webpack并不能处理js以外的静态资源,通过loader来支持他们 npm install --save-dev style-loader css-loader const path ...

  2. Android自定义之流式布局

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

  3. matlab练习程序(Arnold图像置乱)

    自从上次写了Hilbert图像置乱之后,就对图像置乱研究了一下,发现这里面也是有很多置乱算法的. Arnold也算一种比较主要的置乱算法,算法由以下变换公式产生: 这里a和b是参数,n是迭代次数,N是 ...

  4. supervisor运行virtualenv环境下的nagios-api

    supervisord-example.conf [unix_http_server] file=/tmp/supervisor.sock ; path to your socket file [su ...

  5. 服务器bios&raid管理

    新服务器配置流程 https://github.com/alces-software/knowledgebase/wiki/server-dell-rseries-r630 dell DTK(sysc ...

  6. Winform中 DataGridView控件中的 CheckBox 的值读出来 始终 为 False ,已解决

    private void DGV_DetailsViewer_CellContentClick(object sender, DataGridViewCellEventArgs e) { )) { D ...

  7. 常见WEB开发安全漏洞 原因分析及解决

    目 录 1 会话标识未更新 3 1.1 原因 3 1.2 解决 3 2 SQL注入 3 2.1 原因 3 2.2 解决 5 3 XSS跨站脚本编制 5 3.1 原因 5 3.2 解决 5 4 XSRF ...

  8. php配置优化-生产环境应用版

    [global] error_log = log/php-fpm.log log_level = warning emergency_restart_threshold = 10 emergency_ ...

  9. 笨办法学Python(三十八)

    习题 38: 阅读代码 现在去找一些 Python 代码阅读一下.你需要自己找代码,然后从中学习一些东西.你学到的东西已经足够让你看懂一些代码了,但你可能还无法理解这些代码的功能.这节课我要教给你的是 ...

  10. Linux --Mysql数据库搭建

    Mysql数据库 安装 准备: [root@localhost /]# rpm -e mysql --nodeps 将rpm方式安装的mysql卸载   [root@localhost /]# gro ...