跳到正文

题解

题解:P3823 [NOI2017] 蚯蚓排队

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

文章信息

发布
2025年2月24日
收录
2026年7月24日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
3
题目
P3823

版本

  1. v1当前

前置知识:哈希表
注意到 ss 的长度很长,要用字符串输入,就可以想到字符串哈希

注意到 k≤50k\le50,我们就可以统计一下每个队伍里面长度不超过 5050 的字符串的哈希值,这样就可以用哈希表了。

我们只需要在两个队伍合并时用哈希表统计一下所有新增的哈希值,分开两个队伍时用哈希表统计一下所有减去的哈希值,每次通过哈希表查询一下这个哈希值出现了多少次,就可以了。可以用开散列解决哈希冲突。注意:要用快读。

C++
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
const ull k = 131 , modhash = 10000019 , maxn = 1e7 + 5 , mod = 998244353;
int n , m , fa[200005] , son[200005];
ull jc[200005] = {1} , a[200005] , hashs[200005];
struct hash_table
{
	int cnt , head[modhash] , nxt[maxn];
	ull value[maxn];
	ll sl[maxn];
	inline void add (ull hashvalue , int v)
	{
		int tb = hashvalue % modhash;
		for (int i = head[tb];i;i = nxt[i])
			if(value[i] == hashvalue)
			{
				sl[i] = (sl[i] + v) % mod;
				return;
			}
		value[++ cnt] = hashvalue;
		sl[cnt] ++;
		nxt[cnt] = head[tb];
		head[tb] = cnt;
	}
	inline ll query (ull hashvalue)
	{
		int tb = hashvalue % modhash;
		for (int i = head[tb];i;i = nxt[i])
			if(value[i] == hashvalue) return sl[i];
		return 0ll;
	}
}Hash;
inline int read ()
{
	int x = 0;
	char c = getchar ();
	while (c < '0' || c > '9') c = getchar ();
	while ('0' <= c && c <= '9')
	{
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar ();
	}
	return x;
}
inline string readstr ()
{
	char c = getchar ();
	string ans = "";
	while (c == ' ' || c == '\n') c = getchar ();
	while (c != ' ' && c != '\n')
	{
		ans += c;
		c = getchar ();
	}
	return ans;
}
inline void write (ll s)
{
	if (s > 9) write (s / 10);
	putchar (s % 10 + '0');
}
inline void merge (int u , int v)
{
	son[u] = v;
	fa[v] = u;
	ull hashh = 0;
	for (int i = u , len = 1;i && len <= 50;i = fa[i] , len ++)
	{
		hashh += a[i] * jc[len - 1];
		ull hash2 = hashh;
		for (int j = v , len2 = len + 1;j && len2 <= 50;j = son[j] , len2 ++)
		{
			hash2 = hash2 * k + a[j];
			Hash.add (hash2 , 1);
		}
	}
}
inline void cut (int u , int v)
{
	son[u] = fa[v] = 0;
	ull hashh = 0;
	for (int i = u , len = 1;i && len <= 50;i = fa[i] , len ++)
	{
		hashh += a[i] * jc[len - 1];
		ull hash2 = hashh;
		for (int j = v , len2 = len + 1;j && len2 <= 50;j = son[j] , len2 ++)
		{
			hash2 = hash2 * k + a[j];
			Hash.add (hash2 , -1);
		}
	}
}
int main ()
{
	n = read ();
	m = read ();
	for (int i = 1;i <= n;i ++) a[i] = read ();
	for (int i = 1;i <= n;i ++) Hash.add (a[i] , 1);
	for (int i = 1;i <= 200000;i ++) jc[i] = jc[i - 1] * k;
	while (m --)
	{
		int opt = read ();
		if (opt == 1)
		{
			int u = read () , v = read ();
			merge (u , v);
		}
		else if (opt == 2)
		{
			int u = read ();
			cut (u , son[u]);
		}
		else
		{
			string s = readstr ();
			int kk = read () , len = s.size ();
			for (int i = 1;i <= len;i ++) hashs[i] = hashs[i - 1] * k + s[i - 1] - '0';
			ll ans = 1;
			for (int i = kk;i <= len;i ++) (ans *= Hash.query (hashs[i] - hashs[i - kk] * jc[kk])) %= mod;
			write (ans);
			putchar ('\n');
		}
	}
}

引用:CSDN博客

原文
存档评论1 条抓取于