B - Power Strings

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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.
 又是纯KMP算法
昨晚太晚了,浏览了下题目,就好困,然后就睡觉去了
今天早上坐公交的时候开始想了下这个题目,就想出来做法了。(PS.长沙周一上班高峰,公交真TM挤。地铁遥遥无期啊)。
做法如下:
只需用KMP算法求出next数组值,这个具体求法我昨晚的日志写的很清晰。
在求的过程中,用一个变量记录最新的失配处的位置,这样,初始点到失配点的字符串,即为重复母串,比如ababab,从第三位到最后一位均可匹配,因此失配点即为第二位,b。。因此结果就是总位数6/2=3;
当然之前想简单了,比如abababa,如果按上述做法,也会输出3.因为它的next值为0012345,明显失配处好像是第二位。。。实则要加入判断条件:如果总位数%重复母船长度!=0,则重复母串不存在,直接输出1.
 
#include <iostream>
#include <cstdio>
using namespace std;
char str[];
int next[];
int knext()
{
int mini=;
next[]=;
int i,j=;
for (i=;str[i]!='\0';i++)
{
if (str[i]==str[j]) j++;
else
{
while (j>&&str[j]!=str[i]) j=next[j-];
if (str[j]==str[i]) j++;
}
next[i]=j;
if (next[i]!=next[i-1]) mini=i-j;//当前失配,记录最新失配点。
}
if (i%(mini+)==)//如果重复母串根本不能构成总串,说明根本不是重复母串。
return i/(mini+);
else return ;
}
int main()
{
while (gets(str))
{
if (str[]=='.') break;
printf("%d\n",knext());
}
return ;
}

poj_2406 KMP寻找重复子串问题的更多相关文章

  1. poj 2406 Power Strings(kmp求一个串的重复子串)

    题意:重复子串次数 思路:kmp #include<iostream> #include<stdio.h> #include<string.h> using nam ...

  2. POJ 1743 - Musical Theme 最长不重叠重复子串

    题意:    给出一列数据,问你其中重复的最长连续子串的长度    但是有要求:        1. 长度至少为 5 .        2. 两串可以不相等,但两串每个对应位置的数字相减差值固定 (即 ...

  3. poj2406 连续重复子串

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 41110   Accepted: 17099 D ...

  4. POJ 3693 Maximum repetition substring(连续重复子串)

    http://poj.org/problem?id=3693 题意:给定一个字符串,求重复次数最多的连续重复子串. 思路: 这道题确实是搞了很久,首先枚举连续子串的长度L,那么子串肯定包含了r[k], ...

  5. 3. Longest Substring Without Repeating Character[M] 最大不重复子串

    题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  7. 【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串

    后缀数组的论文里的例题,论文里的题解并没有看懂,,, 求一个重复次数最多的连续重复子串,又因为要找最靠前的,所以扫的时候记录最大的重复次数为$ans$,扫完后再后从头暴力扫到尾找重复次数为$ans$的 ...

  8. 【POJ 3261】Milk Patterns 可重叠的k次最长重复子串

    可重叠的k次最长重复子串 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...

  9. spoj687 后缀数组重复次数最多的连续重复子串

    REPEATS - Repeats no tags  A string s is called an (k,l)-repeat if s is obtained by concatenating k& ...

随机推荐

  1. php hash算法实现memcached分布式

    一.概述Memcached和mysql一样,是一款客户端/服务器端(C/S)系统管理软件,有IP.端口,一旦启动,服务器就一直处于可用状态.Mysql是通过SQL语句管理“磁盘中”的文件,Memcac ...

  2. MySQL 如何使用 PV 和 PVC?【转】

    本节演示如何为 MySQL 数据库提供持久化存储,步骤为: 创建 PV 和 PVC. 部署 MySQL. 向 MySQL 添加数据. 模拟节点宕机故障,Kubernetes 将 MySQL 自动迁移到 ...

  3. 第3节 sqoop:7、通过java代码远程连接linux执行shell命令

    数据库的数据同步软件sqoop 数据同步 关系型数据库到大数据平台 任务:sqoop 是批量导入数据太慢,如何做到实时的数据同步 实时的数据同步工具: canal 阿里开源的一个数据库数据实时同步的软 ...

  4. 手机连接jmeter录制脚本测试

    1.准备条件 电脑安装好jmeter,准备好一个手机 注意: 电脑和手机连接的网络要一致 手机设置代理协议前要先进入想要抓取的网站: http://39.107.96.138:3000/ 2.jmet ...

  5. Linux-kernel-timeline

    Linux kernel Protocol Location HTTP https://www.kernel.org/pub/ GIT https://git.kernel.org/ RSYNC rs ...

  6. DCGAN增强图片数据集

    DCGAN增强图片数据集 1.Dependencies Python 3.6+ PyTorch 0.4.0 numpy 1.14.1, matplotlib 2.2.2, scipy 1.1.0 im ...

  7. 吴裕雄--天生自然java开发常用类库学习笔记:SortedSet接口

    import java.util.SortedSet ; import java.util.TreeSet ; public class TreeSetDemo05{ public static vo ...

  8. Spring入门之三-------SpringIoC之Scopes

    一.singleton和prototype public class Bean1 { public Bean1() { System.out.println(this.getClass().getSi ...

  9. ORIGIN(起源属性)路由起源骗术

    ORIGIN(起源属性)配置: ①:抓取感兴趣流量——prefix.access ②:创建route-map 流量地图——permit 10 ③:匹配感兴趣流量——match ④:设置起源属性——se ...

  10. 控制数据的小数位数 java / js

    //java一般控制格式都是通过 DecimalFormat 来控制的.下边是个例子. import java.text.DecimalFormat; public class ControlBit ...