kuangbin专题十六 KMP&&扩展KMP POJ2406 Power Strings
2024-10-05 04:29:59
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.
prekmp写残了,导致以为EOF不会写,后来才发现Next[j]=-1了。。。。
其实就是求最小循环节,然后循环次数n就最大了。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
using namespace std;
int Next[],n;
char p[]; void prekmp() {
int i,j;
j=Next[]=-;
i=;
while(i<n) {
while(j!=-&&p[i]!=p[j]) j=Next[j];
if(p[++i]==p[++j]) Next[i]=Next[j];
else Next[i]=j;
}
} int main() {
//freopen("in","r",stdin);
while(~scanf("%s",p)) {
if(p[]=='.') break;
n=strlen(p);
prekmp();
int l=n-Next[n];
if(n%l==) printf("%d\n",n/l);
else printf("%d\n",);
}
}
最新文章
- centos6.5安装oracle11g_2
- map 和 vector 的erase函数说明
- grep使用多个查询条件--或
- 转: Oracle表空间查询
- Spring MVC实现文件下载
- Xcode7如何添加pch文件
- 错误: 找不到或无法加载主类 / Class not found
- Latex调整行距
- mob分享
- JAVA并发编程实战---第三章:对象的共享(2)
- ajaxfileupload批量上传文件+图片尺寸限制
- 【伯乐在线】HashMap的工作原理
- 【憩园】C#并发编程之异步编程(一)
- 浅谈AJAX基本实现流程
- MySQL数据库对象-索引
- ffmpag总结_android_to_ios视频转换
- Mysql更改表名大小写不敏感
- CRC8校验
- spring AOP 之一:spring AOP功能介绍
- srv.exe蠕虫病毒~