题意:

  给出一个长度不超过1000000的字符串S, 对于该字符串的所有前缀求其周期, 如果周期K >= 2输出起始位置是第几个字符和其周期K

解析:

  先求next数组

  对于每一个位置如果i % (i-next[i]) == 0 && i /(i - next[i]) >= 2 则成立 即i-next[i] 为其最短循环节

  周期为i /(i - next[i])

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int nex[maxn];
int tlen;
string str;
void get_nex()
{
mem(nex, );
int j, k;
j = , k = -, nex[] = -;
while(j < tlen)
{
if(k == - || str[j] == str[k])
nex[++j] = ++k;
else
k = nex[k];
}
} int main()
{
int n, kase = ;
while(cin>> n && n)
{
cin>> str;
tlen = str.size();
get_nex();
printf("Test case #%d\n", ++kase);
for(int i=; i<=n; i++)
{
if(i % (i-nex[i]) == && i /(i - nex[i]) >= )
cout<< i << " " << i/(i - nex[i]) <<endl;
}
cout<< endl;
} return ;
}

Period UVALive - 3026(next数组)的更多相关文章

  1. Period UVALive - 3026

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...

  2. 【暑假】[实用数据结构]UVAlive 3026 Period

    UVAlive 3026 Period 题目: Period   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  3. UVALive 3026(KMP算法)

    UVALive 3026     KMP中next[]数组的应用: 题意:给出一个字符串,问该字符串每个前缀首字母的位置和该前缀的周期. 思路:裸KMP直接上就是了: 设该字符串为str,str字符串 ...

  4. UVALive - 3026 Period kmp next数组的应用

    input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法 ...

  5. UVALive - 3026:Period

    用KMP里面的next数组即可,原理就是next数组的原理 #include<cstdio> #include<cstdlib> #include<algorithm&g ...

  6. UVALive 3026 Period (KMP算法简介)

    kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标 ...

  7. Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)

    参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html 总结一下,如果对于next数组中的 i, 符合 i % ( i - n ...

  8. UVALIVE 3026 Period

    题意:给你一个字符串,问第i位前是否有循环节,若存在,则循环节是多少? 思路:考察失配函数f[i]的意义.只要i%(i-f[i])==0,则循环节长度为i/(i-f[i]).字符在[0,f[i]],[ ...

  9. FZU1901 Period II —— KMP next数组

    题目链接:https://vjudge.net/problem/FZU-1901  Problem 1901 Period II Accept: 575    Submit: 1495Time Lim ...

随机推荐

  1. SaltStack入门篇(二)之远程执行和配置管理

    1.远程执行 第一条命令: [root@linux-node1 master]# salt '*' test.ping linux-node2.example.com: True linux-node ...

  2. 利用Anaconda进行python爬虫环境的配置-安装scrapy

    1.下载Anaconda,下载地址:https://www.continuum.io/downloads 2.安装anaconda. 3.安装scrapy

  3. 修改Qt源码遇到的问题

    1.修改源码后用新的Qt版本调试Qt工程,程序直接崩溃:

  4. HCIE理论-IPV6

    ipv4与ipv6的对比 IPv4 :32 bit 点分十进制 192.168.1.1 2^32=42.9亿 ipv4地址不足IPv6 :128 bit 十六进制 2^128 冒号分十六进制ipv4 ...

  5. My status

    I haven‘t any one who is strong relationship with me. My skill is normal. I'm not interesting in neg ...

  6. python环境通过selenium实现自动化web登陆及终端邀请

    自动化主要的就是识别对象,可以在网上搜到各种各样的方法,自行百度.下面仅附上一个简单的例子. 环境搭建参考如下链接: https://www.cnblogs.com/hepeilinnow/p/101 ...

  7. python序列和其它类型的比较

    序列对象可以与相同类型的其他对象比较.它们使用 字典顺序 进行比较:首先比较两个python序列的第一个元素,如果不同,那么这就决定了比较操作的结果.如果它们相同,就再比较每个序列的第二个元素,以此类 ...

  8. 阿里云服务器Centos上Apache安装SSL证书配置Https

    首先我们先去阿里云申请一个免费的SSL证书(https://common-buy.aliyun.com/?spm=5176.7968328.1266638..5e971232BzMSp5&co ...

  9. JAVA基础学习之路(十一)引用传递

    引用传递: 不同栈内存可以指向同一块堆内存,不同栈内存可以对一块堆内存进行修改 范例一: class Message { private int num = 10; public Message(in ...

  10. 设置PNG图片DPI 信息,保存为PDF(使用Magick),与OpenCV转换

    目录 任务描述 解决方案 Magick++ Talk is cheap, show me the code. 与 Opencv 配合 相关链接 任务描述 我有这样一个需求,读取一张格式为PNG 或者 ...