跳到正文

题解

[JLOI2016] 字符串覆盖 题解

StarPatrick约 8 分钟
原文
文章信息与版本

文章信息

发布
2023年11月7日
收录
2026年7月21日
更新
2026年11月16日
篇幅
约 8 分钟
获赞
3
题目
P3269

版本

  1. v1当前

写一篇 SA 的题解,只不过这个 SA 是模拟退火,不是后缀数组。

为什么会想到模拟退火呢,观察到 N≤4N\le4,自然浮现搜索的想法,但是搜索也是 O(∣A∣N)O(|A|^N) 的复杂度,过不了,加了减枝应该也不好过(也许可以启发式搜索什么的,没试过),因此想到模拟退火。

这题模拟退火比较好想,直接哈希把能匹配的位置找出来退火就好,这部分的复杂度是 O(n)O(n) 的。

具体模拟退火的过程就记录 u1,u2,u3,u4u1,u2,u3,u4 表示在 4 个串中匹配的位置,计算答案就直接把四个区间求并集,排序即可,计算答案是 O(1)O(1) 的。

参数看代码即可,就不多赘述。

C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int base = 31, mod = 1e9+7;
int t, n, m, len[5], MIN, MAX, tot;
ll H[10005], pw[10005];
char a[10005], b[1005];
vector<int> v[5];
mt19937 Rand(time(0));
uniform_real_distribution<double> zqw(0, 1);
struct e{
	int l, r;
}temp[5];
inline bool cmp(e x, e y) {
	return x.l<y.l;
}
inline int calc(int u1, int u2, int u3, int u4) {
	tot = 0;
	temp[++tot] = {v[1][u1], v[1][u1]+len[1]-1};
	if (m>=2) temp[++tot] = {v[2][u2], v[2][u2]+len[2]-1};
	if (m>=3) temp[++tot] = {v[3][u3], v[3][u3]+len[3]-1};
	if (m>=4) temp[++tot] = {v[4][u4], v[4][u4]+len[4]-1};
	stable_sort(temp+1, temp+tot+1, cmp);
	int last = temp[1].r, u = temp[1].r-temp[1].l+1;
	for (int p=2;p<=tot;p++) {
		if (temp[p].l>last) {
			u+=temp[p].r-temp[p].l+1;
			last = temp[p].r;
		}
		else if (temp[p].r>last) {
			u+=temp[p].r-last;
			last = temp[p].r;
		}
	}
	return u;
}
void SAMIN() {
	uniform_int_distribution<int> rand1(-v[1].size(), v[1].size()), rand2(-v[2].size(), v[2].size()), rand3(-v[3].size(), v[3].size()), rand4(-v[4].size(), v[4].size());
	int u1 = rand1(Rand), u2 = rand2(Rand), u3 = rand3(Rand), u4 = rand4(Rand);
	u1 = (u1+v[1].size())%v[1].size();
	if (m>=2) u2 = (u2+v[2].size())%v[2].size();
	if (m>=3) u3 = (u3+v[3].size())%v[3].size();
	if (m>=4) u4 = (u4+v[4].size())%v[4].size();
	int now = calc(u1, u2, u3, u4);
	MIN = min(MIN, now);
	MAX = max(MAX, now);
	double T = 100;
	while (T>1) {
		int d1 = u1+(int)rand1(Rand)*T/100, d2 = u2+(int)rand2(Rand)*T/100, d3 = u3+(int)rand3(Rand)*T/100, d4 = u4+(int)rand4(Rand)*T/100;
		d1 = (d1+v[1].size())%v[1].size();
		if (m>=2) d2 = (d2+v[2].size())%v[2].size();
		if (m>=3) d3 = (d3+v[3].size())%v[3].size();
		if (m>=4) d4 = (d4+v[4].size())%v[4].size();
		int fut = calc(d1, d2, d3, d4);
		MIN = min(MIN, fut);
		MAX = max(MAX, fut);
		if (fut<=now||exp((now-fut)/sqrt(T))>zqw(Rand)) {
			now = fut;
			u1 = d1, u2 = d2, u3 = d3, u4 = d4;
		}
		T*=0.99;
	}
	return ;
}
void SAMAX() {
	uniform_int_distribution<int> rand1(-v[1].size(), v[1].size()), rand2(-v[2].size(), v[2].size()), rand3(-v[3].size(), v[3].size()), rand4(-v[4].size(), v[4].size());
	int u1 = rand1(Rand), u2 = rand2(Rand), u3 = rand3(Rand), u4 = rand4(Rand);
	u1 = (u1+v[1].size())%v[1].size();
	if (m>=2) u2 = (u2+v[2].size())%v[2].size();
	if (m>=3) u3 = (u3+v[3].size())%v[3].size();
	if (m>=4) u4 = (u4+v[4].size())%v[4].size();
	int now = calc(u1, u2, u3, u4);
	MIN = min(MIN, now);
	MAX = max(MAX, now);
	double T = 2e5;
	while (T>0.001) {
		int d1 = u1+(int)rand1(Rand)*T/200000, d2 = u2+(int)rand2(Rand)*T/200000, d3 = u3+(int)rand3(Rand)*T/200000, d4 = u4+(int)rand4(Rand)*T/200000;
		d1 = (d1+v[1].size())%v[1].size();
		if (m>=2) d2 = (d2+v[2].size())%v[2].size();
		if (m>=3) d3 = (d3+v[3].size())%v[3].size();
		if (m>=4) d4 = (d4+v[4].size())%v[4].size();
		int fut = calc(d1, d2, d3, d4);
		MIN = min(MIN, fut);
		MAX = max(MAX, fut);
		if (fut>=now||exp((fut-now)/sqrt(T))>zqw(Rand)) {
			now = fut;
			u1 = d1, u2 = d2, u3 = d3, u4 = d4;
		}
		T*=0.99;
	}
	return ;
}
int main() {
	pw[0] = 1;
	for (int p=1;p<=10000;p++) {
		pw[p] = pw[p-1]*base%mod;
	}
	scanf("%d", &t);
	for (int zqw=1;zqw<=t;zqw++) {
		MIN = 1e9, MAX = 0;
		cin>>a+1;
		n = strlen(a+1);
		for (int p=1;p<=n;p++) {
			H[p] = (H[p-1]*base+a[p]-'a'+1)%mod;
		}
		scanf("%d", &m);
		for (int p=1;p<=m;p++) {
			v[p].clear();
			cin>>b+1;
			len[p] = strlen(b+1);
			ll u = 0;
			for (int k=1;k<=len[p];k++) {
				u = (u*base+b[k]-'a'+1)%mod;
			}
			for (int k=1;k<=n-len[p]+1;k++) {
				if ((H[k+len[p]-1]-H[k-1]*pw[len[p]]%mod+mod)%mod==u) v[p].push_back(k);
			}
		}
		for (int p=1;p<=300;p++) {
			SAMIN();
		}
		SAMAX();
		printf("%d %d\n", MIN, MAX);
	}
    return 0;
}
原文
存档评论2 条抓取于