# 题意

$$|S| \le 3 \times 10^6$$

# 题解

• 要么有 $$S_1S_1S_2 \le S_1S_2 S_1 \le S_2S_1S_1$$
• 要么有 $$S_1S_1S_2 \ge S_1S_2 S_1 \ge S_2S_1S_1$$

# 代码

#include <bits/stdc++.h>

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}

void File() {
freopen ("3103.in", "r", stdin);
freopen ("3103.out", "w", stdout);
#endif
}

const int N = 3e6 + 1e3;

void Get_Next(char *S, int *next) {
int lenS = strlen(S + 1), p = 1, pos;
next[1] = lenS;
while (p + 1 <= lenS && S[p] == S[p + 1]) ++ p;
next[pos = 2] = p - 1;

For (i, 3, lenS) {
int len = next[i - pos + 1];
if (len + i < p + 1) next[i] = len;
else {
int j = max(p - i + 1, 0);
while (i + j <= lenS && S[j + 1] == S[i + j]) ++ j;
p = i + (next[pos = i] = j) - 1;
}
}
}

char S[N]; int lcp[N], n; vector<int> cur;

inline int cmp(int p, int len) {
return lcp[p] >= len ? 0 : (S[lcp[p] + 1] < S[p + lcp[p]] ? 1 : -1);
}

inline int cmp(int x, int y, int len) {
static int res; assert(x > y);
if ((res = cmp(y + (len - x + 1), x - y))) return res > 0 ? x : y;
if ((res = cmp(x - y + 1, y - 1))) return res > 0 ? y : x;
return y;
}

int main () {

File();

scanf ("%s", S + 1); n = strlen(S + 1); Get_Next(S, lcp);

For (k, 1, n) {
vector<int> tmp(1, k);
for (int i : cur) {
while (!tmp.empty() && S[i + k - tmp.back()] < S[k]) tmp.pop_back();
if (tmp.empty() || S[i + k - tmp.back()] == S[k]) {
while (!tmp.empty() &&
k - tmp.back() >= tmp.back() - i) tmp.pop_back();
tmp.push_back(i);
}
}

cur = tmp; int ans = cur[0];
For (i, 1, cur.size() - 1) ans = cmp(ans, cur[i], k);
printf ("%d%c", ans, k == kend ? '\n' : ' ');
}

return 0;

}

posted @ 2019-05-04 20:28  zjp_shadow  阅读(650)  评论(0编辑  收藏  举报