【POJ2406】 Power Strings (KMP)
Power StringsDescription
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
3Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
① len==1||s[next[len-1]]!=s[len-1] ans=1;
② len%(len-next[len])==0 ans= len/(len-next[len]);
原因如图:
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1000010 char s[Maxn];
int len,nt[Maxn]; void kmp()
{
nt[]=;
int p=;
for(int i=;i<=len;i++)
{
while(s[i]!=s[p+]&&p) p=nt[p];
if(s[i]==s[p+]) p++;
nt[i]=p;
}
} int main()
{
int n,i;
while()
{
gets(s+);
len=strlen(s+);
if(len==&&s[]=='.') break;
kmp();
if(len%(len-nt[len])==) printf("%d\n",len/(len-nt[len]));
else printf("1\n");
}
}
[POJ2406]
2016-07-20 16:30:02
【POJ2406】 Power Strings (KMP)的更多相关文章
- 【POJ2406】Power Strings(KMP,后缀数组)
题意: n<=1000000,cas较大 思路:这是一道论文题 后缀数组已弃疗,强行需要DC3构造,懒得(不会)写 ..]of longint; n,m,i,j,len,ans,st:longi ...
- poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...
- POJ 2406 Power Strings(KMP)
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
- Power Strings(KMP)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 45008 Accepted: 18794 D ...
- 【TOJ 2406】Power Strings(KMP找最多重复子串)
描述 Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc& ...
- 【POJ 2406】Power Strings(KMP循环节)
终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节.i-next[i]就是循环节. #include<cstdio> #define N 1000005 char s[N]; i ...
- jzoj3208. 【JSOI2013】编程作业(kmp)
题面 Description Will相信,很多同学都有过这样的经历:大牛已经写好了编程作业,而作为菜鸟的自己不会写怎么办呢?拿大牛的代码抄一下嘛!但是提交一模一样的作业是不是不太好?于是就改一改变量 ...
- 【CH1809】匹配统计(KMP)
题目链接 摘自https://www.cnblogs.com/wyboooo/p/9829517.html 用KMP先求出以a[i]为结尾的前缀与b匹配的最长长度. 比如 f[i] = j,就表示a[ ...
- 【poj2406】 Power Strings
http://poj.org/problem?id=2406 (题目链接) 题意 给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的, 求 R 的最大值. Solution 后 ...
随机推荐
- Mysql权限对照表
Mysql分为5种数据权限.12种结构权限.11种管理权限,当我们需要精细化权限的时候,我们就需要对照来进行授权,这样可以很好的的控制登录人员的权限.
- 修改浏览器User-Agent
IE: 1,F12进入开发人员工具 2,工具->更改用户代理字符串->自定义 3,在"友好名称"中填入"IE9",在"用户代理字符串&qu ...
- 在iframe中获取父页面的元素
a.html <!DOCTYPE html> <html> <head> <title></title> </head> < ...
- File类最基础知识
package File; /** * 创建一个文件: * 判断是否存在,若存在,则创建,若不存在,则删除,最后输出文件是否存在. */ import java.io.File; import jav ...
- 学点css基础
中午时间学点css,附带http://www.w3cschool.cc/css/css-tutorial.html这个链接! 中午的时间学了这些东西!如下图: 附带代码: <!DOCTYPE h ...
- Asp.Net Core简单整理
1.Asp.NetCore 中文入门文档 http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-index.html
- .NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常)
.NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常) 前段时间项目使用一次性读去SQL Server中保存的电子文件的文件流然后返回给客户端保存下载电子文件, ...
- 数据库连接池php-cp介绍
php-cp(php-connect-pool)是用php扩展写的一个数据库连接池. 我们知道php开发速度快,适合创业快速迭代,但当流量大了之后,php大量的短连接给db层造成多余的消耗,而php处 ...
- win10 Administrator
和Win7/Win8.1一样,Win10的管理员账户Administrator是默认隐藏和关闭的,因为该账户权限极高,被不法分子利用后存在极大风险.但如果你想在某些特殊情况下使用该账户,就需要手动将其 ...
- IOS 本地通知
操作流程 1.接收通知 2.注册发送通知 用途:提示时间,闹钟 //接收本地通知(在Appdelegate里面实现) - (void)application:(UIApplication *)appl ...