跳到正文

题解

P4595 [COCI 2011/2012 #5] POPLOČAVANJE 详解

T0urist约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年7月22日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 6 分钟
题目
P4595

版本

  1. v1当前

题意

题意不是很清晰,其实是把那个街道上和瓷砖图案相等的字串全部认为是好的,不能被覆盖的叫做不好的。

思路

想不出正解先打暴力。

匹配问题最好写的无疑是哈希,不会取出一段字符串哈希值的同学可以看这篇 文章。

我们每次匹配,若是成功则暴力打上标记,然后跳到成功的字符串的末尾,继续匹配。

时间复杂度 Θ(nm)\Theta(nm),对于四秒的时限还是比较卡的,我们尝试去优化常数。

考虑到在长度较小的时候匹配次数会较多,因为跳的不多,但是由于打标记的原因,有用的字符串会比较少,所以离线下来去重。

然后我们尝试优化打标记的过程,不使用 bitset,发现多次区间的修改,只有一次的查询,这时想到用树状数组,但实际上只需要使用它的内核差分数组。

对于每个文本串中的字符,只匹配开头是这个字符的模式串,也就是对模式串按照第开头字母进行分类。

最后,我们可以对每一类按长度从大到小排序,因为长度大的一定可以覆盖长度小的标记。

Code

C++
//# pragma GCC optimize("Ofast")
# include <bits/stdc++.h>
# define fr front
# define il inline
# define fir first
# define sec second
# define vec vector
# define it iterator
# define pb push_back
# define lb lower_bound
# define ub upper_bound
# define all(x) x.begin(), x.end()
# define mem(a, b) memset(a, b, sizeof(a))

# define lc (t[p].l)
# define rc (t[p].r)
# define ls(x) (x << 1)
# define rs(x) (x << 1 | 1)
# define lson ls(p), l, mid
# define rson rs(p), mid + 1, r

# define sqr(x) ((x) * (x))
# define bpc __builtin_popcount
# define lowbit(x) ((x) & (-(x)))
# define geti(x, i) (((x) >> (i)) & 1)
# define set1(x, i) ((x) | (1 << (i)))
# define set0(x, i) ((x) & (~(1 << (i))))

# define debug1(x) cerr << #x << " = " << x << " "
# define debug2(x) cerr << #x << " = " << x << "\n"
# define bug cerr << "--------------------------\n"

# define each1(i, x) for(auto (i) : (x))
# define each2(i, x) for(auto (&i) : (x))
# define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
# define pre(i, a, b) for(int i = (a); i >= (b); -- i)
# define G(i, h, u, ne) for(int i = (h[(u)]); i; i = (ne[(i)]))
# define reps(i, a, b, c) for(int i = (a); i <= (b); i += (c))
# define pres(i, a, b, c) for(int i = (a); i >= (b); i -= (c))
using namespace std;

using DB = double;
using LL = long long;
using LDB = long double;
using PII = pair<int, int>;
using ULL = unsigned long long;

const int N = 3e5 + 10, M = 5e3 + 10, P = 131;
const int INF1 = 0x3f3f3f3f, INF2 = INT_MAX;
const LL INF3 = (LL)1e18, INF4 = 0x3f3f3f3f3f3f3f3f, INF5 = LLONG_MAX;

int n, m, len, sum, ans;
int cnt[N];
ULL val;
ULL h[N], p[N];

char s[N], str[M];

vec<pair<int, ULL>> a[30];

ULL query(int l, int r){
	return h[r] - h[l - 1] * p[r - l + 1];
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	cin >> n;
	
	cin >> s + 1;
	
	p[0] = 1;
	rep(i, 1, n){
		h[i] = h[i - 1] * P + s[i];
		p[i] = p[i - 1] * P;
	}
	
	cin >> m;
	
	while(m --){
		cin >> str + 1;
		val = 0;
		len = strlen(str + 1);
		rep(i, 1, len) val = val * P + str[i];
		a[str[1] - 'a' + 1].pb({len, val});
	}
	
	rep(i, 1, 26) sort(all(a[i]), greater<pair<int, ULL>>()), a[i].erase(unique(all(a[i])), a[i].end());//分类去重
	
	rep(i, 1, n){
		each2(tmp, a[s[i] - 'a' + 1]){//只枚举这一类
			len = tmp.fir, val = tmp.sec;
			int j = i + len - 1;
			if(j > n) continue;
			if(query(i, j) == val){
				cnt[i] ++;
				cnt[j + 1] --;
				break;//覆盖了
			}
		}
	}
	
	rep(i, 1, n) sum += cnt[i], ans += (!sum);
	
	cout << ans;
	
	return 0;
}

當月光灑在我的臉上,我想我就快變了模樣…………

原文