当前位置:网站首页>Power Strings【KMP循环节】
Power Strings【KMP循环节】
2022-06-29 09:17:00 【叶卡捷琳娜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.
解题思路:
典型的kmp求循环节题目。判断n%(n-f[n])是否等于0,如果等于零说明这个字符串是由多个相同字符串组成的,n/(n-f[n])就是所求答案。
代码
#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;
}
边栏推荐
- JVM四种调用方法的指令
- JS获取手机型号和系统版本
- Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
- 在Activity外使用startActivity()方法报错原因与解决办法
- MySQL modify auto increment initial value
- 2020-09-25 boost库的noncopyable,用于单例模式
- 语言特性
- 2020-09-23左右值 右值引用 std::move()
- Recyclerview refreshes blinks and crashes when deleting items
- FreeRTOS(八)——时间管理
猜你喜欢

另类实现 ScrollView 下拉头部放大

JVM之方法返回地址

Segmentation of Head and Neck Tumours Using Modified U-net

A 2.5D Cancer Segmentation for MRI Images Based on U-Net
![[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)](/img/d4/f5ea847573433f7ca7bd429f57e40a.png)
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)

Constructing SQL statements by sprintf() function in C language

完美二叉树、完全二叉树、完满二叉树

自定义mvc框架实现

Binding mechanism of JVM methods

JVM之方法的绑定机制
随机推荐
2020-09-29 非商品模板化代码层次 rapidjson库
动态规划总结
数据源连接池未关闭的问题 Could not open JDBC Connection for transaction
leetcode MYSQL数据库题目176
2020-09-21 Visual Studio头文件和库目录配置
After installing anaconda, you need to enter a password to start jupyterlab
Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
A method of creating easy to manage and maintain thread by C language
FreeRTOS (VIII) - time management
Es error nonodeavailableexception[none of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
RecyclerView 通用适配器封装
IPC(进程间通信)之管道详解
JVM之虚拟机栈之动态链接
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
券商经理给的开户二维码办理股票开户安全吗?我想开个户
RecyclerView 粘性(悬浮)头部
2020-9-14 广告系统入门
容器
gSoap例子——calc
HDU 4578 Transformation(线段树+有技巧的懒标记下放)