当前位置:网站首页>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;
}
边栏推荐
- HDU 6778 Car (分组枚举-->状压 dp)
- Listview of the basic component of the shutter
- Is flush stock trading software reliable and safe?
- Community Union
- 阿里云防火墙配置,多种设置方式(iptables和fireward)
- manacher
- L2-025 divide and rule (25 points)
- Alibaba cloud firewall configuration, multiple settings (iptables and firewall)
- 2019.10.6训练总结
- HDU 4578 Transformation(线段树+有技巧的懒标记下放)
猜你喜欢

自定义控件之侧滑关闭 Activity 控件

Alibaba cloud firewall configuration, multiple settings (iptables and firewall)

阿里云防火墙配置,多种设置方式(iptables和fireward)

Constructing SQL statements by sprintf() function in C language

Alternative implementation of Scrollview pull-down header amplification

JVM之方法返回地址

RecyclerView 通用适配器封装

JVM之虚拟机栈之动态链接

2021年团体程序设计天梯赛-模拟赛

Image of the basic component of the shutter
随机推荐
Gmail: how to quickly read all messages
2019.10.20 training summary
指针数组、数组指针和传参的相关问题
manacher
JVM之TLAB
2019icpc上海区域赛赛后总结
Nacos registry cluster
URAL1517 Freedom of Choice 【后缀数组:最长公共连续子串】
To 3 -- the last programming challenge
Caused by: org. apache. xerces. impl. io. MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
弧形 View 和弧形 ViewPager
Leetcode MySQL database topic 178
任务调度器之Azkaban的使用
逆向思维-小故事
JVM method return address
nacos注册中心集群
JVM之方法的绑定机制
container
FreeRTOS (VIII) - time management
2019.10.6训练总结