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.

Input 

The first line contains T, the number of test cases (T$ \le$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.

Output 

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.


Hint: 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.

Sample Input 

2
arisetocrat
noonnoonnoon

Sample Output 

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

                                                             

KMP :利用KMP 的next 数组,每枚举一次开头就getnext一次,不断的更新next数组,从而得到答案。这种方法没有超时也是醉了=。=

CODE:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1005;

char str[MAXN], s[MAXN];
int next[MAXN], m, n, ans[MAXN];

void getnext()
{
    next[0] = next[1] = 0;
    int j = 0;
    for(int i = 2; i <= m; ++i) {
        while(j && s[i - 1] != s[j]) j = next[j];
        if(s[i - 1] == s[j]) j++;
        next[i] = j;
    }
}

int main()
{
    //freopen("in", "r", stdin);
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ++ca) {
        memset(ans, 0, sizeof(ans));
        scanf("%s", str);
        n = strlen(str);
        for(int i = 0; i < n; ++i) {
            m = 0;
            for(int j = i; j < n; ++j) {
                s[m++] = str[j];
            }
            getnext();
            for(int j = 1; j <= m; ++j) {
                int tmp = j;
                while(tmp) {
                    if(j % (j - next[tmp]) == 0){
                        int t = j / (j - next[tmp]);
                        ans[t] = max(j, ans[t]);
                    }
                    tmp = next[tmp];
                }
            }
        }
        printf("Case #%d:", ca);
        for(int i = 1; i <= n; ++i) {
            printf(" %d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}

WAY2 : 哈希数组处理字符串,再枚举开头和长度,运用vis数组记录已经判断过的开头长度,这种做法很快!

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int x = 131; //质数
char str[1005];
unsigned long long h[1005], xp[1005]; //记得使用unsigned,因为如果数组爆了,还会表示成不一样的数字。
int ans[1005];
bool vis[1005][1005];

int main()
{
//freopen("in", "r", stdin);
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ++ca){
        memset(ans, 0, sizeof(ans));
        memset(vis, 0, sizeof(vis));
        scanf("%s", str);
        int n = strlen(str);
        h[n] = xp[0] = 1;
        for(int i = n - 1; i >= 0; --i)
            h[i] = h[i + 1] * x + (str[i] - 'a');
        for(int i = 1; i <= n; ++i)
            xp[i] = xp[i - 1] * x;
        ans[1] = n;
        for(int i = 0; i < n; ++i){
            for(int j = 1; j + i <= n; ++j)
                if(!vis[i][j]){
                    vis[i][j] = true;
                    int num = 1;
                    unsigned long long tmp;
                    tmp = h[i] - h[j + i] * xp[j];
                    int now = i + j;
                    while(now + j <= n && h[now] - h[now + j] * xp[j] == tmp){
                        num++;
                        vis[now][j] = true;
                        now += j;
                        ans[num] = max(j*num, ans[num]);
                    }
                }
        }
        printf("Case #%d:", ca);
        for(int i = 1; i <= n; ++i){
            printf(" %d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值