https://vjudge.net/problem/UVA-1328

题意

求每个前缀的最小循环节,要求至少循环两次且为完整的。

分析

求next数组,i-next[i]即为前缀i的最小循环节,再判断一下限制条件即可。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
//#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
//const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + ;
const int maxm = 4e5 +;
const int mod = ;
const int sigma_size = ; char s[maxn];
int f[maxn];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("input.txt", "w", stdout);
#endif
int cas=;
int T;
int n;
while(~scanf("%d",&n)&&n){
scanf("%s",s);
f[]=f[]=;
for(int i=;i<n;i++){
int j=f[i];
while(j&&s[i]!=s[j]) j=f[j];
f[i+]=s[i]==s[j]?j+:;
}
printf("Test case #%d\n",cas++);
for(int i=;i<=n;i++){
if(f[i]>&&i%(i-f[i])==)
printf("%d %d\n",i,i/(i-f[i]));
}
printf("\n");
}
return ;
}

UVA - 1328 Period(循环节kmp)的更多相关文章

  1. UVa 1328 Period

    数据范围较大,故用KMP求循环节 之后由小到大枚举长度范围,若该长度下有循环节就输出答案 还要注意输出格式.之前测试时候连着一串presentation error也是悲伤 #include<b ...

  2. Colossal Fibonacci Numbers! UVA 11582 寻找循环节

    /** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...

  3. UVA 1328 - Period KMP

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131 题意:给出一个长度为n的字符串,要求找到一些i,满足说从1 ...

  4. Period---hdu1358(循环节 kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题意 :求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k(k>1); 例如: ...

  5. UVAlive 3026 KMP 最小循环节

    KMP算法: 一:next数组:next[i]就是前面长度为i的字符串前缀和后缀相等的最大长度,也即索引为i的字符失配时的前缀函数. 二:KMP模板 /* pku3461(Oulipo), hdu17 ...

  6. hdu3746(kmp最小循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 题意:问在一个字符串末尾加上多少个字符能使得这的字符串首尾相连后能够循环 题解:就是利用next ...

  7. poj1961 Period kmp解决找字符串的最小循环节

    /** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...

  8. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  9. UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)

    题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...

随机推荐

  1. MT【259】2016天津压轴题之最佳逼近

    (2016天津压轴题)设函数$f(x)=(x-1)^3-ax-b,x\in R$, 其中$a,b\in R$(1)求$f(x)$的单调区间.(2)若$f(x)$存在极值点$x_0$,且$f(x_1)= ...

  2. [ZJOI2016]大森林(LCT)

    题目描述 小Y家里有一个大森林,里面有n棵树,编号从1到n.一开始这些树都只是树苗,只有一个节点,标号为1.这些树都有一个特殊的节点,我们称之为生长节点,这些节点有生长出子节点的能力. 小Y掌握了一种 ...

  3. Centos 6.x/7.x yum安装php5.6.X

    鉴于Centos 默认yum源的php版本太低了,手动编译安装又有点一些麻烦,那么如何采用Yum安装的方案安装最新版呢.那么,今天我们就来学习下如何用yum安装php最新版. 1.检查当前安装的PHP ...

  4. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...

  5. Quick Guide to Microservices with Spring Boot 2.0, Eureka and Spring Cloud

    https://piotrminkowski.wordpress.com/2018/04/26/quick-guide-to-microservices-with-spring-boot-2-0-eu ...

  6. 小R的树(权限题)

    解:考场上爆0了...... 回想怎么求两个排列的最长公共子序列. 回想怎么求1~n每个数恰出现两次的两个序列的最长公共子序列.就是每个数替换为它在另一个序列里的出现位置,降序. 所以我们可以把这每个 ...

  7. A1131. Subway Map (30)

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  8. Django session相关操作

    Django session 是存储在数据库中的所以要先跟数据库建立连接 本连接有Django跟数据库建立连接的操作:https://www.cnblogs.com/Niuxingyu/p/10296 ...

  9. bfs退出条件

    找到结果即退出? Possibly Not! 如拓展(x)将导致距离增加0(x)或1(x+1),[这种情况用优先队列] 那么该拓展得到的结果也许并不是最优的,后面还有x. 所以最好的是遍历(不知怎么说 ...

  10. 多文件协作,extern、static、头文件

    多个cpp文件协同工作.使用外部函数.变量时,必须先声明再使用.声明外部函数(一般在main.cpp中),extern可省略(主函数中默认可访问外部函数)extern void RectArea(); ...