POJ-2752(KMP算法+前缀数组的应用)

Seek the Name, Seek the Fame

POJ-2752

  • 本题使用的算法还是KMP
  • 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来依次找到前面符合的前缀(所谓符合就是可以保持既是前缀也是s的后缀的子串长度)。
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<set>
#include<cstring>
using namespace std;
const int N=400005;
int pi[N];
void Pi(string s){
    memset(pi,0,sizeof(pi));
    int n=s.length();
    pi[0]=0;
    for(int i=1;i<n;i++){
        int j=pi[i-1];
        while(j>0&&s[i]!=s[j]){
            j=pi[j-1];
        }
        if(s[i]==s[j])
            j++;
        pi[i]=j;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    while(cin>>s){
        set<int> se;
        Pi(s);
        int n=s.length();
        if(n==1){
            cout<<1<<endl;
            continue;
        }
        int j=pi[n-2];
        while(j>0){
            if(s[n-1]==s[j]){
                se.insert(j+1);
            }
            j=pi[j-1];
        }
        se.insert(n);
        if(s[0]==s[n-1])
        se.insert(1);
        int k=0;
        int cnt=(int)se.size();
        for(set<int>::iterator it=se.begin();it!=se.end();it++){
            if(k++==cnt-1)
                cout<<*it<<endl;
            else
                cout<<*it<<" ";
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/GarrettWale/p/11326449.html

时间: 2024-08-05 08:46:02

POJ-2752(KMP算法+前缀数组的应用)的相关文章

POJ 2752+KMP+利用next数组性质求出所有相同的前缀和后缀

题目链接:点击进入 这个题目要求所有相同的前缀和后缀的长度.我们可以利用KMP算法中next数组的性质,在next[len]这个点不断的失配下去,这样就可以将所有相同的前后缀的长度求出来.还要注意这个中整个串的长度也可以看成是一个合法的解. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=400000+100; char str

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher H - Seek the Name, Seek the Fame POJ - 2752(kmp的next数组应用)

H - Seek the Name, Seek the Fame POJ - 2752 题目链接:https://vjudge.net/contest/70325#problem/H 题目: 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. Th

POJ 2752 KMP中next数组的理解

感觉这里讲的挺好的.http://cavenkaka.iteye.com/blog/1569062 就是不断递归next数组.长度不断减小. 题意:给你一个串,如果这个串存在一个长度为n的前缀串,和长度为n的后缀串,并且这两个串相等,则输出他们的长度n.求出所有的长度n. 思路:KMP中的get_next().对前缀函数next[]又有了进一步的理解,str[1]~~str[next[len]]中的内容一定能与str[1+len-next[len]]~~str[len]匹配(图1).然后呢我们循

poj 2752 kmp(next数组的应用)

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

poj 2752 kmp的next数组

题目大意: 求一个字符串中某一个既是前缀又是后缀的前缀的结尾下标: 基本思路: 从_next[len]开始找_next[_next[len]],再找_next[_next[_next[len]]],一直找到0: 代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 4000

KMP算法&amp;next数组总结

http://www.cnblogs.com/yjiyjige/p/3263858.html KMP算法应该是每一本<数据结构>书都会讲的,算是知名度最高的算法之一了,但很可惜,我大二那年压根就没看懂过~~~ 之后也在很多地方也都经常看到讲解KMP算法的文章,看久了好像也知道是怎么一回事,但总感觉有些地方自己还是没有完全懂明白.这两天花了点时间总结一下,有点小体会,我希望可以通过我自己的语言来把这个算法的一些细节梳理清楚,也算是考验一下自己有真正理解这个算法. 什么是KMP算法: KMP是三位

poj1961(kmp算法next数组应用)

题目链接:https://vjudge.net/problem/POJ-1961 题意:给定一个长为n的字符串(n<=1e6),对于下标i(2<=i<=n),如果子串s(1...i)是周期子串,输出其最大周期. 思路: 考察对kmp算法中next数组的定义掌握,如果(i+1)%(i-j)==0 && (i+1)/(i-j) > 1,那么该子串即为满足条件. AC代码: #include<cstdio> #include<algorithm>

HDU 3336 Count the string(KMP算法next数组的应用)

解题思路: 求前缀数组出现的次数之和,next[i] > 0 表示长度为next[i]的前缀又出现了一次. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <queue> #

POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame

题意: 求一个字符串的相同前缀后缀的所有可能的长度,这里该字符串其本身也算自己的前缀和后缀. 分析: 我们知道next数组的性质是,该字符之前的字符串的最大相同前缀后缀. 既然知道了最大的,即next[len]. 递归一次next[ next[len] ],就能求得更小的前缀. 不断的递归把所有所有可能的长度找出来,然后递归输出即可. 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 400000; 5 ch