UVA 12012 Detection of Extraterrestrial(KMP求循环节)
题目描述
Received signals can be presented by a string of small latin letters 'a' to 'z' whose length is N. For each X between 1 and N inclusive, she wants you to find out the maximum length of the substring which can be written as a concatenation of X same strings. For clarification, a substring is a consecutive part of the original string.
输入
输出
样例输入
2
arisetocrat
noonnoonnoon
样例输出
Case #1: 11 0 0 0 0 0 0 0 0 0 0
Case #2: 12 8 12 0 0 0 0 0 0 0 0 0
提示
For the second sample, the longest substring which can be written as a concatenation of 2 same strings is "noonnoon", "oonnoonn", "onnoonno", "nnoonnoo", any of those has length 8; the longest substring which can be written as a concatenation of 3 same strings is the string itself. As a result, the second integer in the answer is 8 and the third integer in the answer is 12.
给你一个长度为n的串,让你求其中的字串是1~n循环串的最长长度
KMP的性质能让我们求出最小循环节的长度跟循环次数
如果一个长度为len的字符串,如果 len%(len-nxt[len])==0&&nxt[len]!=0就说明字符串循环 (如果nxt[len0]==0那么说明这个串不循环啊)
循环节长度为len-nxt[len] 循环次数为len/(len-nxt[len])
这个题循环串不一定出现在串首,我们要枚举这个串的所有字串,先枚举起点,再枚举长度
对于每个字串我们求KMP,但是我们求的是最小循环节,对于aaaaaa这个样例我们求出循环节长度为1,然而我们还要更新循环节为aa,aaa的答案
所以对于一个循环串我们就沿着nxt的路走步步更新,因为循环节的位置肯定是沿着nxt数组的位置跳的
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int nxt[maxn];
char s[maxn];
int ret[maxn];
int casee = ;
void getnxt (char s[])
{
int j,k;
int len = strlen(s);
j = ,k = -;
nxt[] = -;
while (j<len){
if (k==-||s[j]==s[k])
nxt[++j] = ++k;
else
k = nxt[k];
}
}
int main()
{
int T;
scanf("%d",&T);
while (T--){
scanf("%s",s);
memset(ret,,sizeof ret);
int len = strlen(s);
ret[] = len;
for (int i=;i<len;++i){
memset(nxt,,sizeof nxt);
int tmplen = strlen(s+i);
getnxt(s+i);
for (int j=;j<=tmplen;++j){
int tmp = j;
while (tmp){//对于每个循环串我们寻找循环节
tmp = nxt[tmp];//每次沿着nxt跳是不会错过循环节的
if (j%(j-tmp)==){
int x = j/(j-tmp);
ret[x] = max(ret[x],j);
}
}
}
}
printf("Case #%d:",++casee);
for (int i=;i<=len;++i)
printf(" %d",ret[i]);
printf("\n");
}
return ;
}
UVA 12012 Detection of Extraterrestrial(KMP求循环节)的更多相关文章
- HDU 3746 Cyclic Nacklace (KMP求循环节问题)
<题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题] #include &l ...
- Uva 12012 Detection of Extraterrestrial 求循环节个数为1-n的最长子串长度 KMP
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=3163">点击打开链接 题意: ...
- HDU 1358 Period (kmp求循环节)(经典)
<题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...
- HDU - 3374 String Problem (kmp求循环节+最大最小表示法)
做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...
- ( KMP 求循环节的个数)Power Strings -- poj -- 2406
链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS Memory Limit:65536KB 64bi ...
- KMP与循环节相关题目
HDU 3746 Cyclic Nacklace ( KMP求最小循环节 ) len - nextval[len]即为最小循环节长度. #include <cstdio> #include ...
- 用KMP征服循环节问题
以前我还是写过KMP的文章的 现在我们可以求一下循环节啊 Slot Machines Gym - 101667I #include<bits/stdc++.h> using namespa ...
- Java求循环节长度
两个整数做除法,有时会产生循环小数,其循环部分称为:循环节.比如,11/13=6=>0.846153846153..... 其循环节为[846153] 共有6位.下面的方法,可以求出循环节的长 ...
- HDU 1358 Period(KMP+最小循环节)题解
思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...
随机推荐
- Jexus 強勁、堅固、免費、易用的Linux ASP.NET服務器
Jexus 強勁.堅固.免費.易用的Linux ASP.NET服務器 Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关,以支持ASP.NET.ASP.NET CORE.PHP为特色, ...
- python while 循环打印九九乘法表
方向一 i = 1 while i <= 9: j = 1 while j <= i print('%d*%d = %2d'%( j,i ,i*j),end='') j += 1 prin ...
- .Net core 2.0 利用Attribute获取MVC Action来生成菜单
最近在学习.net core的同时将老师的MVC5项目中的模块搬过来用,其中有一块就是利用Attribute来生成菜单. 一·首先定义Action实体 /// <summary> /// ...
- Netty之大动脉Pipeline
Pipeline 设计原理 Channel 与ChannelPipeline: 相信大家都已经知道,在Netty 中每个Channel 都有且仅有一个ChannelPipeline 与之对应,它们的组 ...
- Java单链表
一.概述 二.主方法 //创建头结点 private HeroNode head = new HeroNode(-1,null,null); //计数器,用于id的自增 private static ...
- centos7 nginx完整支持thinkphp pathinfo模式开启方法
thinkphp运行在linux+nginx,如何开启pathinfo模式,我是完整测试过了,没问题的,在thinkphp配置文件 开启 'URL_MODEL' => 1, 1代 ...
- Java-技术专区-设计模式-reactor模式
模型: 反应器模式做法是:汽车是乘客访问的主体(Reactor),乘客上车后,到售票员(acceptor)处登记,之后乘客便可以休息睡觉去了,当到达乘客所要到达的目的地后,售票员将其唤 ...
- spring(六):spring中AOP的基本使用
AOP:面向切面编程[底层使用动态代理实现],就是在运行期间动态的将某段代码切入到方法的指定位置进行运行的编程方式 基本使用 使用AOP功能需要引入spring的aop以及aspects相关包 < ...
- 一、小程序内嵌Html示例
小程序内嵌Html 1.下载wxParse:https://github.com/icindy/wxParse 2.下载完成后将插件目录下的wxParse文件夹拷贝到项目目录下 (文件夹明细) 3.全 ...
- tracert显示为超时
1.那一跳禁PING2.那一跳不对TTL超时做响应处理,直接丢弃3.MPLS VPN网络