POJ2406 Power Strings
来源:互联网 发布:算法设计与分析分治法 编辑:程序博客网 时间:2024/06/15 18:31
Description
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.
POJ怎么大多都是英文题…
继续翻译…
如下:
给定一个字符串L,已知这个字符串是由某个字符串S重复R次而得到的,求R的最大值。
表面上看是后缀数组,事实上是一道暴力!
暴力做法如下:枚举每一串,是否匹配,如果是字符串字母数的整倍数就是答案
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[1100000],n=0;int main(){ while(1) { char st[1100000]; scanf("%s",st+1);int len=strlen(st+1); if(st[1]=='.'&&len==1)break; int i=2,j=1; while(i<=len) { if(st[i]==st[j]) { i=i+1; j=j+1; } else { j=1; i++; } } if(len%(i-j)==0)printf("%d\n",len/(i-j)); else printf("1\n"); } return 0;}
后缀数组也算简单,如下:
穷举字符串S的长度k,然后判断是否满足。判断的时候,先看字符串L的长度能否被k整除,再看suffix(1)和suffix(k+1)的最长公共前缀是否等于n-k。在询问最长公共前缀的时候,suffix(1)是固定的,所以RMQ问题没有必要做所有的预处理,只需求出height数组中的每一个数到height[rank[1]]之间的最小值即可。整个做法的时间复杂度为O(n)。
代码本人没有打……
by_lmy
- POJ2406 Power Strings
- poj2406 Power Strings
- poj2406--Power Strings
- 【KMP】 poj2406 Power Strings
- poj2406 Power Strings
- poj2406 Power Strings-------KMP
- POJ2406:Power Strings
- poj2406 Power Strings
- poj2406-Power Strings
- [POJ2406] Power Strings[KMP]
- poj2406 Power Strings kmp
- poj2406 Power Strings
- poj2406 Power Strings
- POJ2406 Power Strings 【KMP】
- poj2406 Power Strings (KMP)
- poj2406 Power Strings
- poj2406-Power Strings(kmp)
- poj2406 Power Strings(KMP)
- JVM之方法调用-分派
- Phinecos(洞庭散人) 专注于开源技术的研究与应用 Nehe的OpenGL框架(MFC版)
- 设计模式系列之扉页
- nachos-Runnable和Thread的区别
- Android实现多点触控,自由缩放图片
- POJ2406 Power Strings
- 线性回归(2)缩减系数理解
- 1076. Forwards on Weibo (30)
- numpy
- 我博客里用到的android各种Util文件
- 利用开源软件 Hugin 实现照片的景深合成
- hadoop入门-Mapreduce程序相关日志
- JAVA 入门学习之路-注册登录案例。
- eclipse插件版本或路径更改后的异常处理:java.lang.NoClassDefFoundError: org/eclipse/core/resources/IContainer