【TOJ 2406】Power Strings(KMP找最多重复子串)
描述
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
提示
思路:
就是问一个字符串写成(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找最多重复子串)的更多相关文章
- POJ 2406 Power Strings (KMP)
Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...
- poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)
http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submiss ...
- poj 2406 Power Strings(KMP变形)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28102 Accepted: 11755 D ...
- poj 2406 Power Strings kmp算法
点击打开链接 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27368 Accepted: ...
- POJ 2406 - Power Strings - [KMP求最小循环节]
题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...
- POJ 2406 Power Strings KMP运用题解
本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...
- POJ 2406 Power Strings KMP算法之next数组的应用
题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...
- POJ 2406 Power Strings KMP求周期
传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...
- poj 2406 Power Strings KMP匹配
对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],如果t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比方字符串"a ...
随机推荐
- RabbitMQ,Windows环境下安装搭建
切入正题:RabbitMQ的Windows环境下安装搭建 一.首先安装otp_win64_20.1.exe,,, 二.然后安装,rabbitmq-server-3.6.12.exe, 安装完成后,在服 ...
- Android 自定义AlertDialog(退出提示框)
有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog) 以下是我在开发一个小游戏中总结出来的.希望对大家有用. 先上效果图: 下面是用到的背景图或按钮的图片 经过查找资料和参 ...
- Ionic step by step (1)
刚接触 ionic,一步一步学习,有错误的,望大家指出. 公式 Ionic = Cordova + Angular2 + ionic CSS Cordova: 提供了使用 JavaScript 调用 ...
- Oracle EBS 更改物料说明后,在MTL_SYSTEM_ITEMS_B表中无变化
需要再中文和英文环境同时修改: 程序里,可以通过初始session语言环境来解决.
- springboot 针对jackson是自动化配置
spring.jackson.date-format指定日期格式,比如yyyy-MM-dd HH:mm:ss,或者具体的格式化类的全限定名 spring.jackson.deserialization ...
- js判断状态
'<input type="radio" class="danxuan" name="danxuan" code="'||v ...
- [翻译] DCPathButton
DCPathButton https://github.com/Tangdixi/DCPathButton DCPathButton 2.0 is a menu button for iOS. Des ...
- MVC中JavaScript和CSS的自动打包与压缩
在程序中安装System.Web.Optimization程序集 依赖关系如下图所示: 添加BundleConfiguration类 代码如下所示 注意必须使用对应的ScriptBundle和Styl ...
- 用HashSet存储自定义对象
案例 package cn.itcast_02; import java.util.HashSet; /* * 需求:存储自定义对象,并保证元素的唯一性 * 要求:如果两个对象的成员变量值都相同, ...
- D:\hunting2014\小题目\字符串倒序
#include<stdio.h>#include<string.h> char *revert(char *str){ char temp; char *p = str; c ...