题目链接:https://vjudge.net/problem/POJ-2406

kmp学习:https://blog.csdn.net/starstar1992/article/details/54913261/

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.
 
题目大意:问你一个子串的最多多少次方能构成完整的串
思路:其实这题本人是没有想到next数组能直接求出来的 ,后来看了一下题解,大概意思如下:
比如:  ababab,显然最后一位的next数组是3(这里以0开始),所以0~3位和2~5位是一样的,所以0~1和2~3是一样的,2~3和4~5是一样的,所以最多次方数就是3个
比如:ababa,显然最后一位的next数组是3,所以0~2位和2~4位是一样的,所以0~1和2~3是一样的,但是还剩下一位,所以最大只能是1  
综上所述,其实就是求最后一位的next数组,然后总长减去它,看总长是否能够整除这个值,能的话直接输出整除的值,不能的话,答案就是1
看一下证明:

-----------------------------

-----------------------------

k    m        x      j       i

由上,next【i】=j,两段红色的字符串相等(两个字符串完全相等),s[k....j]==s[m....i]

设s[x...j]=s[j....i](xj=ji)

则可得,以下简写字符串表达方式

kj=kx+xj;

mi=mj+ji;

因为xj=ji,所以kx=mj,如下图所示

-------------

-------------

k   m        x     j

看到了没,此时又重复上面的模型了,kx=mj,所以可以一直这样递推下去

所以可以推出一个重要的性质len-next[i]为此字符串的最小循环节(i为字符串的结尾),另外如果len%(len-next[i])==0,此字符串的最小周期就为len/(len-next[i]);

证明来源:https://blog.csdn.net/hp_satan/article/details/18085919

看代码:
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=1e8+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn];
int next[maxn];
void cal_next()
{
next[]=-;
int k=-;
int len=strlen(a);
for(int i=;i<len;i++)
{
while(k>-&&a[k+]!=a[i])
{
k=next[k];
}
if(a[k+]==a[i]) k++;
next[i]=k;
}
}
int main()
{
//while(cin>>a)
while(scanf("%s",a)!=EOF)
{
if(a[]=='.') break;
cal_next();
int len=strlen(a);
if(len%(len-next[len-]-)==) printf("%d\n",len/(len-next[len-]-));
else printf("1\n");
}
return ;
}
 

poj(2406) kmp的更多相关文章

  1. POJ 2406 KMP/后缀数组

    题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...

  2. Power Strings (poj 2406 KMP)

    Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   ...

  3. POJ 2406 KMP next数组的应用

    题意:让你找最小重复串的个数 加深KMP中对next数组的理解 #include <cstdio> #include <cstring> using namespace std ...

  4. POJ 2406 KMP 循环节

    给一个字符串.求这个串的最小的循环节的长度. 好像.num = len/(len-next[len]) 就是循环节的长度.如果 len%(len-next[len]) ==0 就是 说字符串长度刚好是 ...

  5. KMP POJ 2406 Power Strings

    题目传送门 /* 题意:一个串有字串重复n次产生,求最大的n KMP:nex[]的性质应用,感觉对nex加深了理解 */ /************************************** ...

  6. POJ 2406 Power Strings

    F - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  7. poj 2406 Power Strings 后缀数组解法

    连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们 ...

  8. Power Strings POJ - 2406

    Power Strings POJ - 2406 时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 Gi ...

  9. POJ - 2406 ~SPOJ - REPEATS~POJ - 3693 后缀数组求解重复字串问题

    POJ - 2406 题意: 给出一个字符串,要把它写成(x)n的形式,问n的最大值. 这题是求整个串的重复次数,不是重复最多次数的字串 这题很容易想到用KMP求最小循环节就没了,但是后缀数组也能写 ...

随机推荐

  1. 【转】 Pro Android学习笔记(三三):Menu(4):Alternative菜单

    目录(?)[-] 什么是Alternative menu替代菜单 小例子说明 Alternative menu代码 关于Category和规范代码写法 关于flags 多个匹配的itemId等参数 什 ...

  2. 什么是Nginx?为什么使用Nginx?

    源自 https://blog.csdn.net/yougoule/article/details/78186138 一.前言      为毛要用nginx服务器代理,不直接用tomcat 7.0,还 ...

  3. Windows部署jenkins服务器

    本次使用的操作系统: windows server 2012 r2vs版本: vs 2015jenkins: 2.19.4 一.下载jenkins http://mirror.xmission.com ...

  4. centos6.5安装zookeeper教程(三)

    阅读前建议先阅读: http://www.cnblogs.com/duenboa/articles/6665159.html   1. 下载安装文件zookeeper-3.4.6.tar.gz 镜像地 ...

  5. Math(2)

    Math(2) public static void main(String[] args) { System.out.println(Math.floor(-32.8)); //常数 System. ...

  6. Math类简介

    Math  abs max min 分别是绝对值 最大值,最小值 round 四舍五入 ceil ceil(32.6)  33.0 ceil(32.2) 33.0 返回大于该数值的较大的整数 与之相对 ...

  7. Java解析XML:Jdom解析和SAX解析

    今天看了Java解析XML的几种方法,记录一下 1.Jdom解析 (1)读取XML内容 private static void readXML() { // TODO Auto-generated m ...

  8. Luogu 3320 [SDOI2015]寻宝游戏

    一开始还真没想到. 发现从所有有宝藏的点出发绕一圈只要不刻意绕路答案都是一样的,即我们呢要求的最后答案$ans = dis(x_1, x_2) + dis(x_2, x_3) +... + dis(x ...

  9. String与字符数组

    public class Example { static String str = new String("good"); static char[] ch = {'a','b' ...

  10. 2.XML实体注入漏洞攻与防

    XML实体注入基础 当允许引用外部实体时,通过构造恶意内容,可导致读取任意文件.执行系统命令.探测内网端口.攻击内网网站等危害. 简单了解XML以后,我们知道要在XML中使用特殊字符,需要使用实体字符 ...