跳到正文

题解

P2375 [NOI2014] 动物园 题解报告

wuwendan约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年9月24日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 7 分钟
题目
P2375

版本

  1. v1当前

Problem

给定一个字符串 ss,求 ss 的每个前缀的特殊 border 数量,其中特殊 border 指的是 len≤i/2len \le i/2 的 border。

Solution

这里给出三种本质不同的做法

朴素通解

首先 nxtnxt 数组是好求的,如何计算 numnum 数组呢?考虑直接每次暴力跳 nxtnxt,直到 len≤i/2len \le i/2。

时间复杂度 O(n2)O(n^2)。

考虑优化,发现貌似可以倍增,然后就做完了,但是要注意一个细节:倍增二维数组的较小的一维放在前面可以显著减小常数。

时间复杂度 O(n log n)O(n~log~n)。

C++
#include <bits/stdc++.h>
#define rg register
#define ll long long
#define dd double
using namespace std;
template <typename T> inline void D(T &x)
{
	x = 0; bool f = 0; char ch = getchar();
	while('0' > ch || ch > '9') { if(ch == '-') f = !f; ch = getchar(); }
	while('0' <= ch && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
	x = f ? -x : x; return;
}
template <typename T> inline void P(T x)
{
	if(x < 0) putchar('-'), x = -x;
	if(x > 9) P(x / 10);
	putchar(x % 10 + '0'); return;
}
const int N = 1e6 + 10, mod = 1e9 + 7;
char s[N];
int n, nxt[25][N], sum[N];
void solve()
{
	cin >> s + 1;
	n = strlen(s + 1);
	for (int i = 2, p = 0; i <= n; i ++)
	{
		while (p && s[p + 1] != s[i]) p = nxt[0][p];
		nxt[0][i] = p = p + (s[p + 1] == s[i]);
	}
	for (rg int j = 1; j <= 20; ++ j)
	{
		for (rg int i = 1; i <= n; ++ i) nxt[j][i] = nxt[j - 1][nxt[j - 1][i]];
	}
	memset(sum, 0, sizeof(sum));
	for (int i = 1; i <= n; i ++) sum[i] = sum[nxt[0][i]] + 1;
	ll ans = 1;
	for (rg int i = 1; i <= n; ++ i)
	{
		int now = i;
		for (rg int j = 20; j >= 0; -- j)
		{
			if (nxt[j][now] > i / 2) now = nxt[j][now];
		}
		if (now > i / 2) now = nxt[0][now];
		ans = ans * (sum[now] + 1) % mod;
	}
	cout << ans << '\n';
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	//freopen(".in", "r", stdin); freopen(".out", "w", stdout);
	int T; cin >> T;
	while (T --) solve();
	return 0;
}

鄙人拙解

目前没有在题解区看到这样的做法。

从后往前做,还是暴力跳 nxtnxt,最终跳到 numnum,但是每次跳完,都把沿途经过的 nxtnxt 赋值为 numnum,这样是由正确性的,因为我们本身是从后往前做,长度是逐渐缩短的。

时间复杂度:不知道是不是均摊 O(n)O(n),至少实际表现是线性,比倍增快 55 倍。

C++
#include <bits/stdc++.h>
#define rg register
#define ll long long
#define dd double
using namespace std;
template <typename T> inline void D(T &x)
{
	x = 0; bool f = 0; char ch = getchar();
	while('0' > ch || ch > '9') { if(ch == '-') f = !f; ch = getchar(); }
	while('0' <= ch && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
	x = f ? -x : x; return;
}
template <typename T> inline void P(T x)
{
	if(x < 0) putchar('-'), x = -x;
	if(x > 9) P(x / 10);
	putchar(x % 10 + '0'); return;
}
const int N = 1e6 + 10, mod = 1e9 + 7;
char s[N];
int n, nxt[N], sum[N];
void solve()
{
	cin >> s + 1;
	n = strlen(s + 1);
	for (int i = 2, p = 0; i <= n; i ++)
	{
		while (p && s[p + 1] != s[i]) p = nxt[p];
		nxt[i] = p = p + (s[p + 1] == s[i]);
	}
	memset(sum, 0, sizeof(sum));
	for (int i = 1; i <= n; i ++) sum[i] = sum[nxt[i]] + 1;
	ll ans = 1;
	for (rg int i = n; i >= 1; i --)
	{
		int now = i;
		while (now > i / 2) now = nxt[now];
		int k = now;
		now = i;
		while (now > i / 2)
		{
			int y = nxt[now];
			nxt[now] = k;
			now = y;
		}
		if (now > i / 2) now = nxt[now];
		ans = ans * (sum[now] + 1) % mod;
	}
	cout << ans << '\n';
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	//freopen(".in", "r", stdin); freopen(".out", "w", stdout);
	int T; cin >> T;
	while (T --) solve();
	return 0;
}

题解区妙解

第一次 KMP 求完 nxtnxt 数组之后,再做一次 KMP。

此时如果发现长度不符合要求,依旧暴力跳,因为每次最多是指针 +1+1,而每次跳可以使指针至少 −1-1,所以最多跳 O(n)O(n) 次,因此最终时间复杂度 O(n)O(n)。

正确性证明:如果一个子串可以继续往后匹配,那么它的 border 也可以。

原文