当前位置:网站首页>Power strings [KMP cycle section]
Power strings [KMP cycle section]
2022-06-29 10:15:00 【Yekaterina 2】
Given two strings a and b we define ab to be their concatenation. For example, if a = “abc” and b = “def” then ab = “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.
Their thinking :
Typical kmp Find the topic of circular section . Judge n%(n-f[n]) Is it equal to 0, If it is equal to zero, it means that the string is composed of multiple identical strings ,n/(n-f[n]) Is the answer .
Code
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 1000005;
char s[N];
int f[N];
int len;
void getfail(){
f[0]=0,f[1]=0;
int j;
for(int i=1;i<len;i++){
j=f[i];
while(j&&s[i]!=s[j]) j=f[j];
f[i+1]= s[i]==s[j]?j+1:0;
}
}
int main(){
while(~scanf("%s",s),s[0]!='.'){
len=strlen(s);
getfail();
if(len%(len-f[len])==0)
printf("%d\n",len/(len-f[len]));
else
printf("1\n");
}
return 0;
}
边栏推荐
猜你喜欢

Flutter 基础组件之 Container

任务调度器之Azkaban的使用

图片验证码控件

Codeforces Round #645 (Div. 2)

Dynamic linking of virtual machine stack of JVM

Constructing SQL statements by sprintf() function in C language

弧形 View 和弧形 ViewPager

EDA and VHDL question bank

Perfect binary tree, complete binary tree, perfect binary tree

Codeforces Round #659 (Div. 2)
随机推荐
Minorgc, majorgc, fullgc
十六制计数器和流水灯
L2-3 is this a binary search tree- The explanation is wonderful
Constructing SQL statements by sprintf() function in C language
Codeforces Round #652 (Div. 2)
Codeforces Round #645 (Div. 2)
Codeforces Round #641 Div2
Flutter 基础组件之 ListView
Ural1517 freedom of choice [suffix array: longest common continuous substring]
GCC and makefile
子串分值-超详细版——最后的编程挑战
520 钻石争霸赛 2021
HDU 4578 Transformation(线段树+有技巧的懒标记下放)
Codeforces Round #652 (Div. 2)
Setinterval, setTimeout and requestanimationframe
同花顺炒股软件可靠吗,安全吗?
Six dimensional space BFS
山科 的C语言2018练习题(电信)
JVM instructions for four call methods
Codeforces Round #645 (Div. 2)