Period

  题目大意:给定一个字符串,要你找到前缀重复了多少次

  思路,就是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)的更多相关文章

  1. Period POJ - 1961

    Period POJ - 1961 时限: 3000MS   内存: 30000KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 For each ...

  2. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

  3. POJ 1961 2406 (KMP,最小循环节,循环周期)

    关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406  Powe ...

  4. poj 1961 Period

    Period http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K       Description Fo ...

  5. 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中的出现位置或者是次数 这个出现的次数是可 ...

  6. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

  7. POJ 1961 Period(KMP)

    http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...

  8. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

  9. (简单) POJ 1961 Period,扩展KMP。

    Description For each prefix of a given string S with N characters (each character has an ASCII code ...

随机推荐

  1. php 遍历目录下的所以文件和文件夹

    <?php/** * 遍历文件夹和文件列 * @author lizhiming * @date 2016/06/30 */define('DS', DIRECTORY_SEPARATOR); ...

  2. oracle中substr与instr

    在oracle中,可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符.在一个字符串中查找指定的字符,返回被查找到的指定字符的位置. 语法: Instr(sourceString,de ...

  3. vector族函数

     本文原创,转载请注明出处,本人Q1273314690 vector(mode = "logical", length = 0) as.vector(x, mode = " ...

  4. VTK初学一,a_Vertex图形点的绘制

    系统:Win8.1 QT版本:2.4.2,Mingw VTK版本:6.3 2. main.cpp #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #incl ...

  5. go学习与记录

    搭建go开发环境:http://studygolang.com/articles/5406 日志相关:https://github.com/hpcloud/tail go定时器:http://stud ...

  6. Linq查询操作语句学习

    对于一个集合,我们通常会用foreach或者for循环来判断查找里面的元素. 但这种方法通常会看起来比较复杂,我们可以使用linq. Linq允许编写C#代码以查询数据库相同的方式操作内存数据(写法类 ...

  7. HTML5 之Canvas 绘制时钟 Demo

    <!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...

  8. HDOJ 1561 The more, The Better

    树形DP.... The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. 【C语言入门教程】7.4 共用体

    7.4 共用体 共用体又称为联合体,是由不同的数据类型组成的一个整体.与结构体不同的是,共用体每次只能使用其中一个成员.结构体的总长度是结构体所有成员长度之和,共用体的总长度是其中最长一个数据类型的长 ...

  10. BeanFactory和ApplicationContext的区别

     1.BeanFactory和ApplicationContext的异同点: 相同点:     两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比 ...