题目链接:传送门

请允许我说这是个板子
把串在后面接一个做SA就好
从在范围内的开始输出答案

/**
 * @Date:   2019-03-10T07:35:22+08:00
 * @Last modified time: 2019-03-10T07:36:32+08:00
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define A 1000010
#define B 2010

using namespace std;
typedef long long ll;
int n, nn, m = 'z', pos, now, c[A], y[A], rk[A], sa[A];
char a[A], tmp[A];

int main(int argc, char const *argv[]) {
    cin >> (tmp + 1); strcat(a + 1, tmp + 1); strcat(a + 1, tmp + 1); n = strlen(a + 1); nn = strlen(tmp + 1);
    for (int i = 1; i <= n; i++) ++c[rk[i] = a[i]];
    for (int i = 1; i <= m; i++) c[i] += c[i - 1];
    for (int i = n; i >= 1; i--) sa[c[rk[i]]--] = i;
    for (int k = 1; k <= n; k <<= 1) {
        int num = 0;
        for (int i = n - k + 1; i <= n; i++) y[++num] = i;
        for (int i = 1; i <= n; i++)
        if (sa[i] > k)
        y[++num] = sa[i] - k;
        memset(c, 0, sizeof c);
        for (int i = 1; i <= n; i++) ++c[rk[i]];
        for (int i = 1; i <= m; i++) c[i] += c[i - 1];
        for (int i = n; i >= 1; i--) sa[c[rk[y[i]]]--] = y[i], y[i] = 0;
        swap(rk, y); rk[sa[1]] = num = 1;
        for (int i = 2; i <= n; i++)
        if (y[sa[i]] == y[sa[i - 1]] and y[sa[i] + k] == y[sa[i - 1] + k]) rk[sa[i]] = num;
        else rk[sa[i]] = ++num;
        if (num == n) break;
        m = num;
    }
	for (int i = 1; i <= n; i++)
		if (sa[i] <= nn) cout << a[sa[i] + nn - 1];
	return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐