1.前言
虽然猜对了结论,但还是要 yy 一下证明。
变态,卡 STL
2.题解
我们只需要一个结论:对于两个字符串 A , B ( ∣ A ∣ ≤ ∣ B ∣ ) A, B(|A| \leq |B|) A,B(∣A∣≤∣B∣),它们的答案不超过 2 ∣ B ∣ 2|B| 2∣B∣
反证法:
记 n = ∣ A ∣ n = |A| n=∣A∣
如图,绿色方框表示 A A A,红色方框表示 B B B。
由图可以看出:若答案超过了 2 ∣ B ∣ 2|B| 2∣B∣,则可以得出一个必要条件: ∃ x \exists x ∃x,使得 A A A 的前 x x x 和 A A A 的后 x x x 相匹配, A A A 的后 n − x n - x n−x 和 A A A 的前 n − x n - x n−x 相匹配。
如图,两个绿色方框表示字符串 A A A。
可以推出,红色部分与蓝色蓝色部分相匹配。
继续推导,可以发现
A
A
A 的循环节为
x
x
x,且
∣
A
∣
x
∈
Z
∗
\frac{|A|}{x} \in Z^*
x∣A∣∈Z∗。则
A
A
A 是
B
B
B 的循环节,与题意不符。
□
□
□
用 H a s h Hash Hash ,枚举 A A A 串的长度,统计在当前枚举的 B B B 串中最大匹配数量,用 unordered_map 保存
(
p
t
s
pts
pts 60) 变态,卡 STL
#include <map>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define LL long long
#define ULL unsigned long long
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar ();
}
x *= f;
}
template <typename T>
void write (T x) {
if (x < 0) {
putchar ('-');
x = -x;
}
if (x < 10) {
putchar (x + '0');
return;
}
write (x / 10);
putchar (x % 10 + '0');
}
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
const int Maxn = 2 * 1e4;
const int Maxlen = 500;
const int P = 31;
int n;
char s[Maxlen * 2 + 5];
string c[Maxn + 5];
ULL Hash[Maxlen * 2 + 5], w[Maxlen * 2 + 5];
unordered_map <ULL, int> mp;
ULL Query (int l, int r) {
return Hash[r] - Hash[l - 1] * w[r - l + 1];
}
bool cmp (string x, string y) {
return x.length () > y.length ();
}
int main () {
w[0] = 1;
for (int i = 1; i <= Maxlen * 2; i++) w[i] = w[i - 1] * P;
int ans = 0;
read (n);
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
sort (c + 1, c + 1 + n, cmp);
//从大到小排序,保证靠前的字符串是 B
for (int k = 1; k <= n; k++) {
memset (s, 0, sizeof s);
for (int i = 0; i < c[k].length (); i++) {
s[i + 1] = c[k][i];
}
//s为 B串
int len = strlen (s + 1);
for (int i = 1; i <= len; i++) {
s[i + len] = s[i];
}
for (int i = 1; i <= len * 2; i++) {
Hash[i] = Hash[i - 1] * P + s[i] - '0' + 1;
}
ULL idx = 0;
for (int i = 1; i <= len; i++) {
idx = idx * P + s[i] - '0' + 1;
}
if (mp.find (idx) != mp.end ())
ans = Max (ans, mp[idx]);
for (int i = 1; i <= len; i++) {//枚举|A|
int res = i;
for (int j = i + 1; j + i - 1 <= len * 2; j += i) {
if (Query (j - i, j - 1) == Query (j, j + i - 1)) res += i;
else break;
}
for (int p = res + 1, q = 1; p <= len * 2; p++, q++) {
if (s[p] == s[q]) res++;
else break;
}
ULL idx = 0;
for (int j = 1; j <= i; j++) {
idx = idx * P + s[j] - '0' + 1;
}
//当A 串为idx 时,答案为 res
if (mp.find (idx) == mp.end ()) mp[idx] = res;
else mp[idx] = Max (mp[idx], res);
//记录下答案
}
}
write (ans);
return 0;
}
用 T r i e Trie Trie 树维护
#include <map>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define LL long long
#define ULL unsigned long long
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar ();
}
x *= f;
}
template <typename T>
void write (T x) {
if (x < 0) {
putchar ('-');
x = -x;
}
if (x < 10) {
putchar (x + '0');
return;
}
write (x / 10);
putchar (x % 10 + '0');
}
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
const int Maxn = 2 * 1e4;
const int Maxlen = 500;
int n;
string c[Maxn + 5];
bool cmp (string x, string y) {
return x.length () > y.length ();
}
int tot;
int Tire[2][Maxn * Maxlen * 2 + 5];
void Update (string s) {
s += s;
int p = 0;
for (int i = 0; i < s.length (); i++) {
int ch = s[i] - '0';
if (Tire[ch][p] == 0)
Tire[ch][p] = ++tot;
p = Tire[ch][p];
}
}
int Query (string s) {
int p = 0, res = 0;
for (int i = 0; ; i = (i + 1) % (int)s.length ()) {
int ch = s[i] - '0';
if (Tire[ch][p] == 0) return res;
res++; p = Tire[ch][p];
}
return res;
}
int main () {
read (n);
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
sort (c + 1, c + 1 + n, cmp);
int ans = 0;
for (int k = 1; k <= n; k++) {
ans = Max (ans, Query (c[k]));
Update (c[k]);
}
write (ans);
return 0;
}