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 ...
随机推荐
- (准备写)URAL1824 Ifrit Bomber 题解
http://acm.timus.ru/problem.aspx?space=1&num=1824 1824. Ifrit Bomber Time limit: 0.5 second Memo ...
- Spring Quartz定时调度任务配置
applicationContext-quartz.xml定时调度任务启动代码: <?xml version="1.0" encoding="UTF-8" ...
- 根据不同的实体及其ID来获取数据库中的数据
/// <summary> /// 根据不同的实体和其ID来获取信息 /// </summary> /// <typeparam name="T"&g ...
- 系列文章:老项目的#iPhone6与iPhone6Plus适配#(持续更新中,更新日期2014年10月12日 星期日 )
本文永久地址为http://www.cnblogs.com/ChenYilong/p/4020399.html ,转载请注明出处. ********************************** ...
- 【bzoj1864】[ZJOI2006]三色二叉树
题目描述 输入 仅有一行,不超过500000个字符,表示一个二叉树序列. 输出 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. 样例输入 1122002010 样例输出 ...
- Windows 7 共享文件夹 给 VirtualBox 中的 Ubuntu 14
操作步骤如下: 1.打开虚拟机中的 Ubuntu 系统: 2.安装“增强工具” 设备 -> 安装增强工具 3.设置“共享文件夹” 控制 -> 设置 -> 添加共享文件夹 -> ...
- mysql 修改表结构
alter table 表名 modify column 字段名 varchar(数量); 将varchar(50)改为255 alter table 表名 modify column 字段名 var ...
- go outside @ CULTS LYRICS
I really want to go out I really want to go outside and stop to see your day You really want to hole ...
- CF570D——Tree Requests
1.题目大意:给你一棵树,每个点有一个字符,然后我们定义1的深度是1,然后1的儿子深度是2... 然后有一个询问,询问以i为根节点的子树,然后深度是k的那层的所有字符,可否组成一个回文串 2.分析:首 ...
- CentOS6系升级Python2.7版本
安装前准备 本实例以CentOS6.7为例 [root@E tools]# uname -r 2.6.32-431.23.3.el6.x86_64 [root@E tools]# uname -m x ...