当前位置:网站首页>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;
}
边栏推荐
- ImageView picture fill problem
- JVM之对象的内存布局
- HDU 6778 Car (分组枚举-->状压 dp)
- JVM instructions for four call methods
- SymPy Tutorial(译)
- 520 钻石争霸赛 2021
- nacos注册中心集群
- Leetcode MySQL database topic 181
- Caused by: org. apache. xerces. impl. io. MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
- Constructing SQL statements by sprintf() function in C language
猜你喜欢
随机推荐
使用Rancher搭建Kubernetes集群
Pipeline details of IPC (interprocess communication)
另类实现 ScrollView 下拉头部放大
山科 的C语言2018练习题(电信)
Codeforces Round #657 Div. 2
L2-3 is this a binary search tree- The explanation is wonderful
Ural1517 freedom of choice [suffix array: longest common continuous substring]
子串分值-超详细版——最后的编程挑战
Codeforces Round #641 Div2
Caused by: org. apache. xerces. impl. io. MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
如果我在北京,到哪里开户比较好?另外想问,现在在线开户安全么?
Time varying and non time varying
Middle order traversal of Li Kou 94 binary tree
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
The Stones Game【取石子博弈 & 思维】
setInterval、setTimeout和requestAnimationFrame
Flutter 基础组件之 ListView
逆向思维-小故事
Leetcode MySQL database topic 181
FreeRTOS (IX) - queue








