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

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.
 
题意:
给你一个字符串,求该字符串最多由多少个循环字串构成。
 
题解:
  定义一个整型变量 m;   //m表示字符串长度
  用kmp里的next数组 求出字符串的最长前缀长度,然后判断 m 是否能被 m-next[m] 整除,即 m%(m-next[m])是否等于0,如果能被整除,m-next[m]为最短循环字串,否则最短循环字串长度等于整个字符串长度。
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int next[1000002];
char a[1000002]; void getNext()
{
int i=-1,j=0;
next[0]=-1;
int cnt=0;
while(a[j])
{
if(a[i]==a[j]||i==-1)
{
next[++j]=++i;
}
else
i=next[i];
}
}
int main()
{
while(scanf("%s",a)!=EOF)
{
if(a[0]=='.') break;
int m=strlen(a);
getNext();
int cc=1;
if(m%(m-next[m])==0) //如果条件满足,m-next[m]为最短循环字串,然后求出该字符串由多少个最短循环字串构成
cc=m/(m-next[m]);
cout<<cc<<endl;
}
return 0;
}

  

poj2406 Power Strings (kmp 求最小循环字串)的更多相关文章

  1. POJ 2406 - Power Strings - [KMP求最小循环节]

    题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...

  2. [KMP求最小循环节][HDU1358][Period]

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

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

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

  4. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

  5. UVA - 10298 Power Strings (KMP求字符串循环节)

    Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...

  6. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

  7. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

  8. KMP + 求最小循环节 --- HUST 1010 - The Minimum Length

    The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...

  9. HDU 3746 (KMP求最小循环节) Cyclic Nacklace

    题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...

随机推荐

  1. 团体程序设计天梯赛-练习集-L1-024. 后天

    L1-024. 后天 如果今天是星期三,后天就是星期五:如果今天是星期六,后天就是星期一.我们用数字1到7对应星期一到星期日.给定某一天,请你输出那天的“后天”是星期几. 输入格式: 输入第一行给出一 ...

  2. Windows Server菜鸟宝典之一:Windows Server 2008 R2 AD服务器搭建

        1.对于将要安装成为DC的服务器来讲,其系统配置以及基本的磁盘规划在此就不在累述了,但是关键的网络连接属性是必须要注意的.可以通过打开本地连接的属性来进行配置其IP属性.作为服务器DC的IP地 ...

  3. How many integers can you find HDU - 1796_容斥计数

    Code: #include<cstdio> using namespace std; typedef long long ll; const int R=13; ll a[R]; ll ...

  4. linux 结构需要清理 (structure needs cleaning)

    下面操作会删除挂载点所有文件,注意备份. df -T 查看出错的挂载点对应的文件系统和文件系统类型   然后umount这个文件系统 umount /dev/sda1 然后文件系统类型不同操作不同  ...

  5. sql修改字段值

    update dfw_USER_ACCOUNTS set USER_INTEGRAL="20" where USER_INTEGRAL="17"

  6. Python 安装 numpy 以及 matplotlib 的过程

    系统:ubuntu 16.04 版本:Python3.5 步骤: 安装 pip sudo apt install python3-pip 查看 pip list 是否有 numpy 以及 matplo ...

  7. 【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 定义dis[i][j]表示到达i这个点. 用掉了j次去除边的机会的最短路. dis[1][0]= 0; 在写松弛条件的时候. 如果用 ...

  8. js cookie 设置

    (function () { function getCookie(name) { var start = document.cookie.indexOf(name + "="); ...

  9. (转)关于使用iText导出pdf

    一.iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  10. Hadoop Word Count程序

    Hadoop Word Count程序 pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...