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. struts中日期处理以及文件下载

    日期处理 对于jsp提交的基本数据类型和日期格式为yyyy-MM-dd的自动转换为相应的 对于其它的日期格式需要自定义转换器 局部类型转换器 1,写转换器类(继承StrutsTypeConverter ...

  2. [NOI2005]维护数列_Splay

    真的毫无算法可言,就是比谁的码力强罢了... Code: #include<stack> #include<cstdio> #include<algorithm> ...

  3. 热重载 预编译 编译器 JS引擎 作用域

    热重载就是页面每次改动,不需要手动去刷新,可自动刷新.保持vuex的状态. JS之预编译 JavaScript的预编译 编译器 JS引擎 作用域三者之间的关系 建议你先去看看你不知道的JavaScri ...

  4. Day 13迭代器生成器

    迭代器 1.迭代器就是迭代的工具,迭代也可以说成是重复,并且每一次重复都是基于上一次的结果而来的,在python中一切皆对象. 2.可迭代对象:只要拥有__iter__方法的对象都是可迭代对象. 3. ...

  5. Centos7下 yum安装php扩展

    linux下, 使用thinkphp的模板标签,如 eq, gt, volist defined, present , empty等 标签时, 报错: used undefined function ...

  6. Codeforces Round #471 (Div. 2)B. Not simply beatiful strings

    Let's call a string adorable if its letters can be realigned in such a way that they form two conseq ...

  7. 在小程序中实现全局混入,以混入的形式扩展小程序的api

    GitHub: https://github.com/WozHuang/mp-extend 相关文章: 小程序全局状态管理,在页面中获取globalData和使用globalSetData 通过页面预 ...

  8. 3.1、Ansible命令简要说明及初步使用

    1.Ansible命令 1.1 Ad-hoc说明 Ansible中有一个很重要的功能就是可以执行ad-hoc命令,它表示即时.临时的意思,即表示一次性的命令.与之相对的是ansible playboo ...

  9. 【codeforces 807D】Dynamic Problem Scoring

    [题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...

  10. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...