Hdu 1358 Period (KMP 求最小循环节)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1358
题目描述:
给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和k。
解题思路:
打表算出next数组,然后对位置i求循环节,如果满足 i % (i - Next[i]) == 0 && Next[i] != 0,所对应的循环节(i - Next[i]), 循环次数是i / (i - Next[i])
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std; #define LL long long
#define maxn 1000010
#define mod 100000007 char a[maxn];
int Next[maxn], m; void get_next (char a[])
{
int i, j;
Next[] = j = -;
i = ; while (i < m)
{
while (j!=- && a[i]!=a[j])
j = Next[j]; Next[++ i] = ++j;
}
} void solve ()
{
for (int i=; i<=m; i++)
{
if (Next[i] != && i % (i - Next[i]) == )
printf ("%d %d\n", i, i / (i - Next[i]));
}
printf ("\n");
}
int main ()
{
int l = ;
while (scanf ("%d", &m), m)
{
scanf ("%s", a);
get_next (a); printf ("Test case #%d\n", ++l); solve ();
}
return ;
}
/*
3
aabccb
*/
Hdu 1358 Period (KMP 求最小循环节)的更多相关文章
- [KMP求最小循环节][HDU1358][Period]
题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...
- HDU 3746 (KMP求最小循环节) Cyclic Nacklace
题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...
- [KMP求最小循环节][HDU3746][Cyclic Nacklace]
题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...
- KMP + 求最小循环节 --- POJ 2406 Power Strings
Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...
- HDU 1358 Period (kmp求循环节)(经典)
<题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...
- 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace
Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...
- KMP + 求最小循环节 --- HUST 1010 - The Minimum Length
The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...
- KMP + 求最小循环节 --- HDU 1358 Period
Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...
- [HDU 1358]Period[kmp求周期]
题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...
随机推荐
- 设计模式学习笔记——State状态模式
从一个类中,将有关状态的处理分离出来,独立成类,并面向接口编程.作用是可以简化代码,避免过多的条件判断:if-else-
- numpy计算
import numpy as np import cv2 from PIL import Image #lenna.jpg # Create a black image #img=np.zeros( ...
- QT实现FTP服务器(二)
QClientThread类的实现: #include "QClientThread.h" #include <QDebug> /******************* ...
- 几个 PHP 的"魔术常量"
__LINE__ 文件中的当前行号. __FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径(如果是符 ...
- 谈谈Paxos一致性算法和一致性这个名词
转自:http://www.cnblogs.com/esingchan/p/3917718.html 维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的& ...
- filename extension
题目描述 Please create a function to extract the filename extension from the given path,return the extra ...
- iOS中区分照片的来源
原理就是通过枚举出每个assets group,然后取得group property,group property是个整数,对应头文件中的一些枚举值.用这个可以判断照片是从哪来的(相机胶卷.照片流.相 ...
- manacher求最长回文子串算法模板
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...
- Echarts饼状图
<head> <meta charset="utf-8"> <title>ECharts</title> <script sr ...
- function.py
#文档字符串 def square(x): 'calculates the square of the number x' return x*x square.__doc__ help(square) ...