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 ...
随机推荐
-
Android课程---简单的音乐播放器
第一个:用Activity实现 activity_music_play1.xml <?xml version="1.0" encoding="utf-8" ...
-
libevent源码分析:hello-world例子
hello-world是libevent自带的一个例子,这个例子的作用是启动后监听一个端口,对于所有通过这个端口连接上服务器的程序发送一段字符:hello-world,然后关闭连接. /* * gcc ...
-
2.ViewBag、ViewData、TempData之间的区别
1.ViewBag and ViewData(非跨视图访问) 1)ViewBag是一种dynamic动态类型,用户可以自定义属性并为其赋值,它会在运行时动态解析(例:可以作为变量.数组等各种对象传递并 ...
-
2016HUAS_ACM暑假集训4M - 基础DP
简单的0-1背包问题,大家都会做的.题意不想解释太多. 简述题目的案例及以几个关键 Sample Input 1 //测试组数T 5 10 ...
-
sql语句将本地服务器中的数据插入到外网服务器中
--将本地的数据库中的某张表中的数据导入到180的数据库中 --这个要在本地的数据库运行 exec sp_addlinkedserver 'srv_lnk', '', 'SQLOLEDB','xxx. ...
-
文件下载cordovaFileTransfer:cordova.file.documentsDirectory is null
在Android平台上使用:$cordovaFileTransfer进行文件下载时提示:cordova.file.documentsDirectory is null,查了以下文档参照:http:// ...
-
DMP文件的生成和使用
1.生成dmp的程序 #include <dbghelp.h> #pragma comment(lib, "dbghelp.lib") //设置异常处理回调函数Se ...
-
August 12th 2017 Week 32nd Saturday
That which does not kill us makes us stronger. 但凡不能杀死你的,最终都会使你更强大. Seemingly I have heard this from ...
-
hdu1394Minimum Inversion Number(线段树,求最小逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
-
JavaEE 数据库随机值插入测试
package com.jery.javaee.dbtest; import java.sql.Connection; import java.sql.DriverManager; import ja ...