当前位置:网站首页>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 基础组件之 ListView

2021 team programming ladder competition - Simulation Competition

Codeforces Round #659 (Div. 2)

另类实现 ScrollView 下拉头部放大

图片验证码控件

JVM之方法返回地址

Pipeline details of IPC (interprocess communication)

Constructing SQL statements by sprintf() function in C language

The Stones Game【取石子博弈 & 思维】

Alibaba cloud firewall configuration, multiple settings (iptables and firewall)
随机推荐
Introduction to intranet penetration tool FRP
2019.11.3学习总结
container
1021 Deepest Root (25 分)
Middle order traversal of Li Kou 94 binary tree
The Stones Game【取石子博弈 & 思维】
阿里云防火墙配置,多种设置方式(iptables和fireward)
单片机集成开发环境Keil5的使用
CodeForces - 1151B 思维
sympy的dsolve函数
使用Rancher搭建Kubernetes集群
HDU 6778 car (group enumeration -- > shape pressure DP)
Reverse thinking - short story
聊聊你理解的线程与并发
Binding mechanism of JVM methods
qgis制图
JVM之TLAB
详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
Codeforces Round #641 Div2
Wandering -- the last programming challenge