解題思路
先對原字串與反轉字串建立後綴陣列、後綴排名與 LCP 的區間最小值表。兩後綴間的 LCP 等於兩排名之間相鄰 LCP 的最小值;反轉後的同樣查詢則得到可向左延伸的相等長度。
固定週期 p,只檢查 i=0,p,2p,… 且 i+p<n 的邊界。令 R=LCP(i,i+p),L 為 i 與 i+p 前方可向左延伸的相等長度。區間 [i−L,i+p+R) 具有週期 p,因此最多可放 k=1+floor((L+R)/p) 個完整片段。任何至少重複兩次的片段,其第一個長度 p 的區塊中必含一個 p 的倍數邊界,所以不會漏掉最佳解。
達到這個 k 的合法開始位置恰為 [i−L,i+R−(k−1)p]。在此範圍查詢最小的後綴排名,即得到字典序最小的長 p 片段。相同長度的片段若不同,其字典序和後綴排名一致;若相同,選哪個位置都輸出同一片段。
答案依序比較重複次數、片段長度及字典序。先以整個字串、重複一次作基底;若沒有任何串聯重複,這正是最長的合法解。當 floor(n/p) 已小於目前最佳次數時,更大的 p 也不可能改善答案,可停止。
倍增排序建後綴陣列 O(n log²n),稀疏表與所有週期邊界共 O(n log n),總時間 O(n log²n)、空間 O(n log n);兩種程式均計入輸入與查詢表。
Step-by-step animation
公開範例 aabababa:p=2 的雙向延伸與字典序選擇
步驟 1 / 3
找出週期區間
固定 p=2、邊界 i=2。位置2與4向右相等4格,向左相等1格,因此 R=4、L=1。
a
a
b
a
b
a
b
a
- p
- 2
- i
- 2
- L / R
- 1 / 4
#include <bits/stdc++.h>
using namespace std;
struct SparseMin {
vector<vector<int>> table;
vector<int> logarithm;
SparseMin() = default;
explicit SparseMin(const vector<int> &values) {
int n = values.size();
logarithm.resize(n + 1);
for (int i = 2; i <= n; ++i)
logarithm[i] = logarithm[i / 2] + 1;
table.push_back(values);
for (int width = 1; width * 2 <= n; width *= 2) {
vector<int> row(n - width * 2 + 1);
for (int i = 0; i < (int)row.size(); ++i)
row[i] = min(table.back()[i], table.back()[i + width]);
table.push_back(move(row));
}
}
int get(int left, int right) const {
int power = logarithm[right - left + 1];
return min(table[power][left], table[power][right - (1 << power) + 1]);
}
};
struct SuffixIndex {
int n;
vector<int> order, rank;
SparseMin lcpMinimum;
explicit SuffixIndex(const string &text) : n(text.size()), order(n), rank(n) {
if (n == 1) {
order[0] = rank[0] = 0;
lcpMinimum = SparseMin(vector<int>{0});
return;
}
iota(order.begin(), order.end(), 0);
for (int i = 0; i < n; ++i)
rank[i] = (unsigned char)text[i];
for (int width = 1; width < n; width *= 2) {
auto key = [&](int i) {
return pair<int, int>{rank[i], i + width < n ? rank[i + width] : -1};
};
sort(order.begin(), order.end(),
[&](int a, int b) { return key(a) < key(b); });
vector<int> nextRank(n);
for (int at = 1; at < n; ++at)
nextRank[order[at]] =
nextRank[order[at - 1]] + (key(order[at - 1]) != key(order[at]));
rank.swap(nextRank);
if (rank[order.back()] == n - 1)
break;
}
vector<int> lcp(n);
int common = 0;
for (int i = 0; i < n; ++i) {
int at = rank[i];
if (at == 0) {
common = 0;
continue;
}
int j = order[at - 1];
while (i + common < n && j + common < n &&
text[i + common] == text[j + common])
++common;
lcp[at] = common;
if (common)
--common;
}
lcpMinimum = SparseMin(lcp);
}
int lce(int a, int b) const {
if (a == b)
return n - a;
int left = rank[a], right = rank[b];
if (left > right)
swap(left, right);
return lcpMinimum.get(left + 1, right);
}
};
pair<string, int> solve(const string &text) {
int n = text.size();
SuffixIndex forward(text);
string reversed = text;
reverse(reversed.begin(), reversed.end());
SuffixIndex backward(reversed);
SparseMin rankMinimum(forward.rank);
int bestCount = 1, bestLength = n, bestPosition = 0;
for (int period = 1; period <= n / 2; ++period) {
if (n / period < bestCount)
break;
for (int anchor = 0; anchor + period < n; anchor += period) {
int right = forward.lce(anchor, anchor + period);
int left = anchor ? backward.lce(n - anchor, n - anchor - period) : 0;
int count = 1 + (left + right) / period;
if (count < bestCount || (count == bestCount && period < bestLength) ||
count < 2)
continue;
int first = anchor - left;
int last = anchor + right - (count - 1) * period;
int position = forward.order[rankMinimum.get(first, last)];
if (count > bestCount || period > bestLength ||
forward.rank[position] < forward.rank[bestPosition]) {
bestCount = count;
bestLength = period;
bestPosition = position;
}
}
}
return {text.substr(bestPosition, bestLength), bestCount};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string text;
while (cin >> n >> text) {
auto [unit, count] = solve(text);
cout << unit << '\n' << count << '\n';
}
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。