Period UVALive - 3026
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and
126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 ≤ i ≤ N)
we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be
written as AK, that is A concatenated K times, for some string A. Of course, we also want to know
the period K.
Input
The input file consists of several test cases. Each test case consists of two lines. The first one contains
N (2 ≤ N ≤ 1000000) the size of the string S. The second line contains the string S. The input file
ends with a line, having the number zero on it.
Output
For each test case, output ‘Test case #’ and the consecutive test case number on a single line; then, for
each prefix with length i that has a period K > 1, output the prefix size i and the period K separated
by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
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
给定一个长度为n的字符串s,求它每个前缀的最短循环节。换句话说,对于每个i,求一个最大的整数k(如果k 存在),使得s的前i哥字符组成的前缀是某个字符串重复k次得到。输出所有存在k的i和对应的k
比如对于字符串aabaabaabaab,只有当i=2,6,9,12时k存在,且分别为2,2,3,4
#include <algorithm>
#include <iostream>
#include <string>
#include<cstdio>
#include <vector>
#define maxn 1000001
using namespace std;
char p[maxn];
int f[maxn];
int main()
{
int n,kase = ;
while(cin >> n)
{
if(!n)
break;
cin >> p;
f[]=,f[]=;//递推边界初值
for(int i=;i<n;i++)
{
int j = f[i];
while(j&&p[i]!=p[j])
j = f[j];
f[i+] = ((p[i]==p[j])?j+:);
}
printf("Test case #%d\n",++kase);
for(int i=;i<=n;i++)
if(f[i]>&&i%(i-f[i])==)
printf("%d %d\n",i,i/(i-f[i]));//找到i和k
printf("\n");
}
return ;
}
Period UVALive - 3026的更多相关文章
- Period UVALive - 3026(next数组)
题意: 给出一个长度不超过1000000的字符串S, 对于该字符串的所有前缀求其周期, 如果周期K >= 2输出起始位置是第几个字符和其周期K 解析: 先求next数组 对于每一个位置如果i % ...
- 【暑假】[实用数据结构]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字符串 ...
- UVALIVE 3026 Period
题意:给你一个字符串,问第i位前是否有循环节,若存在,则循环节是多少? 思路:考察失配函数f[i]的意义.只要i%(i-f[i])==0,则循环节长度为i/(i-f[i]).字符在[0,f[i]],[ ...
- UVALive - 3026 Period kmp next数组的应用
input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法 ...
- UVALive - 3026:Period
用KMP里面的next数组即可,原理就是next数组的原理 #include<cstdio> #include<cstdlib> #include<algorithm&g ...
- UVALive 3026 Period (KMP算法简介)
kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标 ...
- Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)
参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html 总结一下,如果对于next数组中的 i, 符合 i % ( i - n ...
- UVAlive 3026 KMP 最小循环节
KMP算法: 一:next数组:next[i]就是前面长度为i的字符串前缀和后缀相等的最大长度,也即索引为i的字符失配时的前缀函数. 二:KMP模板 /* pku3461(Oulipo), hdu17 ...
随机推荐
- java中System.out.println()打印输出结果
疑点:syso()是打印输出语句,打印的是什么? syso()不同情况下打印输出的结果不一样: 1. package com.briup; public class Syso { public sta ...
- MySQL-5.7.21非图形化下载、安装、连接问题记录
1.安装包下载链接:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-winx64.zip 官网:https://www.mysql.co ...
- Java性能调优的11个实用技巧
译文出处: ITeye 原文出处:dzone 大多数开发人员认为性能优化是个比较复杂的问题,需要大量的经验和知识.是的,这并不没有错.诚然,优化应用程序以获得最好的性能并不是一件容易的事情,但这 ...
- WPF中如何禁用空格键(或其他键)
在选择的控件中添加KeyDown event method private void OnKeyDown(object sender, KeyEventArgs e){ if (e.Key == Ke ...
- JavaScript数据结构——树的实现
在计算机科学中,树是一种十分重要的数据结构.树被描述为一种分层数据抽象模型,常用来描述数据间的层级关系和组织结构.树也是一种非顺序的数据结构.下图展示了树的定义: 在介绍如何用JavaScript实现 ...
- 10分钟安装Elasticsearch
关注公众号 itweknow,回复"ES"获取<Elasticsearch权威指南 中文版>. 最近在尝试着搭建一个ELK(一个开源的实时日志分析平台),而本文所讲的E ...
- Mybatis的工作流程
MyBatis工作流程 1:加载配置文件(mybatis-config.xml . *...Mapper.xml)并初始化, 将SQL的配置信息加载成为一个个MappedStatement对象(包括了 ...
- 迁移学习(Transformer),面试看这些就够了!(附代码)
1. 什么是迁移学习 迁移学习(Transformer Learning)是一种机器学习方法,就是把为任务 A 开发的模型作为初始点,重新使用在为任务 B 开发模型的过程中.迁移学习是通过从已学习的相 ...
- abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十四)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- 如何:从 bool? 安全地强制转换为 bool(C# 编程指南)
bool? 可以为 null 的类型可以包含三个不同的值:true.false 和 null.因此,bool? 类型不能用于条件语句,如 if.for 或 while.例如,此代码无法编译,并将报告编 ...