UVALive - 3026 Period kmp next数组的应用
input
n 2<=n<=1000000
长度为n的字符串,只含小写字母
output
Test case #cas
长度为i时的最小循环串 循环次数(>1)
若没有则不输出
做法:若next数组是连续的整数且next[i]+1是错位部分长度的倍数,则必有循环出现,且循环次数为(i+1)/错位部分长度
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define MAX 100000
#define LL long long
int cas=,T,n,f[MAX*+];
char s[MAX*+];
void getFail(char*s,int*f)
{
f[]=;f[]=;
for(int i=;i<n;i++)
{
int j=f[i];
if((j+)%(i-j)==&&s[i]==s[j]) printf("%d %d\n",i+,(i+)/(i-j));
while(j&&s[i]!=s[j]) j=f[j];
f[i+]=s[i]==s[j]?j+:;
}
}
int main()
{
//freopen("/home/user/桌面/in","r",stdin);
//scanf("%d\n",&T);
while(scanf("%d",&n)==&&n)
{
scanf("%s",s);
printf("Test case #%d\n",cas++);
getFail(s,f);
printf("\n");
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}
UVALive - 3026 Period kmp next数组的应用的更多相关文章
- Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)
参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html 总结一下,如果对于next数组中的 i, 符合 i % ( i - n ...
- 【暑假】[实用数据结构]UVAlive 3026 Period
UVAlive 3026 Period 题目: Period Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- UVALive 3026(KMP算法)
UVALive 3026 KMP中next[]数组的应用: 题意:给出一个字符串,问该字符串每个前缀首字母的位置和该前缀的周期. 思路:裸KMP直接上就是了: 设该字符串为str,str字符串 ...
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- UVALive 3026 Period (KMP算法简介)
kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标 ...
- LA 3026 - Period KMP
看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- UVALIVE 3026 Period
题意:给你一个字符串,问第i位前是否有循环节,若存在,则循环节是多少? 思路:考察失配函数f[i]的意义.只要i%(i-f[i])==0,则循环节长度为i/(i-f[i]).字符在[0,f[i]],[ ...
- Period UVALive - 3026(next数组)
题意: 给出一个长度不超过1000000的字符串S, 对于该字符串的所有前缀求其周期, 如果周期K >= 2输出起始位置是第几个字符和其周期K 解析: 先求next数组 对于每一个位置如果i % ...
- KMP(fail数组应用) LA 3026 Period
题目传送门 题意:(训练指南P213) 求每个前缀的最短循环节 分析:利用失配函数的性质,如果i % (i - fail[i]) == 0,那么正好错位移动一个循环节长度. #include < ...
随机推荐
- springmvc注释
//请求json串(商品信息) 输出json(商品信息) //@RequestBody将请求的商品信息的json串转成itemsCustom对象 //@ResponseBody将itemsCus ...
- centos redis 安装
# wget http://download.redis.io/releases/redis-2.8.6.tar.gz # tar xzf redis-2.8.6.tar.gz # cd redis- ...
- 最有用的Gulp插件汇总
HTML&CSS autoprefixer - parse CSS and add vendor prefixes to rules by Can I Use. gulp-browser-sy ...
- linux安装bind with DLZ <NIOT>
2015年6月11日 1.sudo wget ftp://ftp.isc.org/isc/bind9/9.10.1/bind-9.10.1.tar.gz 或者 使用“rz”命令 2.tar -zxv ...
- python修炼7----迭代器
迭代器 -------------------------------------------------------------------------------- 充电小知识 1.yield-- ...
- LWP::UserAgent介绍2
#这个LWP::UserAgent一般要配合其他模块使用 #比如: #HTTP::Request #HTTP::Cookie #HTTP::Respose #HTTP::Status #LWP::Us ...
- mysql count max min 语句用法
count 用法 求总条数 $sql="select count(*) as total from e_user"; $query = mysql_query($sql, $lin ...
- blog地址
1 Java 设计模式 http://www.cnblogs.com/java-my-life/
- 关于Java泛型的新解
////////////////////////////////////////////////////////////////////////////////为了方便您的观看,请在web版式视图在观 ...
- box2d 已知bug
1.动态刚体与一个与静态刚体重叠的小的感应刚体在contactBegin时,有些时候无法侦测到