题目链接: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. 利用MsChart控件绘制多曲线图表(转载)

    在.Net4.0框架中,微软已经将Mschart控件集成了进来,以前一直在web下面用过,原来winform下的Mschart控件更加简单更加方便,今天我们用mschart绘制一个多曲线图,话不多说, ...

  2. xcode修改默认头部注释(__MyCompanyName__) (转)

    打开命令行: defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ "ORGANIZATIONNAME&qu ...

  3. UDP通信-UdpClient

    static void Main(string[] args) { Console.WriteLine("发送端"); byte[] buffer = System.Text.En ...

  4. 错误 error C2678: 二进制“<”: 没有找到接受“const card”类型的左操作数的运算符(或没有可接受的转换)

    错误出现的地方如下 而我又重载了<运算符,但是我没有将<运算符重载函数定义成const类型,此处是const _Ty&,不可以调用非const成员函数 而且,一般而言,像<, ...

  5. iOS开发-- 字符串分割、拼接

    ---------------------字符串分割实例---------------------NSString * ptr = @"I am a man"; //将字符串整体作 ...

  6. Linux下的/proc目录介绍

    proc被称为虚拟文件系统,它是一个控制中心,可以通过更改其中某些文件改变内核运行状态, 它也是内核提空给我们的查询中心,用户可以通过它查看系统硬件及当前运行的进程信息. Linux中许多工具的数据来 ...

  7. code_blocks 使用操作手册

                                                                              38 39 编译以上程序,产生如下提示信息. 如此简 ...

  8. could not bind to address 0.0.0.0:80 no listening sockets available, shutting down

    在启动apache服务的时候(service httpd start 启动)出现这个问题. 出现这个问题,是因为APACHE的默认端口被占用的缘故.解决方法就是把这个端口占用的程序占用的端口去掉. 使 ...

  9. Esper学习之十五:Pattern(二)

    上一篇开始了新一轮语法——Pattern的讲解,一开始为大家普及了几个基础知识,其中有说到操作符.当时只是把它们都列举出来了,所以今天这篇就是专门详解这些操作符的,但是由于篇幅限制,本篇先会讲几个,剩 ...

  10. 【大数据系列】Hive安装及web模式管理

    一.什么是Hive Hive是建立在Hadoop基础常的数据仓库基础架构,,它提供了一系列的工具,可以用了进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在Hadoop中的按规模数据的 ...