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. asp天猫自主发码的请求

    这几天在做天猫自主发码.实现了通知和核销部门.其他的部分待后续实现. 值得注意的是consume回调中,要加入sign_method=md5

  2. Linux日常使用指令大全

    Linux日常使用指令大全 Java代码 www.ahlinux.com 001.日常维护常用查询命令 #top    显示系统进程 #clear  清理屏幕信息 #cat /etc/redhat-r ...

  3. FTP命令详解

    FTP的命令行格式为:ftp -v -d -i -n -g [主机IP或者主机名],其中 -v显示远程服务器的所有响应信息: -n限制ftp的自动登录,即不使用: .n etrc文件: -d使用调试方 ...

  4. HDFS的java操作方式

    1.RPC 1.1 RPC (remote procedure call)远程过程调用. 远程过程指的是不是同一个进程. 1.2 RPC至少有两个过程.调用方(client),被调用方(server) ...

  5. CFileDialog使用总结

    http://blog.csdn.net/tianhai110/article/details/2055149 CFileDialog经常用,但经常忘,现归纳整理下,方便今后查询. 例子: CFile ...

  6. config windows virtual machine on mac

    1.download virtualbox  and related extension pack from http://www.oracle.com/technetwork/server-stor ...

  7. ubuntu 中 ssh连接用UTF-8

    在ubuntu中,文本模式的终端默认情况下是无法显示中文的,尽管有些解决办法,但通常情况下都不是很如意.这时,我们可能会采用英文终端,但当我们用ssh连接的时候,又想用中文的.每次都改是个麻烦事.于是 ...

  8. 推荐 10 款最好的 Python IDE

    简述 Python 非常易学,强大的编程语言.Python 包括高效高级的数据结构,提供简单且高效的面向对象编程. Python 的学习过程少不了 IDE 或者代码编辑器,或者集成的开发编辑器(IDE ...

  9. VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程

      开篇语:最近在弄ocx控件发布到asp.net网站上使用,就是用户在使用过程中,自动下载安装ocx控件.(此文章也是总结了网上好多人写的文章,我只是汇总一下,加上部分自己的东西,在这里感谢所有在网 ...

  10. BZOJ 1076 奖励关

    注意几点: 1.为什么要逆推?由此状态可以轻易算出彼状态是否可行,而彼状态却无法轻易还原为此状态. 2.为什么可以逆推?假设时光倒流了....23333 3.注意位运算的准确,大胆写方程. #incl ...