【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 ;
}
最新文章
- C# salt+hash 加密
- 第二章 --- 关于Javascript 设计模式 之 策略模式
- 我的js函数库(持续更新)
- HTTP,FTP,TCP,UDP及SOCKET
- HTTP 协议详解
- Midway-ModelProxy — 轻量级的接口配置建模框架
- jQuery 的 json 格式的处理问题
- 在CentOS 6.2上安装 MemcacheQ 最新版
- el表达式跟ognl表达式的区别(转)
- [013]函数重载--int*和void*的匹配优先级
- dd usb 启动盘制作 成功版本
- Nginx配置免费SSL证书StartSSL,解决Firefox不信任问题
- 【测试】解决loadrunner11无法运行负载测试
- CSS3知识点整理(四)----布局样式及其他
- 转:js闭包
- SQL优化 MySQL版 - 索引分类、创建方式、删除索引、查看索引、SQL性能问题
- 内存管理-MRC与ARC详解
- Python#常用的模块和简单用法
- HDFS-HA高可用
- WebApi参数传递实例
热门文章
- 网络I/O模型--07Netty基础
- web 应用响应乱码问题
- LeetCode 之二叉树中序遍历(使用栈实现)
- SqlServer查询存储过程中包含指定的内容
- 如何在ScrollView滑动的瞬间禁用拖拽手势
- Difference Between Arraylist And Vector : Core Java Interview Collection Question
- redis几种数据类型以及使用场景
- 绕过CDN查找网站真实IP方法收集
- jenkins + k8s + lnp
- ZT CSDN 如何以最快的速度计算出一个二进制数中1的个数? [