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 ...
随机推荐
- CF467D Fedor and Essay 建图DFS
Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...
- [整理]iis7.5下部署MVC5
IIS7.5下部署MVC5 测试环境服务器部署 windows server 2008 r2 1.安装iis 7.5 2.安装 .net framework4.5.1并注册 cd C:\Windows ...
- [译]git revert
git revert git revert用来撤销一个已经提交了的快照. 但不是从项目历史中移除这个commit, 而是生成一个新的commit, 老的commit还是保留在历史项目里面的. 这样做的 ...
- 第一个C++例子
#include <iostream> using namespace std; class Time { private: int hour; int minute; int secon ...
- HttpClient连接池的连接保持、超时和失效机制
HTTP是一种无连接的事务协议,底层使用的还是TCP,连接池复用的就是TCP连接,目的就是在一个TCP连接上进行多次的HTTP请求从而提高性能.每次HTTP请求结束的时候,HttpClient会判断连 ...
- Swift2.1 语法指南——类型转换
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- 信号屏蔽的切换的理解sigsuspend
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h&g ...
- 如何开启telnet 23端口
netstat -tnl|grep 23 查看23端口是否开启 或者 chkconfig --list|grep telnet 检查telnet状态 如果关闭状态, 开启:chkconfig --le ...
- PHPCMS几个有用的全局函数
1.$site_setting = get_site_setting($siteid); 这个get_site_setting()函数读取的是多站点中$siteid站点的相关配置,具体位置在网站根 ...
- gradle类重复的问题解决方法
今天遇到一个gradle的类重复问题,学习到一个命令 gradle -q dependencies,可以查看项目里包的依赖关系,发生这个错误是因为我用了一个相册的项目,这个项目里用到了v4包,我自己的 ...