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 < ...
随机推荐
- CentOS 7将Python 2.X.X升级到Python 3.X.X
# cd /usr/local/src/ # ls Python-.tgz # .tgz # ls Python- Python-.tgz # cd Python-/ # yum install gc ...
- sqlmap基础使用
测试许多款 sql注入工具 最终还是发现 sqlmap 最为强悍 谁用谁知道!赶紧抛弃掉手上一大堆 sql 注入工具吧 : )测试环境:ubuntu 10.10 & windows 7(x64 ...
- Linux 服务器系统监控脚本 Shell【转】
转自: Linux 服务器系统监控脚本 Shell - 今日头条(www.toutiao.com)http://www.toutiao.com/i6373134402163048961/ 本程序在Ce ...
- 批处理改hosts
@echo off color 0F @attrib -r "%windir%\system32\drivers\etc\hosts" @echo ######测试配置 beg & ...
- Multidimensional Array And an Array of Arrays
One is an array of arrays, and one is a 2d array. The former can be jagged, the latter is uniform. T ...
- java导出数据Excel总结
//创建获取到JFileChooser的文件名的JTextField public JTextField getTextField(Container c){ JTextField textField ...
- CMake如何执行shell命令
我在cmake编译后想执行一些特定的shell命令(执行.lcov收集代码覆盖报告等),我又不想写到XX.sh的shell脚本中,如何直接通过CMake执行shell命令呢? 在网上翻江倒海了一下,找 ...
- 微信小程序Tabbar文字在真机不显示
按照官方文档在json中定义好了Tabbar后,在模拟器上显示没问题,而在真机上不显示Tabar文字. 让我很苦笑不得的原因是: 在app.json定义Tabbar文字选中态和非选中态颜色时我用了英文 ...
- iOS开发app自动更新的实现
#define kStoreAppId @“xxxxxxxxx” // (appid数字串) -(void)checkAppUpdate { NSDictionary *infoDict = [[NS ...
- Sun jdk, Openjdk, Icedtea jdk关系
转自: http://blog.chinaunix.net/uid-20648944-id-3204527.html Sun jdk与Openjdk版本发展历史如下图所示: 1. Openjdk ...