xtu字符串 B. Power Strings
2024-08-22 19:56:30
B. Power Strings
Time Limit: 3000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld Java class name: Main
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "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的适配数组。如果字符长度可以被(字符长度-fail[字符长度])整除,循环节这是这个商,否则循环节长度为1,即就是这个字符本身。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
char str[maxn];
int fail[maxn];
void getFail(int &len) {
int i,j;
len = strlen(str);
fail[] = fail[];
for(i = ; i < len; i++) {
j = fail[i];
while(j && str[j] != str[i]) j = fail[j];
fail[i+] = str[j] == str[i] ? j+:;
}
}
int main() {
int len;
while(gets(str) && str[] != '.') {
getFail(len);
if(len%(len-fail[len])) puts("");
else printf("%d\n",len/(len-fail[len]));
}
return ;
}
最新文章
- 给numpy矩阵添加一列
- Leetcode study time
- 【荐】使用eval()、new Function()将JSON字符串转换为JSON对象
- BIEE使用技巧
- juqery 实现商城循环倒计时
- ASP.NET 客户端静态文件请求设置缓存(Client Cache)
- jquery layer弹出层插件
- C++学习基础二——指针与引用的区别
- PHP执行.SQL文件的实例代码分享
- SqlServer按照指定顺序对字段进行排序
- vs自带服务测试工具
- 自己动手写一个编译器Tiny语言解析器实现
- iOS开发只简单动画实现
- Python之False和None
- springboot 2.0部署到Tomat8.5上
- 【C++】STL常用容器总结之五:双端队列deque
- Esper学习之八:EPL语法(四)
- 02: DOM 实例
- EF学习-获取实体框架01
- hdu1754 I Hate It(线段树单点更新,区间查询)