POJ2752 Seek the Name, Seek the Fame

/*
next数组存储了这样的信息,以第一个样例来看:

下标:     0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
模式串:   a b a b c a b a b a b  a   b  c  a  b  a  b
next数组:-1 0 0 1 2 0 1 2 3 4 3  4   3  4  5  6  7  8  9

18肯定符合要求, 接下来是next[18] = 9符合要求, 接下来是next[9] = 4,接下来next[4] = 2, 接下来next[2] = 0, 接下来next[0] = -1;
按照这种方式,即可得到答案分别为18 9 4 2;
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <cctype>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <fstream>

using namespace std;

#define mem(A, X) memset(A, X, sizeof A)
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define all(x) x.begin(), x.end()
#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
#define sz(x) (int)((x).size())
#define vi vector<int>
#define rep(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#define Rep(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define dbg(a) cout << a << endl;
#define fi first
#define se second
typedef long long int64;
int gcd(const int64 &a, const int64 &b) {return b == 0 ? a : gcd(b, a % b);}
int64 int64pow(int64 a, int64 b){if(b == 0) return 1;int64 t = int64pow(a, b / 2);if(b % 2) return t * t * a;return t * t;}
const int inf = 1 << 30;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int MAX_N = 400005;

string pattern;
int n, next[MAX_N];
vi v, ans;

void FindNext()
{
    mem(next, 0);next[0] = -1;
    v.clear();
    ans.clear();
    n = sz(pattern);
    v.resize(n + 1);

    rep(i, 1, n) {
        int j = i;
        while (j > 0) {
            j = next[j];
            if (pattern[j] == pattern[i]) {
                next[i + 1] = j + 1;
                break;
            }
        }
    }
}
int main()
{
    while (cin >> pattern) {
        FindNext();
        ans.pb(sz(pattern));
        for (int pos = sz(pattern); pos != 0; pos = next[pos]) {
            ans.pb(next[pos]);
        }
        sort(all(ans));
        cout << ans[1];
        rep(i, 2, sz(ans)) cout << " " << ans[i];
        cout << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值