POJ 2752 Seek the Name, Seek the Fame

Seek the Name, Seek the Fame

Time Limit: 2000MS Memory Limit: 65536K

Description

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:

Step1. Connect the father’s name and the mother’s name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).

Example: Father=‘ala’, Mother=‘la’, we have S = ‘ala’+‘la’ = ‘alala’. Potential prefix-suffix strings of S are {‘a’, ‘ala’, ‘alala’}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)

Input

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

Output

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby’s name.

Sample Input

ababcababababcabab
aaaaa

Sample Output

2 4 9 18
1 2 3 4 5

题意:

题目有个mo串,题目要求我们求出有多少个在mo串中前缀和后缀相等的字串,递增输出。

第一种做法:(kmp)

思路:

实际上就是求next数组,比如第一个例子中2, 4的时候当出现4的时候mo[4] = c但是前缀是abab,后缀也是abab,然后再2时候是前缀是ab,后缀是ab,所以就出现了2的前缀的等于4后缀的后缀(就是等于4后缀abab中的后面的ab),同理就可以得出其实虽然ab只是2位置上前缀和后缀相等,但对于整个是不影响的。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
const int maxn = 4e+5 + 10;
string mo;
int Next[maxn];
vector<int> v;
void GetNext() {
    int i = 0, j = -1;
    while (i < mo.size()) {
        if (j == -1 || mo[j] == mo[i]) Next[++i] = ++j;
        else j = Next[j];
    }
}
int main() {
    ios::sync_with_stdio(false);
    while (cin >> mo) {
        v.clear();
        Next[0] = -1;
        GetNext();
        int j = Next[mo.size()];
        while (j > 0) {
            v.push_back(j);
            j = Next[j];
        }
        for (int i = v.size() - 1; i >= 0; i--) {
            printf("%d ", v[i]);
        }
        printf("%d\n", mo.size());
    }
    return 0;
}

第二种做法:(扩展kmp)

思路:

因为求得是后缀和前缀相等的值,所以也就是mo[0-len - 1] = mo[x - len - 1]这也符合扩展kmp的定理,所以就用扩展kmp求解了。然后主要求next数组。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 400000 + 10;
char mo[maxn];
int next[maxn]; 
void getnext() {
	int m = strlen(mo);
	next[0] = m;
	int j = 0;
	while (j + 1 < m && mo[j] == mo[j + 1]) j++;
	next[1] = j;
	int k = 1;
	for (int i = 2; i < m; i++) {
		int p = next[k] + k - 1;
		int L = next[i - k];
		if (i + L < p + 1) next[i] = L;
		else {
			j = max(0, p - i + 1);
			while (i + j < m && mo[i + j] == mo[j]) j++;
			next[i] = j;
			k = i;
		}
	}
}
int main() {
	while (scanf("%s", mo) != EOF) {
		getnext();
		int len  =strlen(mo);
		for (int i = len - 1; i >= 0; i--) {
			if (len - i == next[i]) printf("%d ", next[i]);
		}
		printf("\n");
	}
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值