[POJ1961 Period]
[题目来源]:Southeastern Europe 2004
[关键字]:字符串
[题目大意]:给定一个字符串,求到哪一位时的字串是前几位循环组成的,并求出循环次数。
//=====================================================================================================
[分析]:利用KMP来求解。在KMP中有一个p数组记录当前字符的它的上一个位置,且保证这一段一定是来连续的。所以如果i处有循环则i到p[i]的长度即为循环节长度,所以如果i mod (i-p[i])=0则说明有循环,i div (i-p[i])就是循环次数。
[代码]:
1 var 2 n, tc: longint; 3 s: ansistring; 4 p: array[0..2000010] of longint; 5 6 procedure init; 7 var 8 i: longint; 9 begin10 readln(n);11 if n = 0 then halt;12 s := '';13 readln(s);14 end;15 16 procedure work;17 var18 i, k, temp: longint;19 begin20 fillchar(p,sizeof(p),0);21 k := 0;22 p[1] := 0;23 for i := 2 to n do24 begin25 while (k > 0) and (s[i] <> s[k+1]) do k := p[k];26 if s[i] = s[k+1] then inc(k);27 p[i] := k;28 end;29 for i := 1 to n do30 if p[i] <> 0 then31 begin32 temp := i-p[i];33 if i mod temp = 0 then34 writeln(i,'',i div temp);35 end;36 end;37 38 begin39 tc := 0;40 while 1 = 1 do41 begin42 inc(tc);43 init;44 writeln('Test case #',tc);45 work;46 writeln;47 end;48 end.
转载于:https://www.cnblogs.com/procedure2012/archive/2011/11/02/2232908.html
[POJ1961 Period]相关推荐
- 【读书笔记】《算法竞赛进阶指南》读书笔记——0x10基本数据结构
to do(perhaps never) CH1401 后缀数组 所有课后题 栈 例题:HDU4699 Editor 维护一个整数序列的编辑器,支持以下五种操作: I x:在当前光标位置处插入一个整数 ...
- [LA3026]Period
[LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...
- 时间周期 java_周期和持续时间 / Period and Duration
# 周期和持续时间 / Period and Duration 当您编写代码来指定一段时间时,请使用最符合您需要的类或方法: Duration 类, Period 类或 ChronoUnit.betw ...
- java between_Java8 Period.between方法坑及注意事项
在使用Java8 新特性中关于Period.between的方法时需注意该方法获取日期的区间问题. @Test public void test1(){ LocalDate from = LocalD ...
- HDU 5908 Abelian Period 暴力
Abelian Period 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5908 Description Let S be a number st ...
- Java 8 – Period and Duration examples
Few examples to show you how to use Java 8 Duration, Period and ChronoUnit objects to find out the d ...
- Back Stub Period
http://www.investopedia.com/terms/p/portfolio.asp#axzz1lnDvrq00 The last interim period in the life ...
- JDK8中的新时间API:Duration Period和ChronoUnit介绍
文章目录 简介 Duration Period ChronoUnit 简介 在JDK8中,引入了三个非常有用的时间相关的API:Duration,Period和ChronoUnit. 他们都是用来对时 ...
- Java8中使用以下类计算日期时间差:Period、Duration、ChronoUnit
一.简述 在Java8中,我们可以使用以下类来计算日期时间差异: 1.Period 2.Duration 3.ChronoUnit 二.Period类 主要是Period类方法getYears(),g ...
最新文章
- python API docs
- JAVA反射+SOCKET实现远程方法调用
- 构造函数,对象原型,实例对象三者之间的关系
- 我有单片机,你有酒吗?
- 怎样通过Java程序提交yarn的mapreduce计算任务
- keras中一个LSTM的具体例子
- Zookeeper及客户端的基本操作
- java当中的定时器的4种使用方式
- ubuntu oracle 工具,ubuntu上安装Oracle InstantClient
- I2S/PCM协议及TDM模式详解
- 【MATLAB】报错:数组索引必须为正整数或逻辑值
- 数学建模-自来水管道铺设问题
- 微信支付之App支付
- Objective-C的方法替换
- one-hot表示和分布式表示
- Android-Rxjava 常用操作符
- micropython stm32f429 tft_基于STM32F429的TFT0.96屏幕驱动
- mysql的slow_log表_【转载】mysql 开启慢查询 清空slow_log日志或者slow_log表
- 【经验】CV or NLP研究生如何学才能毕业找到一份好工作?
- 在已安装win10环境中利用EasyBCD引导安装Ubuntu16.04
热门文章
- Pocket英语语法---三、英语动词的特点是什么
- 《UG NX8.0中文版完全自学手册》一2.4 布局
- SuperMap IS.NET保存地图之Ajax篇(转)
- 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之26---礼品卡支付...
- c语言位运算符怎么用,傻傻分不清
- 问题 E: 编写函数:求数组中的最小值 (Append Code)
- uc3842开关电源电路图_详解6款简单的开关电源电路设计原理图
- python如何调用阿里云接口_python 调用阿里云云解析api添加记录
- has install-snap change in progress问题
- 基于 Flow 的 NFT 交易平台 Tibles 完成 114 万美元种子轮融资