题目链接:http://poj.org/problem?id=2406

Time Limit: 3000MS Memory Limit: 65536K

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

题意:

求给出字符串,最多是多少个循环节组成的。

题解:

利用len % (len-Next[len]) == 0的话,len-Next[len]是最小循环节长度的性质。

AC代码:

#include<cstdio>
#include<cstring>
using namespace std; const int MAXpat = +; char pat[MAXpat];
int Next[MAXpat],len; void getNext()
{
int i=, j=-;
len=strlen(pat);
Next[]=-;
while(i<len)
{
if(j == - || pat[i] == pat[j]) Next[++i]=++j;
else j=Next[j];
//printf("now i=%d j=%d next[%d]=%d pat[%d]=%c\n",i,j,i,Next[i],i,pat[i]);
}
}
int main()
{
while(scanf("%s",pat))
{
if(pat[]=='.' && pat[]=='\0') break;
getNext();
if(len%(len-Next[len])==) printf("%d\n",len/(len-Next[len]));
else printf("1\n");
}
}

注意:

①当且仅当len%(len-Next[len])==0时,len-Next[len]才是最小循环节长度。

②关于Next数组,我觉得这个图很不错的展示了Next数组存储了啥:

  

POJ 2406 - Power Strings - [KMP求最小循环节]的更多相关文章

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

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

  2. POJ 2406 Power Strings KMP求周期

    传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...

  3. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  4. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  5. poj2406--Power Strings(KMP求最小循环节)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33178   Accepted: 13792 D ...

  6. KMP 求最小循环节

    转载自:https://www.cnblogs.com/chenxiwenruo/p/3546457.html KMP模板,最小循环节   下面是有关学习KMP的参考网站 http://blog.cs ...

  7. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  8. hdu 3746 Cyclic Nacklace (KMP求最小循环节)

    //len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...

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

                                                                                                      Po ...

随机推荐

  1. C#静态构造函数调用机制

    https://blog.csdn.net/cjolj/article/details/56329230 若一个类中有静态构造函数,在首次实例化该类或任何的静态成员被引用时,.NET自动调用静态构造函 ...

  2. 给一个由n-1个整数组成的未排序的序列,其元素都是1~n中的不同的整数。如何在线性时间复杂度内寻找序列中缺失的整数

    思路分析:尼玛这不就是等差数列么.首先将该n-1个整数相加,得到sum,然后用(1+n)n/2减去sum,得到的差即为缺失的整数.因为1~n一共n个数,n个数的和为(1+n)n/2,而未排序数列的和为 ...

  3. SVN的Hooks功能--强制添加注释

    所谓hooks,可以类似 理解Linux内核Netfilter框架的hook点和hook函数的概念.当用户在维护代码的过程中,其执行的相关动作正好触发了相关hook点,就 会去执行对应hook点的脚本 ...

  4. 【SVN】自动定时更新

    程序或脚本:"C:\Program Files\TortoiseSVN\bin\svn.exe" 参数:update E:\XXXXProjects\Code 参考:https:/ ...

  5. [XPath] XPath 与 lxml (五)XPath 实例

    本文继续沿用第三章的 XML 示例文档. 选取价格高于30的 price 节点 # 从父节点进行筛选 >>> root.xpath('//book[price>30]/pric ...

  6. mybayis 之resultType="map"

    List<Map> publishInfos = memberShareMapper.shareToCouponCountGroupByPublishId(memberShare.getA ...

  7. C++ template —— 深入模板基础(二)

    上一篇C++ template —— 模板基础(一)讲解了有关C++模板的大多数概念,日常C++程序设计中所遇到的很多问题,都可以从这部分教程得到解答.本篇中我们深入语言特性.------------ ...

  8. iOS - UITextView放在自定义cell里面-自适应高度

    textView放在自定义cell里面-自适应高度 1,textView有个属性 scrollEnabled  要设置为NO; 2,设置tableview的时候  添加这两行代码: self.tabl ...

  9. OpenCV——识别各省份地图轮廓

    好久没有发OpenCV的博客了,最近想到了一个识别地图轮廓的方案,就写来试试.(识别中国的28个省份地图轮廓,不考虑直辖市) 首先,我的基本思路是  用最小的矩形将地图的轮廓圈出来,可以根据长方形的长 ...

  10. RAID在数据库存储上的应用

    随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...