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-Day79

    27-url控制器之path方法 由于基于re_path的路径编写方式是重复性高的编写url,如果规则改变了,则对应的要去修改很多url,这样是很不方便的 所以采用Django2.0的path去编写u ...

  2. Quoit Design (HDU 1007)平面的最近点对

    题目大意:给定平面上的 n 个点,求距离最近的两个点的距离的一半. n <= 10^5.   晕乎乎的度过了一上午... 总之来学习下分治吧233 分治就是把大问题拆成小问题,然后根据对小问题处 ...

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

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

  4. JAVA基础知识复习小结

    集合 Set集合 Set集合的基本特征是元素不允许重复.HashSet不保存元素顺序,LinkedHashSet用链表保持元素的插入顺序,TreeSet可定制排序规则. HashSet的底层是用Has ...

  5. 提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数

    在Web项目中,如果前端MVC框架使用的是SpringMVC,可以使用Map接收前端请求参数,比bean要方便很多. 尤其是SpringMVC和Mybatis一起用的时候,用Map大大减少了需要的be ...

  6. 教你十分钟构建好 SpringBoot + SSM 框架

    目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵活目前受到了许多人的青睐.而 SpringBoot 的轻量化,简化项目配置, 没有 XML 配置要求等优点现在也得到了 ...

  7. nyoj 803 大数问题

    #include<stdio.h> #include<string.h> #define ll long long #define N 110000 int main() { ...

  8. 数据库连接池和connection的理解

    数据库连接池Data Source Pool的理解 1.数据库连接池允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个连接,避免了每个方法里new connection的耗费资源和时间. ...

  9. MySQL DROP TABLE操作以及 DROP 大表时的注意事项

    语法: 删表 DROP TABLE Syntax DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CA ...

  10. 【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】

    [033-Search in Rotated Sorted Array(在旋转数组中搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Suppose a sort ...