UVA 12012 Detection of Extraterrestrial(KMP求循环节)

题目描述

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数组的位置跳的

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1100;
 4 int nxt[maxn];
 5 char s[maxn];
 6 int ret[maxn];
 7 int casee = 0;
 8 void getnxt (char s[])
 9 {
10     int j,k;
11     int len = strlen(s);
12     j = 0,k = -1;
13     nxt[0] = -1;
14     while (j<len){
15         if (k==-1||s[j]==s[k])
16             nxt[++j] = ++k;
17         else
18             k = nxt[k];
19     }
20 }
21 int main()
22 {
23     int T;
24     scanf("%d",&T);
25     while (T--){
26         scanf("%s",s);
27         memset(ret,0,sizeof ret);
28         int len = strlen(s);
29         ret[1] = len;
30         for (int i=0;i<len;++i){
31             memset(nxt,0,sizeof nxt);
32             int tmplen = strlen(s+i);
33             getnxt(s+i);
34             for (int j=2;j<=tmplen;++j){
35                 int tmp = j;
36                 while (tmp){//对于每个循环串我们寻找循环节
37                     tmp = nxt[tmp];//每次沿着nxt跳是不会错过循环节的
38                     if (j%(j-tmp)==0){
39                         int x = j/(j-tmp);
40                         ret[x] = max(ret[x],j);
41                     }
42                 }
43             }
44         }
45         printf("Case #%d:",++casee);
46         for (int i=1;i<=len;++i)
47             printf(" %d",ret[i]);
48         printf("\n");
49     }
50     return 0;
51 }

 

转载于:https://www.cnblogs.com/agenthtb/p/7638367.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值