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. python tips: is同一性运算符

    is用来判断两个对象是不是同一个对象,通过id()函数取对象地址进行判断.python会缓存一些小的整数,以及只包含字母数字以及下划线的字符串.所以在对于这些值的时候,is判断为True. >& ...

  2. HDU 1164 Eddy's research I( 试除法 & 筛法改造试除法 分解整数 )

    链接:传送门 题意:给出一个整数 n ,输出整数 n 的分解成若干个素因子的方案 思路:经典的整数分解题目,这里采用试除法 和 用筛法改造后的试除法 对正整数 n 进行分解 方法一:试除法对正整数 n ...

  3. [网络流24题] 最长k可重线段集问题 (费用流)

    洛谷传送门 LOJ传送门 最长k可重区间集问题的加强版 大体思路都一样的,不再赘述,但有一些细节需要注意 首先,坐标有负数,而且需要开$longlong$算距离 但下面才是重点: 我们把问题放到了二维 ...

  4. [网络流24题] 最长k可重区间集问题 (费用流)

    洛谷传送门 LOJ传送门 很巧妙的建图啊...刚了$1h$也没想出来,最后看的题解 发现这道题并不类似于我们平时做的网络流题,它是在序列上的,且很难建出来二分图的形. 那就让它在序列上待着吧= = 对 ...

  5. python开发项目:学生选课系统

    程序要求:1.创建北京.上海两所学校(分析:通过学校类实例化两个学校实例) 2.创建Linux.python.go三个课程,Linux\go在北京开,Linux在上海开(创建Linux.python. ...

  6. 为DK2应用程序迁移到Oculus CV1做好准备!

    为DK2应用程序迁移到Oculus CV1做好准备! 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/arti ...

  7. android onContextItemSelected和onMenuItemSelected厉害关系

    Android 的activity中onCreateOptionsMenu onMenuItemSelected onOptionsItemSelected onCreateContextMenu o ...

  8. 多个API接口

    青云客智能聊天机器人API 为保证接口稳定,API接口正式启用 api.qingyunke.com,www.qingyunke.com 如何更换API接口地址? 答:如果您的API接口是用在微信公众号 ...

  9. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 用SPD开发SharePoint应用程序

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 用SPD开发SharePoint应用程序         非常多开 ...

  10. Android适屏

    总结一下自己的适屏经验,仅仅希望自己不断进步,不断完好,假设有热心肠的"前辈"指导一下,不胜感激! Android5.0已经出来了,说是这个版本号对Android屏幕适配做了非常多 ...