题目描述

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. day17—Flex弹性布局详解(一)

    转行学开发,代码100天——2018-04-02 今天看到一篇大神的文章,关于flex布局的详解,对flex用法介绍的相当详细,非常有助于我等初学者更深入了解这种布局方式. 文章链接 [基础知识]Fl ...

  2. Vagrant 手册之 Vagrantfile - 概述

    原文地址 Vagrantfile 的主要用途是描述用于项目的机器类型,以及如何配置和提供这些机器. Vagrant 的每个项目运行一个 Vagrantfile,并且 Vagrantfile 应该被提交 ...

  3. 16/8/23-jQuery完全图解scrollLeft,scrollWidth,clientWidth,offsetWidth 获取相对途径,滚动图片

    引用地址:http://www.cnblogs.com/mguo/archive/2013/03/19/2969657.html scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最 ...

  4. HTML--JS 二级联动

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. State Hook

    1 useState函数的第一个参数,是state变量的初始值. 2 每次渲染时,多个State Hook的顺序.数量都是一样的.(不能多.不能少) 3 state变量是只读的 4 state变量发生 ...

  6. 逻辑回归提高阈值对p和r的影响

    这里我做了一个实验 也就是随着阈值的增大,precision增加或者不变,recall减少或者不变.

  7. Pikachu漏洞练习平台实验——SQL注入(四)

    1.概述 1.1发生原因 SQL注入漏洞,主要是开发人员在构建代码时,没有对输入边界进行安全考虑,导致攻击者可以通过合法的输入点提交一些精心构造的语句,从而欺骗后台数据库对其进行执行,导致数据库信息泄 ...

  8. jmeter 添加header

    接口说明文档: article.fetch(通用转码服务) 通用转码服务,获取任意 url 的正文以及 title 等基本信息,仅支持 post 方法请求. 参数 参数 类型 是否必须 示例 其它说明 ...

  9. 《JAVA设计模式》之享元模式(Flyweight)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述享元(Flyweight)模式的: Flyweight在拳击比赛中指最轻量级,即“蝇量级”或“雨量级”,这里选择使用“享元模式”的意译,是 ...

  10. HashMap之扰动函数和低位掩码

    我们都知道,hashMap在实现的时候,为了寻找在数组上的位置,主要做了两件事 int hash = hash(key); int i = indexFor(key, table.length); 这 ...