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> ...
随机推荐
- WEB安全----XSS和CSRF
随着Web2.0.社交网络.微博等等一系列新型的互联网产品的诞生,基于Web环境的互联网应用越来越广泛,企业信息化的过程中各种应用都架设在Web平台上,Web业务的迅速发展也引起黑客们的强烈关注,接踵 ...
- JQ获取当前根目录
function getRootPath_web() { //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp ...
- python datetime模块的strftime()
strftime() 可以对datetime对象进行格式化,生成需要时间格式的时间 strptime() 可以对格式化后的时间再生成datetime对象 格式化时间时,如果不想要-来隔开,还可以 ...
- python中函数的嵌套和作用域链
1.三元运算if条件成立的结果 if 条件 else 条件不成立的结果例如: a=20 b=10 c=a if a>b else b print(c) 2.命名空间 全局命名空间:创建的存储“变 ...
- 《JAVA设计模式》之迭代器模式(Iterator)
在阎宏博士的<JAVA与模式>一书中开头是这样描述迭代子(Iterator)模式的: 迭代子模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不 ...
- 学习mybaits用到的4张表
items orderdetail orders user
- node进程一些信号的意义
1.SIGINT这个信号是系统默认信号,代表信号中断,就是ctrl+c: 2.SIGQUIT 3.SIGTERM 4.exit
- 二叉树BinTree4种遍历及其应用
前序遍历 template<class T> void BinTree<T>::PreOrder(BinTreeNode<T>*subTree){ //前序遍历以s ...
- CSS 实现水平垂直居中
使用绝对定位 在已经知道子元素的宽高的时候,子元素设置成绝对定位,top,left,right,bottom=0, margin = auto .wrap{ position: relative; w ...
- LeetCode Array Easy 448. Find All Numbers Disappeared in an Array
Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear ...