题目描述

E.T. Inc. employs Maryanna as alien signal researcher. To identify possible alien signals and background noise, she develops a method to evaluate the signals she has already received. The signal sent by E.T is more likely regularly alternative. 
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.

输入

The first line contains T, the number of test cases (T <= 200). Most of the test cases are relatively small. T lines follow, each contains a string of only small latin letters 'a' - 'z', whose length N is less than 1000, without any leading or trailing whitespaces.

输出

For each test case, output a single line, which should begin with the case number counting from 1, followed by N integers. The X-th (1-based) of them should be the maximum length of the substring which can be written as a concatenation of X same strings. If that substring doesn't exist, output 0 instead. See the sample for more format details.

样例输入

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求循环节)的更多相关文章

  1. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  2. Uva 12012 Detection of Extraterrestrial 求循环节个数为1-n的最长子串长度 KMP

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=3163">点击打开链接 题意: ...

  3. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  4. HDU - 3374 String Problem (kmp求循环节+最大最小表示法)

    做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...

  5. ( KMP 求循环节的个数)Power Strings -- poj -- 2406

    链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bi ...

  6. KMP与循环节相关题目

    HDU 3746 Cyclic Nacklace ( KMP求最小循环节 ) len - nextval[len]即为最小循环节长度. #include <cstdio> #include ...

  7. 用KMP征服循环节问题

    以前我还是写过KMP的文章的 现在我们可以求一下循环节啊 Slot Machines Gym - 101667I #include<bits/stdc++.h> using namespa ...

  8. Java求循环节长度

    两个整数做除法,有时会产生循环小数,其循环部分称为:循环节.比如,11/13=6=>0.846153846153.....  其循环节为[846153] 共有6位.下面的方法,可以求出循环节的长 ...

  9. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

随机推荐

  1. 牛客提高D4t2 卖羊驼

    分析 不难想到dp[i][j]表示前i个数分了j组的最大值 我们发现这个dp状态有决策单调性 g[i][j]表示对于第i个数它的第j位最近出现的位置 每次一定从这些点转移 预处理即可 似乎还可以做到1 ...

  2. P2239螺旋矩阵

    传送 看到这数据范围,显然咱不能暴力直接模拟(二维数组开不下,而且会T掉) 我们目前有两种选择: 1.优化暴力  走这边(jyy tql%%%) 2.数学做法 我们看一下题目中的那个矩阵 我们能不能找 ...

  3. Mac下隐藏或显示文件/文件夹

    命令行操作 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:defaults write com.apple.fi ...

  4. cts测试流程

    测试目的: 用于检测你做的Android系统是否满足兼容性要求,通俗点说,Google认为Android系统应该满足的条件,你需要满足. 例如框架层暴露给应用层的某些接口,Google认为你因该有,那 ...

  5. C#后台验证含0的正整数

    Regex r = new Regex(@"^([1-9]\d*|[0]{1,1})$");//含0正整数 if (!r.IsMatch(GNumber)) {  return f ...

  6. 09 (H5*) JS第7天 原型

    目录 1:创建对象的3中方式 2:工厂模式创建实例对象 3:  实例对象和构造函数的关系 4:构造函数创建对象带来的问题--原型 5:原型中创建方法 6:构造函数.原型对象.实例对象的关系 7:原型对 ...

  7. ssh公私钥免密登陆

    简介ssh Secure Shell(简写SSH) 为一项建立在应用层和传输层基础上的安全协议,专门为远程登录会话和其他网络服务提供安全性的协议. SSH安全机制分为两种,一种是基于口令的安全认证,一 ...

  8. kmp(前中后最长相同长度)

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)  ...

  9. KMP字符串匹配学习

    KMP字符串匹配学习 牛逼啊 SYC大佬的博客

  10. python第一部分小结

       1.python的种类                                                                              Cpython: ...