描述

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).

输入

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.

输出

For each s you should print the largest n such that s = a^n for some string a.

样例输入

abcd
aaaa
ababab
.

样例输出

1
4
3

提示

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

思路:

就是问一个字符串写成(a)^n的形式,求最大的n.

根据KMP的next函数的性质,已知字符串t第k个字符的next[k],那么d=k-next[k],如果k%d==0,那么t[1……k]最多可均匀的分成k/d份。也就是可以生成一个长度为d的重复度为k/d的字串。

#include<bits/stdc++.h>
using namespace std;
const int M=1e6+;
char t[M];
int next[M],tlen;
void getNext()
{
int i=,j=-;
next[]=-;
while(i<tlen)
{
if(j==-||t[i]==t[j])
next[++i]=++j;
else j=next[j];
}
}
int main()
{
while(scanf("%s",t)!=EOF,t[]!='.')
{
tlen=strlen(t);
getNext();
if(tlen%(tlen-next[tlen])==)
printf("%d\n",tlen/(tlen-next[tlen]));
else printf("1\n");
}
return ;
}

【TOJ 2406】Power Strings(KMP找最多重复子串)的更多相关文章

  1. POJ 2406 Power Strings (KMP)

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

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

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

  3. poj 2406 Power Strings(KMP变形)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28102   Accepted: 11755 D ...

  4. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  5. POJ 2406 - Power Strings - [KMP求最小循环节]

    题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...

  6. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  7. POJ 2406 Power Strings KMP算法之next数组的应用

    题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...

  8. POJ 2406 Power Strings KMP求周期

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

  9. poj 2406 Power Strings KMP匹配

    对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],如果t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比方字符串"a ...

随机推荐

  1. Linux sar命令工具详细介绍

    sar命令工具详细介绍 by:授客 QQ:1033553122 由于篇幅限制,采用网盘分享, 下载地址: sar命令工具详细介绍.pdf

  2. Flutter隐藏控件方法

    new Offstage( offstage: true, //这里控制 child: Container(color: Colors.blue,height: 100.0,), ),

  3. 网鼎杯 pwn 记录

    题目位置 https://gitee.com/hac425/blog_data/tree/master/wdb babyheap 通过分配和释放构建 2 个 fastbin 链 利用 show 功能, ...

  4. c# 从一个服务器 访问另外一个服务器上的文件

    页面调用 function fnOpen(path) { window.open("~/FileHelp.ashx? url="); //window.open(url); } 后 ...

  5. Android通过Intent传递对象

    1.传递Serializable方式类对象 首先创建一个序列化类:User import java.io.Serializable; public class User implements Seri ...

  6. 我的blog第一天

    今天是2017年6月13号,一个很平凡的日子,但是对我来说意义非凡.这是我开通博客的第一天,这是我写的第一篇文章. 先自我介绍一下,本人邢卜,河北石家庄人,生于89年6月,说到这我马上就要过生日了!嘿 ...

  7. Debian出现in the drive ‘/media/cdrom/’ and press enter解决办法

    没有光盘源解决打开/etc/apt/sources.list文件,注释掉cdrom那一行,然后再执行apt-get update更新下deb仓库这样以后再使用apt-get安装时就不会再搜寻cdrom ...

  8. 精华阅读第 12 期 | 最新 App Store 审核指南与10大被拒理由?

    很多时候,我们对技术的追求是没有止境的,我们需要不断的学习,进步,再学习,再进步!本文系移动精英开发俱乐部的第12期文章推荐阅读整理,其中涉及到了 Android 数据库框架,架构设计中的循环引用,同 ...

  9. C#中的三种timer

    转 https://blog.csdn.net/hoiven/article/details/51362582 如果你需要使用规律的时间间隔重复执行一些方法,最简单的方式是使用定时器(timer). ...

  10. [控件] TranformFadeView

    TranformFadeView 效果图: 源码地址: https://github.com/YouXianMing/UI-Component-Collection 注意: maskView是iOS8 ...