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. 十三: 悲观锁&乐观锁:解决丢失更新问题

    悲观锁:认为丢失更新一定会出现,可以在查询的时候加入for update 认为丢失更新一定会出现,查询时: select * from account for update;for update :  ...

  2. java表单基础

    一.表单  基本语法:   <form method="表单提交方式(post/get)" action="表单提交地址">       </ ...

  3. greenplum 存储过程 函数

    参考:https://docs.pivotal.io/search?q=function

  4. 使用Ubuntu系统编译安装Zabbix企业级监控系统

    使用Ubuntu系统编译安装Zabbix企业级监控系统   作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Ubuntu系统部署笔记:https://www.cnblogs.com/ ...

  5. 热门提示输入之AutoCompleteTextView

    前言: 在我们百度时,经常会遇到在你输入一个内容后在你的输入框的下面会提示一些别的热门的东西,接下来我们来简单实现这种功能 正文: 直接上代码,和之前的东西都大同小异 下面是Java代码 public ...

  6. synchronized和锁(ReentrantLock) 区别

    synchronized和锁(ReentrantLock) 区别 java的两种同步方式, Synchronized与ReentrantLock的区别 并发(一):理解可重入锁 可重入锁和不可重入锁 ...

  7. Http与Https协议规范

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...

  8. PIP无法使用,script文件夹为空解决

    [问题]环境变量已配置,但pip.pip3无法使用,且script文件夹为空解决: 一.安装pip3 python -m ensurepip 运行完之后就pip3有了: 二.安装pip python ...

  9. Kafka--初识Kafka

    前言 数据为企业的发展提供动力.我们从数据中获取信息,对他们进行分析处理,然后生成更多的数据.每个应用程序都会产生数据,包括日志消息,度量指标,用户活动记录,响应消息等.数据的点点滴滴都在暗示一些重要 ...

  10. 【pwnable.kr】 codemap

    pwnable新的一题. download: http://pwnable.kr/bin/codemap.exe ssh codemap@pwnable.kr -p2222 (pw:guest) 这道 ...