UVa 10298 - Power Strings
题目:求一个串的最大的循环次数。
分析:dp。KMP,字符串。这里利用KMP算法。
KMP的next函数是跳跃到近期的串的递归结构位置(串元素取值0 ~ len-1);
由KMP过程可知:
假设存在循环节,则S[0 ~ next[len]-1] 与 S[len-next[len] ~ len-1]相匹配;
则S[next[len] ~ len-1]就是循环节(且最小)。否则next[len]为0;
因此,最大循环次数为len/(len-next[len])。最小循环节为S[next[len] ~ len-1]。
说明:强大的KMP(⊙_⊙)。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio> using namespace std; char str[1000004];
int next[1000004]; void getnext( char *T, int L )
{
next[0] = -1;
int i = 0,j = -1;
while ( i < L ) {
if ( j == -1 || T[i] == T[j] ) {
i ++; j ++;
next[i] = j;
}else j = next[j];
}
} int main()
{
while ( scanf("%s",str) && strcmp( str, "." ) ) {
int len = strlen(str);
getnext( str, len );
printf("%d\n",len/(len-next[len]));
}
return 0;
}
UVa 10298 - Power Strings的更多相关文章
- UVA - 10298 Power Strings (KMP求字符串循环节)
Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...
- UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)
题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...
- POJ 2406 Power Strings (KMP)
Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...
- POJ 2406 Power Strings
F - Power Strings Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- poj 2406:Power Strings(KMP算法,next[]数组的理解)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30069 Accepted: 12553 D ...
- POJ 2406:Power Strings
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 41252 Accepted: 17152 D ...
- E - Power Strings,求最小周期串
E - Power Strings Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- poj 2406 Power Strings kmp算法
点击打开链接 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27368 Accepted: ...
- poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)
http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submiss ...
随机推荐
- Squid普通代理&&透明代理&&反向代理学习
普通代理 背景 ...
- TJ Holowaychuk是怎样学习编程的?
TJ Holowaychuk是怎样学习编程的? 学习了:https://blog.csdn.net/wozaixiaoximen/article/details/49507111 Q:TJ Holow ...
- BSTR
BSTR A BSTR (Basic string or binary string) is a string data type that is used by COM, Automation, a ...
- 设置网站expires和max-age属性
转:http://www.zicheng.net/article/982022.htm 在使用百度站长工具测试网站优化建议时,在 设置静态内容缓存时间 栏目里,会提示 类似 FAILED - (未设置 ...
- AutoPlay Menu Builder入门教程
1 拖动窗口可以设置主界面的窗口大小,在下面有版面预览 2 常用东西介绍.右侧的素材库除了按钮还有图像,背景,音乐等.使用按钮的时候选中需要的按钮样式,双击即可上屏.图形按钮即使可以使用图像作为背景的 ...
- c#实现验证码功能(多种模式下分别实现验证功能)详细,带注释
网上找了很多验证相关的代码,发现有很多瑕疵.现在本人整理测试了一个实现验证码功能的代码,里面有纯数字,纯英文,英文和数字混合等三种模式.并且在必要地方都已经备有注释,希望可以帮到那些需要的人. 验证码 ...
- hibernate:MySQL No Dialect mapping for JDBC type: -1
出处:(hibernate中使用原生的sql语句,报如下错误:) MySQL No Dialect mapping for JDBC type: -1 代码: List list = session. ...
- 第一节,学习cocos2d-x的前期准备
1,我用的mac系统,在mac系统上装上cocos2d-x的模板 2,用doxygen工具装上API,这个非常重要,没有API的开发不叫开发,因此我们要习惯看API 3,知道怎么查看cocos2d-x ...
- js toSting方法实现
function toString (val: any): string { return val == null ? '' : typeof val === 'object' ? JSON.stri ...
- VS2015 正式版中为什么没有了函数前面引用提示了?
HttpClient _httpClient = new HttpClient(); var clientId = Config.GetValue("AuthUser"); var ...