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 < ...
随机推荐
- 学习任务在继续...css...
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- EFI、GPT和BIOS、MBR
用了数十年的PC机主板架构是BIOS模式.但在2004年,微软和英特尔共同推出一种名为可扩展固件接口(EFI)的主板升级换代方案.EFI,即可扩展固件接口(Extensible Firmware In ...
- 关于css起名
作者:词晖链接:https://www.zhihu.com/question/19586885/answer/48933504来源:知乎著作权归作者所有,转载请联系作者获得授权. CSS书写顺序 1. ...
- 判断iPhone设备是carplay和iPod的方法
Carplay 参考代码: int UdevIsCarplay(int nVid, int nPid)//int UsbModeSwitch(pUdevStatus pStatus) { ; if ( ...
- Jquery跨域调用后台方法
//前端JS function CallHandlerByJquery() { var url = "http://" + window.location.hostname + & ...
- GitHub上优秀的ThirdParty
仅为个人记录. 1.SwiftyJSON GitHub地址:https://github.com/SwiftyJSON/SwiftyJSON 2.CryptoSwift GitHub地址:https: ...
- read/sysread区别
use warnings; use strict; my $readbuff; my $sysreadbuff; ); print "read get:$readbuff\n"; ...
- foreach 循环的应用传值
$arr=array(1,5,8,8,9);foreach ($arr as $key => $value) { //这里可以一边改外面$arr的值一边下一步循环 $value=++$value ...
- MySQL的备份和恢复
MySQL的备份和恢复 备份数据:mysqldump –uroot –p123456 dbname table [option] > dbname.sql mysqldump常用参数option ...
- Python基础篇-day6
本节简介: 1.模块1.1 时间模块1.2 random模块1.3 shutil模块1.4 shelve模块1.5 XML模块1.6 ConfigParser模块1.7 hashlib模块1.8 lo ...