POJ--2752--Seek the Name, Seek the Fame【KMP】

链接:http://poj.org/problem?id=2752

题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值。

思路:这道题加深了对next数组的理解。next[i+1]相当于以第i位结尾的长度为next[i+1]的子串与前next[i+1]个字符组成的子串相同,理解之后就比较好做了,首先字符串的长度len肯定是一个答案,然后next[len]也是一个答案,原因如红字所写,如此迭代直到next下标值等于0停止,这是从大到小得到了答案,再反序输出即可。

因为这道题的特殊性,用优化的getnext函数无法AC。

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 500100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define LLINF 0x7FFFFFFFFFFFFFFF
#define seed 131
#define mod 1000000007
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

char str[401000];
int next[401000];
void getnext(){
    int i = 0, j = -1;
    int l = strlen(str);
    next[0] = -1;
    while(i<l){
        if(j==-1||str[i]==str[j]){
            i++;
            j++;
            next[i] = j;
        }
        else
            j = next[j];
    }
}
int ans[401000];
int main(){
    int i,j;
    while(scanf("%s",str)!=EOF){
        getnext();
        int l = strlen(str);
        int i = l;
        int sum = 0;
        while(i!=0){
            ans[sum++] = i;
            i = next[i];
        }
        printf("%d",ans[sum-1]);
        for(i=sum-2;i>=0;i--){
            printf(" %d",ans[i]);
        }
        puts("");
    }
}
时间: 2024-07-28 22:22:53

POJ--2752--Seek the Name, Seek the Fame【KMP】的相关文章

poj 2752Seek the Name, Seek the Fame 【kmp】

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14154   Accepted: 7049 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 t

POJ2752 Seek the Name, Seek the Fame 【KMP】

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11602   Accepted: 5680 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 t

POJ2752 Seek the Name, Seek the Fame【KMP】

题目链接: http://poj.org/problem?id=2752 题目大意: 给定一个字符串S,计算出所有可能的前缀-后缀字符串的长度.前缀-后缀字符串指的是S的 子串不仅是S的前缀,还是S的后缀.比如S = "alala",前缀-后缀字符有{"a","ala","alala"}. 思路: KMP算法的应用.在KMP算法中,当字符串匹配失败时,模式串的指针并没有指向0从头比 较,而是指向了一个特定的位置,因为这个Nex

poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14106   Accepted: 7018 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 t

【POJ2752】【KMP】Seek the Name, Seek the Fame

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 b

poj2752Seek the Name, Seek the Fame【kmp next数组应用】

大意:给你一个串,如果这个串存在一个长度为n的前缀串,和长度为n的后缀串,并且这两个串相等,则输出他们的长度n.求出所有的长度n 例如 ‘alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'} 分析:考察对于next数组的理解 next数组表示i之前的k个字符与该串钱k个字符匹配 所以 next[l] 就表示最大后缀满足与前缀相同的最大字串 然后  在对获得的字串进行同样的求解  最终得到结果 代码: 1 #i

POJ Period 1961【KMP】

Language: Default Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14658   Accepted: 6968 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want t

poj 2406 Power Strings 【kmp】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37564   Accepted: 15532 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

POJ 2420 A Star not a Tree?【爬山法】

题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只可以加一个点问最小值就是广义的费马点的问题,如果加点的数目不加限制,那问题就成了斯坦纳树的问题(介个属于NPC问题) 这题显然就是广义费马点问题,可以采用局部贪心法,从一个初始点出发,不断向上下左右四个方向拓展,如果在一个方向上走过去到所有点的距离和小于目前这个点到所有点的距离和,那就更新目前点的值