poj2406 Power Strings(kmp失配函数)
Power Strings
Time Limit: 3000MS |
Memory Limit: 65536K |
|
Total Submissions: 39291 |
Accepted: 16315 |
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.
Source
【思路】
KMP。
应用KMP算法中的失配函数,如果是一个周期串那么错位部分(n-f[n])一定是一个最小循环节(仔细想想f函数的意义),则答案为i/(n-f[n])。否则ans=1。
时间复杂度为O(n)。
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int maxn = +; void getFail(char* P,int* f) {
int m=strlen(P);
f[]=f[]=;
for(int i=;i<m;i++) {
int j=f[i];
while(j && P[i]!=P[j]) j=f[j];
f[i+]=P[i]==P[j]?j+:;
}
} char s[maxn];
int f[maxn]; int main() {
while(scanf("%s",s)== && s[]!='.') {
getFail(s,f);
int n=strlen(s);
if(f[n]> && n%(n-f[n])==) printf("%d\n",n/(n-f[n]));
else printf("%d\n",);
}
return ;
}
poj2406 Power Strings(kmp失配函数)的更多相关文章
- POJ2406 Power Strings —— KMP or 后缀数组 最小循环节
题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Tot ...
- poj2406 Power Strings (kmp 求最小循环字串)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 47748 Accepted: 19902 ...
- POJ2406Power Strings[KMP 失配函数]
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 45005 Accepted: 18792 D ...
- POJ2406 Power Strings KMP算法
给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的 譬 ...
- POJ2406 Power Strings(KMP)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56162 Accepted: 23370 Description Giv ...
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...
- poj 2406 Power Strings kmp算法
点击打开链接 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27368 Accepted: ...
- POJ 2406 Power Strings (KMP)
Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...
随机推荐
- Activity.startManagingCursor方法
http://blog.sina.com.cn/s/blog_6f14deb60100wd2n.html 总结一下Activity.startManagingCursor方法: 转 我们将获得的Cur ...
- 创建 window service 定时任务
参考文章:http://www.cnblogs.com/jack-liang/archive/2011/05/20/2051743.html 前段时间做过一个项目,前端系统提供添加定时任务,后端系统要 ...
- C#中的Dictionary简介
简介在C#中,Dictionary提供快速的基于兼职的元素查找.当你有很多元素的时候可以使用它.它包含在System.Collections.Generic名空间中. 在使用前,你必须声明它的键类型和 ...
- Java GC 日志输出分析
搜到的几篇讲GC日志的文章,学到了很多东西.但是有些错误或者不够精确的地方. 因此自己尝试着总结一下. 先写个程序,然后结合程序解释每句话的意思. 运行参数 -Xms200M -Xmx200M -Xm ...
- corejava-chap01
<java是什么:>Programming language 程序语言Development environment 开发环境Application environment 应用环境Dep ...
- 【转】无废话WCF系列教程
转自:http://www.cnblogs.com/iamlilinfeng/category/415833.html 看后感:这系列的作者李林峰写得真的不错,通过它的例子,让我对WCF有了一 ...
- filter过滤器执行顺序
浏览器请求---->进入过滤器---->进入doFilter方法--->执行chain.doFilter()方法就会放行----->进入业务逻辑方法------>进入过滤 ...
- Linux技巧总结(个人经验版)
1:善用桌面:1.图形界面的编辑,2.终端只要开机就在第2桌面,3.浏览器在第3桌面,4.娱乐在第4桌面. 2:cd命令中,输入中文目录很不方便,用 ln -s 桌面 desktop 创建软链接,不必 ...
- php生成缩略图
<?php /** * 生成缩略图函数(支持图片格式:gif.jpeg.png和bmp) * @author ruxing.li * @param string $src 源图片路径 * @pa ...
- 【译】iOS人性化界面指南(iOS Human Interface Guidelines)(一)
1. 引言1.1 译者自述 我是一个表达能力一般的开发员,不管是书面表达,还是语言表达.在很早以前其实就有通过写博客锻炼这方面能力的想法,但水平有限实在没有什么拿得出手的东西分享.自2015年7月以来 ...