题目描述

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. 牛客提高D5t1 deco的abs

    分析 傻子题? 对d取模后随便贪心即可 代码 #include<iostream> #include<cstdio> #include<cstring> #incl ...

  2. npm install 安装过程卡住不动

    修改 npm 的安装目录下的 npmrc文件 增加一条 registry=http://registry.cnpmjs.org $ npm config set registry http://reg ...

  3. HTML中margin与padding的区别!(转)

    我们以DIV为一个盒子为例,既然和现实生活中的盒子一样,那我们想一下,生活中的盒子内部是不是空的好用来存放东西,而里面存放东西的区域我们给他起个名字叫“content(内容)”,而盒子的纸壁给他起个名 ...

  4. PHP批量生成底部带编号二维码(二维码生成+文字生成图片+图片拼接合并)

    PHP批量生成带底部编号二维码(二维码生成+文字生成图片+图片拼接合并) 需求: 输入编号如 : cb05-0000001  至 cb05-0000500 批量生成 以编号为名称的下图二维码,然后压缩 ...

  5. 【FICO系列】SAP FICO模块-完工入库后的差异凭证处理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO模块-完工入库后 ...

  6. 个人珍藏的PC和安卓应用列表

    个人珍藏的PC和安卓应用列表 PC应用名称 功能描述 注意事项 存储位置 维棠FLV视频下载软件 可用来下载腾讯视频的视频 将视频列表的网址粘贴到维棠软件左侧"下载"的地址中即可批 ...

  7. SEC7 - MySQL 查询语句--------------进阶3:排序查询

    # 进阶3:排序查询 /* 引入: select * from employees; 语法: select 查询列表 from 表 [where 筛选条件] order by 排序的列表 asc/de ...

  8. Scala函数高级操作

    字符串高级操作:***** 非常重要 将函数赋值给变量/值def sayHello(name:String): Unit = { println(s"Hello:$name")} ...

  9. IDEA永久破解方法

    链接: https://pan.baidu.com/s/1a1pMOP6rMrh-wJdUFSCqAw 提取码: 46cx 复制这段内容后打开百度网盘手机App,操作更方便哦

  10. [Linux Test Project] [PATCH] Fix an bad variable name erro in runltp script

    [PATCH] Fix an bad variable name erro in runltp script (/opt/ltp/runltp) Hi All,   I got an error fo ...