Match:Period(POJ 1961)

题目大意:给定一个字符串,要你找到前缀重复了多少次
思路,就是kmp的next数组的简单应用,不要修正next的距离就好了,直接就可以跳转了
PS:喝了点酒用递归实现除法和取余了。。。结果tle不知道怎么回事。。。
#include <iostream>
#include <functional>
#include <algorithm>
#include <string> using namespace std; typedef int Positon;
void Get_Next(const int); static int _next[];
static char str[]; int main(void)
{
int str_length, k_count, if_res;
int case_count = ; while (~scanf("%d", &str_length))
{
if (str_length == )break;
getchar(); scanf("%s", str); Get_Next(str_length);
printf("Test case #%d\n", case_count++);
for (int i = ; i <= str_length; i++)
{
k_count = i / (i - _next[i]);
if_res = i % (i - _next[i]);
if (if_res == && k_count > )
printf("%d %d\n", i, k_count);
}
cout << endl;
}
return EXIT_SUCCESS;
} void Get_Next(const int str_length)
{
Positon i = , k = -;
_next[] = -; while (i < str_length)
{
if (k == - || str[i] == str[k])
{
i++;
k++;
_next[i] = k;
}
else k = _next[k];
}
}

这题用STL的string会卡很长的时间,很奇怪,加了std::ios::sync_with_stdio(false);都没用
Match:Period(POJ 1961)的更多相关文章
- Period POJ - 1961
Period POJ - 1961 时限: 3000MS 内存: 30000KB 64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 For each ...
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- POJ 1961 2406 (KMP,最小循环节,循环周期)
关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406 Powe ...
- poj 1961 Period
Period http://poj.org/problem?id=1961 Time Limit: 3000MS Memory Limit: 30000K Description Fo ...
- KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period
首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- POJ 1961 Period(KMP)
http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...
- POJ 1961 Period KMP算法next数组的应用
题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...
- (简单) POJ 1961 Period,扩展KMP。
Description For each prefix of a given string S with N characters (each character has an ASCII code ...
随机推荐
- virtualBox下面安装linux系统如何共享目录
背景: 系统是win+virtualBox 在虚拟机中安装了linuxMint系统. 如何共享目录呢 第一步: 在虚拟机中安装增强功能 2,在virtualBox中设置共享目录 3,在linux下创建 ...
- PHP: 手把手编写自己的 MVC 框架实例教程
1 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...
- C#深入浅出 修饰符(二)
1.函数参数前的修饰符 params ,ref ,out params修饰的数据类型只能数组,用于参数不固定时:且此参数位于所有形式参数的最后: public static int GetMax(pa ...
- dedecms数据库表前缀不一样怎么还原数据
我们在用dedecms建站时,安装一般都“下一步”直接往下点,这样默认的表前缀是dede_,如果我们要还原从其他地方拷贝过来的数据,一定要注意表头是否一致.如果表头不一样怎么办呢?有两种方法,第一种, ...
- Android连接蓝牙耳机播放音乐
参考: Android实现主动连接蓝牙耳机 具体实现: private static final String TAG = "BluetoothA2DPTest"; private ...
- transition代替简单的animation注意事项
一. transition 和 animation 不支持 Internet Explorer 9,以及更早的版本. 二. 要变化的属性 transition-property:要变化的属性, ...
- Pcserver+oracle10g+rac
成本的相对廉价,技术的成熟,功能的强大此方案将越来越受中小企业的青睐. 一.实验前准备 虚拟机版本:Vwareserver1.0.6 Linux版本:redhat5.5enterprise服务 ...
- canvas游戏小试:画一个按方向键移动的圆点
canvas游戏小试:画一个按方向键移动的圆点 自己对canvas,但又有一颗做游戏的心TT.然后记录一下对canvas的学习吧,用一个按方向键控制的小圆点来做练习.(编程时用了一些es6的语法) ...
- SSH如何通过公钥连接云服务器
导读 通常我们连接远程服务器(linux)windows下通过putty或xshell等工具远程连接.linux下可以直接通过ssh命令连接.其实这两者都是一致的,都是通过ssh协议进行传输. 如果我 ...
- osx xcode 创建python项目
http://stackoverflow.com/questions/5276967/python-in-xcode-7