HDU-1358-Period(KMP, 循环节)
链接:
https://vjudge.net/problem/HDU-1358#author=0
题意:
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 A K , that is A concatenated K times, for some string A. Of course, we also want to know the period K.
思路:
先求出Next数组, 然后遍历一遍, 求出每个前缀的循环节, 再判断是否满足即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10;
int Next[MAXN];
char s[MAXN], p[MAXN];
void GetNext()
{
int len = strlen(p);
Next[0] = -1;
int j = 0;
int k = -1;
while (j < len)
{
if (k == -1 || p[k] == p[j])
{
++k;
++j;
Next[j] = k;
}
else
k = Next[k];
}
}
int main()
{
int cnt = 0, n;
while (~scanf("%d", &n) && n)
{
scanf("%s", p);
GetNext();
printf("Test case #%d\n", ++cnt);
for (int i = 2;i <= n;i++)
{
int len = i-Next[i];
if (len != i && i%len == 0)
printf("%d %d\n", i, i/len);
}
puts("");
}
return 0;
}
HDU-1358-Period(KMP, 循环节)的更多相关文章
- HDU 1358 Period (kmp求循环节)(经典)
<题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...
- HDU 1358 Period(KMP计算周期)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- Period(KMP,循环节问题)
题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...
- HDU 1358 Period KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...
- hdu 1358 period KMP入门
Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...
- Hdu 1358 Period (KMP 求最小循环节)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...
- hdu 1358 Period(KMP入门题)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- [HDU 1358]Period[kmp求周期]
题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...
- HDU 1358 Period(KMP+最小循环节)题解
思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...
随机推荐
- 2019牛客暑期多校训练营(第六场)-E Androgynos (构造题)
题目链接:https://ac.nowcoder.com/acm/contest/886/E 题意:给出n,求能否构造一个含n个顶点的简单无向图,且它的补图与它同构. 思路:首先要满足与其补图同构,同 ...
- Highest Frequency Letters
Given a list of strings, output the most frequent characters that are in the same group as the lette ...
- Wordpress 所有 hook 钩子
muplugins_loaded 在必须使用的插件加载之后. registered_taxonomy 对于类别,post_tag 等 Registered_post_type 用于帖子,页面等 plu ...
- oracle报:ORA-01034和ORA-27101的解决办法
出现ORA-01034和ORA-27101的原因是多方面的:主要是oracle当前的服务不可用,shared memory realm does not exist,是因为oracle没有启动或没有正 ...
- 切割nginx access日志
#!/bin/bash nginx_root=/www/server/nginx log_path=/www/wwwlogs yesterday=`date -d "-1 day" ...
- sqlalchemy定义mysql时间戳字段
update_time = Column(TIMESTAMP, nullable=False, comment='更新时间戳', server_default=text('CURRENT_TIMEST ...
- python中while循环打印星星的四种形状
在控制台连续输出五行*,每一行星号数量一次递增 * ** *** **** ***** #1.定义一个行计数器 row = 1 while row <= 5: #定义一个列计数器 col = 1 ...
- 安装calico
安装docker:https://www.cnblogs.com/cjsblogs/p/8717304.html 安装etcd集群:https://www.cnblogs.com/cjsblogs/p ...
- git的常用指令(一)
1. 查看git远程的所有分支 git branch -a 2.查看本地已有的分支 git branch 3.本地检出一个新的分支并推送到远程仓库 一).创建本地分支 git checkout -b ...
- Tomcat中的Host和Engine级别的servlet容器
这边文章主要介绍的是Host容器 和 Engine容器.如果你想在同一个Tomcat上部署运行多个Context容器的话,你就需要使用Host容器,从理论上来讲,如果你的Tomcat只想要部署一个Co ...