描述

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

提示

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

思路:

就是问一个字符串写成(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找最多重复子串)的更多相关文章

  1. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  2. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  3. poj 2406 Power Strings(KMP变形)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28102   Accepted: 11755 D ...

  4. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  5. POJ 2406 - Power Strings - [KMP求最小循环节]

    题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...

  6. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  7. POJ 2406 Power Strings KMP算法之next数组的应用

    题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...

  8. POJ 2406 Power Strings KMP求周期

    传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...

  9. poj 2406 Power Strings KMP匹配

    对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],如果t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比方字符串"a ...

随机推荐

  1. 润乾V5手机报表说明文档

    1.手机报表实例页面简要说明 index.jsp 是报表资源列表页面: mbReport.jsp 是报表展现页面: mbParam.jsp是参数报表展现页面: echarts.jsp是带有echart ...

  2. oracle 企业管理器及无线网环境下配置方法

    注意: oracle em 的访问地址在 D:\oracle\product\11.2.0\dbhome_1\install 下的文件里. 如果在windows 安装oracle,并且在本地访问,or ...

  3. windows 设置CapsLock键开启大写后使用shift键取消大写

    1.打开控制面板——>时钟.语言和区域——>更改键盘或其它输入法——>更改键盘——>高级键设置——>要关闭Caps Lock 2.ok

  4. ASP.NET Claims-based认证实现认证登录-claims基础知识

    claims-based认证这种方式将认证和授权与登录代码分开,将认证和授权拆分成另外的web服务.活生生的例子就是我们的qq集成登录,未必qq集成登录采用的是claims-based认证这种模式,但 ...

  5. CCSUOJ评测系统——第二次scrum冲刺

    1.小组成员 舒 溢 许嘉荣 唐 浩 黄欣欣 廖帅元 2.第二次冲刺任务安排 对HUSTOJ数据库进行分析 序号 表名 作用 备注 1 compileinfo 记录编译错误的记录 2 contest ...

  6. PyQt4(简单界面)

    import sys; from PyQt4 import QtCore, QtGui; app=QtGui.QApplication(sys.argv); widget=QtGui.QWidget( ...

  7. logback总结

    Logback Logback由三大模块组成:logback-core.logback- classic和logback-access. Logback-core是其它两个模块的基础模块. Logba ...

  8. 全局css,js缓存及更新版本策略

    在当今web世界里,CDN对于加速页面加载速度,提高用户体验起了非常重要的作用.但是问题也带来了:作为开发人员,可能需要不定时的更新部分静态文件,比如对网页的重新设计会涉及到css文件的更新,这时怎么 ...

  9. eclipse导入spring aop xml约束

    步骤: 1. 2. 3. 4.取  Location:中最后一个命名:spring-aop-4.2.xsd,放到Key的最后面,Key type:选择 Schema location.点击OK 5.编 ...

  10. 要提高SQL查询效率where语句条件的先后次序应如何写

    我们要做到不但会写SQL,还要做到写出性能优良的SQL语句. (1)选择最有效率的表名顺序(只在基于规则的优化器中有效): Oracle的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句 ...