poj 1961 Period 【KMP-next前缀数组的应用】
题目地址:http://poj.org/problem?id=1961
Sample Input
3
aaa
12
aabaabaabaab
0
Sample Output
Test case #1
2 2
3 3 Test case #2
2 2
6 2
9 3
12 4 题目分析:给你一个字符串,最大长度1百万。输出是:以第1组样例解释,在aaa的字符串中,长度为2时,存在2个循环节a。当长度为3时,存在3个循环节a。
以第二组样例解释,当长度为2时,存在2个循环节a。当长度为6时,存在2个循环节aab。当长度为9时,存在3个循环节aab。依次类推下去。
此题就是poj 2406的难度加强版本。
code:
#include <stdio.h>
#include <string.h>
#include <stdio.h> char s[1000000+10];
int next[1000000+10]; void get_next(int len)
{
int i=0, j=-1;
next[0]=-1;
while(i!=len)
{
if(j==-1 || s[i]==s[j])
next[++i]=++j;
else
j=next[j];
}
} int main()
{
int n;
int i, j, len;
int cnt=1;
while(scanf("%d%*c", &n)!=EOF)
{
if(n==0) break;
scanf("%s", s);
get_next(n);
printf("Test case #%d\n", cnt++);
for(i=1; i<=n; i++){
len=i-next[i];//计算的循环节的长度
if(i!=len && i%len==0){
printf("%d %d\n", i, i/len);
}
}
printf("\n");
}
return 0;
}
poj 1961 Period 【KMP-next前缀数组的应用】的更多相关文章
- 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算法之next数组的应用
题意:给一个长度为n的字符串,如果它长度为l(2 <= l <= n)的前缀部分是由一些相同的字符串相接而成,输出前缀的长度l和长度为l时字符串重复的最大次数. 例如字符串为: aaaba ...
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 14653 Accepted: 6965 Descripti ...
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- 转载-KMP算法前缀数组优雅实现
转自:http://www.cnblogs.com/10jschen/archive/2012/08/21/2648451.html 我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见 ...
- POJ-2752(KMP算法+前缀数组的应用)
Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来 ...
- 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。
Description For each prefix of a given string S with N characters (each character has an ASCII code ...
随机推荐
- android Material design是什么
Material design概述: Material design是一套UI样式标准,应该会提供一些新的API这写API包含了以下五大模块内容,分别是: Material Theme New Wid ...
- sourcenav安装
$ ./configure之后会出现 configure: error: ./configure failed for unixconfigure: error: ./configure failed ...
- hdu 4576(概率dp+滚动数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 思路:由于每次从某一位置到达另一位置的概率为0.5,因此我们用dp[i][j]表示第i次操作落在 ...
- final和finally面试时最好的回答
finally: try块必须和catch块或和finally同在,不能单独存在,二者必须出现一个. finally块总会执行,不论是否有错误出现.但是若try语句块或会执行的catch语句块使用了J ...
- Unity3d 重力感应
Input.acceleration 加速度 最近一次测量的设备在三维空间中的线性加速度(只读); void Update () { v3=Input.acceleration; } void OnG ...
- FZU 2099 魔法阵(计算几何)
Problem 2099 魔法阵 Accept: 120 Submit: 289 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- Hystrix参数说明
参数配置 参数说明 值 备注 groupKey productStockOpLog group标识,一个group使用一个线程池 commandKey addProductStockOpLog com ...
- 统计TCP网络连接情况
#!/bin/bash metric=$1 tmp_file=/tmp/tcp_status.txt /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a i ...
- debug_backtrace final catch
<?php function backtrace_str(){ $str = ''; $w = 0; $backtrace = debug_backtrace(); foreach($backt ...
- javascript实例:两种方式实现tab栏选项卡
方法1: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...