Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 43514   Accepted: 18153

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算法,next表示模式串如果第i位(设str[0]为第0位)与文本串第j位不匹配则要回到第next[i]位继续与文本串第j位匹配。则模式串第1位到next[n]与模式串第n-next[n]位到n位是匹配的。如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]。对于一个串,如果abcdabc, 那么next[len]=3,那么len-next【len】就大于len/2,那么len%(len-next[len])!=0;而对于一个周期串ababab next[len]=4,此时len-next[len]应该等于最小串的长度,最小周期就可以用len%(len-next[len])是否为0来判断。

 
 
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int maxn = ;
char str[maxn];
int Next[maxn];
int len;
void get_next()
{
int j = -;
int i = ;
Next[] = ;
while(i < len)
{
if(str[i] == str[j] || j == -)
{
i++;
j++;
if(str[i] != str[j])
Next[i] = j;
else
Next[i] = Next[j];
}
else
j = Next[j];
}
} int main()
{
while(~scanf("%s",str))
{
if(str[] == '.')
break;
len =strlen(str);
get_next();
int ans = ;
if(len%(len-Next[len]) == )
ans = len/(len-Next[len]);
//for(int g = 0; g <= len; g++)
//{
// cout << Next[g] << ' ';
//}
//cout << endl;
cout << ans << endl;
}
return ;
}

POJ2406----Power Strings解题报告的更多相关文章

  1. poj2406 Power Strings(kmp)

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

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

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

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

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

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

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

  5. 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...

  6. 【LeetCode】555. Split Concatenated Strings 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  7. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  8. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  9. 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 对Java“一切皆对象”的理念的理解

    在从HelloWorld到面向对象中,我们将int, float, double, boolean等称为基本类型(primitive type),也就是特殊的类.我们可以将一个整数理解称为一个int类 ...

  2. Java中什么时候使用构造方法

    JAVA是面向对象的语言,面向对象不是这么直接简单,它的设计思想就是要代码重用.即我以前干过类似的事,那么我找出以前可以用到的代码,完成一部分.以前没有的我重新写.这样就有了类.有了类,就是有了可以重 ...

  3. 谈谈 char *num="123";和char num[4]="123";的区别

    最近写程序的时候发现这样一个问题 #include<iostream> #include <string.h> using namespace std; void revers ...

  4. URAL1355. Bald Spot Revisited

    1355 其实就是求质因子的个数 这样肯定是最多的 注意一下 除到最后不是1的情况 #include <iostream> #include<cstdio> #include& ...

  5. gitflow workflow

    https://www.atlassian.com/git/tutorials/comparing-workflows/forking-workflow Dream big, work smart,  ...

  6. 深入.NET平台和C#编程 错题录

    1.在C#中,关于文件操作相关的类说法正确的是(AB) <选择二项> A:FileInfo类提供了用于操作文件的实例方法 B:File类提供了用于操作文件的静态方法 C:Directory ...

  7. UVa 156 Ananagrams

    题意:给出一些单词,在这些单词里面找出不能通过字母重排得到的单词(判断的时候不用管大小写),然后按照字典序输出. 学习的紫书的map= = 将每一个单词标准化 先都转化为小写,再排序(即满足了题目中说 ...

  8. 如何使用USB安装XenServer 6.x

    在XenServer 5.6以前我们能够很容易的通过一些工具,直接制作USB安装介质,然后快速安装XenServer,但是我们发现,到XenServer6.0以后,通过工具直接制作的XenServer ...

  9. codevs 1228 苹果树

    dfs序+线段树 #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...

  10. JAVA规则引擎 -- Drools

    Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...