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 ;
}

最新文章

  1. 给numpy矩阵添加一列
  2. Leetcode study time
  3. 【荐】使用eval()、new Function()将JSON字符串转换为JSON对象
  4. BIEE使用技巧
  5. juqery 实现商城循环倒计时
  6. ASP.NET 客户端静态文件请求设置缓存(Client Cache)
  7. jquery layer弹出层插件
  8. C++学习基础二——指针与引用的区别
  9. PHP执行.SQL文件的实例代码分享
  10. SqlServer按照指定顺序对字段进行排序
  11. vs自带服务测试工具
  12. 自己动手写一个编译器Tiny语言解析器实现
  13. iOS开发只简单动画实现
  14. Python之False和None
  15. springboot 2.0部署到Tomat8.5上
  16. 【C++】STL常用容器总结之五:双端队列deque
  17. Esper学习之八:EPL语法(四)
  18. 02: DOM 实例
  19. EF学习-获取实体框架01
  20. hdu1754 I Hate It(线段树单点更新,区间查询)

热门文章

  1. BFS HDOJ 1242 Rescue
  2. SpringMVC和MyBatis的整合
  3. C8051特点
  4. 搜狐前端css常用命名
  5. IIS6配置FastCGI遇到ERROR5的解决方法
  6. nodejs on raspi
  7. 【转】Iconfont
  8. iOS自带TTS技术的实现即语音播报
  9. 编程挑战JavaScript进阶篇(慕课网题目)
  10. 通过HTML 取得页面、屏幕、浏览器的高度宽度