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. 写给java web一年左右工作经验的人

      摘要 大学就开始学习web,磕磕绊绊一路走过来,当中得到过开源社区很多的帮助,总结了这些年来的技术积累,回馈给开源社区. ps:图片都是从网上盗...感谢原作者. ps:文字千真万确都是我自己写的 ...

  2. 无线渗透--wifiphisher之wifi钓鱼获取wifi密码

    本来是想试验一下暴力破解的,但是由于字典太大,跑的时间也比较长,于是使用了钓鱼的方法. 先说一下wifiphisher钓鱼获取wifi密码的原理: wifiphisher对于你在攻击中选定的wifi会 ...

  3. CentOS操作系统部署zabbix agent服务

    CentOS操作系统部署zabbix agent服务 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看zabbix的官方手册 1>.点击下载 2>.查看Ubunt ...

  4. HihoCoder第三周与POJ2406:KMP算法总结

    HihoCoder第三周: 输入 第一行一个整数N,表示测试数据组数. 接下来的N*2行,每两行表示一个测试数据.在每一个测试数据中,第一行为模式串,由不超过10^4个大写字母组成,第二行为原串,由不 ...

  5. APM概述

    当前 APM(Application Performance Management (Monitoring)/ 应用性能管理)或者叫 Distributed Tracing System 工具有: G ...

  6. OLTP和OLAP区别详解

    Oracle属于OLTP类型,而gp数据库属于OLAP类型的.具体了解如下: 数据库系统一般分为两种类型,一种是面向前台应用的,应用比较简单,但是重吞吐和高并发的OLTP类型:一种是重计算的,对大数据 ...

  7. 看完本文,Essay写作再也不需要凑字数

    很多同学都说过自己写论文的时候出现“词穷”的情况,无奈只能靠“胡编乱造”来凑字数写出开头段,这其实是大家的阅读量没有达到要求.但不能因为出现这种情况就对自己的论文不负责任,否则你的论文分数可能就不会对 ...

  8. SpringBoot-拦截器配置

    SpringBoot-拦截器配置 SpringBoot-拦截器配置 在我们的SSM项目中,可以在web.xml中配置拦截器,但是在SpringBoot中只能使用java类来配置,配置方法如下. 创建拦 ...

  9. python基础数据类型--元组(tuple)

    python基础数据类型--元组(tuple) 一.元组的定义和特性 定义:与列表相似,只不过就是将[ ] 改成 ( ) 特性:1.可以存放多个值 2.不可变 3.按照从左到右的顺序定义元组元素,下标 ...

  10. 2020/2/21 fiyocms代码审计

    0x00 前言 上午上了网课,一上午就装好了cms,下午还有网课,要是结束的早就进行审计. 解决了一下phpstudy使用过程中: Forbidden You don't have permissio ...