字典树

P5755 [NOI2000] 单词查找树

题意

  • 给定若干个字符串,输出它们构建的字典树的结点个数
  • 根节点也需要计算
  • 所有字符串长度的和不超过 3.2×1043.2\times 10^4

思路

  • 简单字典树的构建
  • 会写插入函数即可,最后的答案就是 cnt + 1(如果默认 0 为根的话)

code

const int N = 3.2e4 + 9;
int tr[N][26], tot = 1;

void ins(string s) {
	int p = 1;
	for(char c: s) {
		c -= 'A';
		if(tr[p][c] == 0) tr[p][c] = ++ tot;
		p = tr[p][c];
	}
}
int main() {
	
	string s;
	while(cin >> s) ins(s);
	cout << tot;
	
	return 0;
}
#F1047. 前缀冲突电话号

题意

  • 向集合中依次添加 n 个字符串,第 i 个字符串为 si
  • 如果当前添加的字符串是集合中某个已经存在字符串的前缀,则不予添加
  • 如果当前添加的字符串在集合中存在某个字符串是它的前缀,也不予添加
  • 求最终成功添加的字符串有多少个
  • 本题开启多组案例,所有案例中 n 的和不超过 3×1053\times 10^5
  • n≤105;  ∣si∣≤20n\le 10^5;\ \ |s_i| \le 20

思路

  • 创建所有成功插入字符串的字典树,并且在树上标记当前是否是终止结点
  • 那么如果某个字符串的插入路径经过了一个终止结点,说明存在这个字符串的前缀,不予添加
  • 如果某个字符串的插入路径没有创建新的结点,那么说明存在一个字符串以当前字符串为前缀,不予添加
  • 最终统计添加的字符串个数即可

code

const int N = 2e6 + 9;
int tr[N][10], tot = 1;
bool ed[N];

bool ins(string s) {
	int p = 1, pre = tot;
	for(char c: s) {
		c -= '0';
		if(tr[p][c] == 0) tr[p][c] = ++ tot;
		p = tr[p][c];
		if(ed[p] == true) return false;
	}
	if(pre == tot) return false;
	return ed[p] = true;
}
void solved() {
	int n;	cin >> n;
	int ans = 0;
	for(int i = 1; i <= n; i ++) {
		string s;	cin >> s;
		ans += ins(s);
	}
	cout << ans << endl;
	for(int i = 1; i <= tot; i ++) for(int j = 0; j <= 9; j ++) tr[i][j] = 0;
	for(int i = 1; i <= tot; i ++) ed[i] = false;
	tot = 1;
}
P2922 [USACO08DEC] Secret Message G

题意

  • 给定 n 个 01 串,第 i 个 01 串为 si
  • 进行 m 次询问,第 i 次询问给定一个 01 串 qi
    • 询问有多少个字符串是 qi 的前缀,或者 qi 是它的前缀
    • 相等也属于前缀的一种
  • $n,m\le 5\times 10^4;\ \ |s_i|,|q_i| \le 10^4;\ \ \sum |s_i| + \sum |q_i| \le 5\times 10^5$

思路

  • 同上题
  • 在每个位置记录结尾标记出现次数(可能有重复串)
    • 那么对于当前字符串扫过的位置累计其结尾标记
    • 这些是作为当前字符串前缀出现的字符串
  • 对于当前字符串做前缀的一些更长字符串
    • 可以对每个结点记录其下结尾标记的总和
    • 那么在最后当前字符串停留结点,累计其子树中所有结尾标记即可
  • 这样需要记录结尾标记,并统计结尾标记总和
    • 这里有个技巧,对于一个字符串,直接把其路过的所有位置的总和都 +1 即可维护

code

const int N = 5e5 + 9;
struct node {
	int l, r, ed, sum;
} tr[N];
int tot = 1;

void ins() {
	int n;	cin >> n;
	int p = 1;
	while(n --) {
		int c;	cin >> c;
		if(c == 0 && tr[p].l == 0) tr[p].l = ++ tot;
		if(c == 1 && tr[p].r == 0) tr[p].r = ++ tot;
		if(c == 0) p = tr[p].l;
		else p = tr[p].r;
		tr[p].sum ++;
	}
	tr[p].ed ++;
}
int ask() {
	int n;	cin >> n;
	int p = 1, ans = 0;
	while(n --) {
		int c;	cin >> c;
		if(c == 0 && tr[p].l == 0 || c == 1 && tr[p].r == 0) {
			while(n --) cin >> c;
			return ans;
		}
		if(c == 0) p = tr[p].l;
		else p = tr[p].r;
		ans += tr[p].ed;
	}
	return ans - tr[p].ed + tr[p].sum;
}
void solved() {
	int n, m;	cin >> n >> m;
	for(int i = 1; i <= n; i ++) ins();
	while(m --) cout << ask() << endl;
}
P4683 [IOI 2008] Type Printer

题意

  • 你需要打印共 n 个字符串,第 i 个字符串为 si,由小写英文字母构成
  • 你有一台打印机,最开始打印台为空
  • 你可以进行若干次操作,每次操作为如下之一
    • 在打印台后方追加一个任意字母,输出追加的小写字母即可
    • 删去打印台的最后一个字母,输出字符 -
    • 按顺序打印当前打印台上的所有字母,构成一个字符串,输出大写字母 P
  • 最终的打印台可以不清空
  • 问,最少需要执行多少次操作才能打印出这 n 个字符串
  • 并给出具体的操作步骤,若长度最短操作序列不唯一输出任意一种即可
  • n≤2.5×104;  ∣si∣≤20n\le 2.5\times 10^4;\ \ |s_i| \le 20

思路

  • 显然,按照字典树 dfs 是最优的
  • 而最后可以不清空,所以最后遍历那个长度最长的字符串,在它的结尾位置结束这一切即可
  • 由于 dfs 的过程中需要决策最晚走最长字符串所在分支,所以需要记录每个结点的最长字符串在哪个分支
  • 这个可以每次插入时
    • 比较当前插入字符串的长度是否超过以前字符串的长度
    • 如果超过就把此次经过的所有结点最长分支标记为当前走去的分支
  • 所以,记录每个结点最长分支来源,和最长分支长度,用长度决策来源是否需要修改即可
  • 特别的,如果某个结点最长分支来源为 -1(这里不能用 0,0 代表 a 分支),说明其为叶子结点,无需继续 dfs

code

const int N = 5e5 + 9;
int tr[N][26], tot = 1;
int mx[N], len[N];
bool ed[N];

void ins() {
	string s;	cin >> s;
	int p = 1;
	for(char c: s) {
		c -= 'a';
		if(len[p] < s.size()) len[p] = s.size(), mx[p] = c;
		if(tr[p][c] == 0) tr[p][c] = ++ tot, mx[tot] = -1;
		p = tr[p][c];
	}
	ed[p] = true;
}
vector<char> ans;
void dfs(int p) {
	if(ed[p]) ans.push_back('P');
	for(int i = 0; i <= 25; i ++) {
		if(i == mx[p]) continue;
		if(tr[p][i] == 0) continue;
		ans.push_back((char)(i + 'a'));
		dfs(tr[p][i]);
	}
	if(mx[p] != -1) {
		ans.push_back((char)(mx[p] + 'a'));
		dfs(tr[p][mx[p]]);
	}
	ans.push_back('-');
}
void solved() {
	int n;	cin >> n;
	while(n --) ins();
	dfs(1);
	while(ans.back() == '-') ans.pop_back();
	cout << ans.size() << endl;
	for(char c: ans) cout << c << endl;
}

01 trie

#F1048. 最大异或和

题意

  • 给定 n 个数,第 i 个数记为 aia_i。
  • 需要从中选出两个元素 axa_x 和 aya_y,使得 ax⊕aya_x \oplus a_y 尽可能大,输出这个最大值(这里 ⊕\oplus 表示按位异或运算)
  • 开启多组案例
  • 1≤n≤105, 0≤ai≤1061\le n \le 10^5,\ 0\le a_i \le 10^6

思路

  • 01 trie 经典例题
  • 我们思考,对于某个数字 x,如果其需要在一个集合中选取一个数和其异或,且最终结果尽可能大我们会如何选取
    • 优先考虑最高位,由于 aia_i 不超过 10610^6,所以最高位最多是 2192^{19}
    • 那么如果最高位存在能够取到与 x 相反的位的数,必定会从这些数中再选取次高位
    • 对于次高位也是一样的
  • 总体来说,从高位到低位,贯彻,能反着取就反着取的路线,最后取出来的路线对应的数字就是答案
  • 那么把每个数的 2 进制表示对应的 01 串,从高位到低位插入到 trie 中
  • 对于每个元素查询,遍历 trie 树,从高位到低位,能取反就取反,贪心获得最大值即可

code

const int N = 1e5 * 20 + 9;
int tr[N][2], tot = 1;
int a[N];

void ins(int x) {
	int p = 1;
	for(int i = 19; i >= 0; i --) {
		int c = x >> i & 1;
		if(tr[p][c] == 0) tr[p][c] = ++ tot;
		p = tr[p][c];
	}
}
int ask(int x) {
	int ans = 0, p = 1;
	for(int i = 19; i >= 0; i --) {
		int c = x >> i & 1;
		if(tr[p][c ^ 1] != 0) p = tr[p][c ^ 1], ans += 1 << i;
		else p = tr[p][c];
	}
	return ans;
}
void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i];
		ins(a[i]);
	}
	int ans = 0;
	for(int i = 1; i <= n; i ++) ans = max(ans, ask(a[i]));
	cout << ans << endl;
	for(int i = 1; i <= tot; i ++) tr[i][0] = tr[i][1] = 0;
	tot = 1;
}
P4735 最大异或和

code

const int N = 3e5 + 9;
int tr[N * 25 * 2][2], tot = 1;
int a[N * 2], rt[N * 2], s[N * 25 * 2][2];

void ins(int p, int q, int x) {
	tr[q][0] = tr[p][0], tr[q][1] = tr[p][1];
	s[q][0] = s[p][0], s[q][1] = s[p][1];
	for(int i = 23; i >= 0; i --) {
		int c = x >> i & 1;
		s[q][c] ++;	// 新版本,多一条边 
		q = tr[q][c] = ++ tot;	// 新版本,走向新结点
		p = tr[p][c];	// 旧版本,朝相同方向走过去
		// 如果旧版本不存在对应方向结点,会发生什么?
		// 会赛博鬼打墙,利用了空为 0,且 tr[0][0] = tr[0][1] = 0 
		tr[q][0] = tr[p][0], tr[q][1] = tr[p][1];
		s[q][0] = s[p][0], s[q][1] = s[p][1];
	}
} 
int ask(int l, int r, int x) {
	int ans = 0;
	for(int i = 23; i >= 0; i --) {
		int c = x >> i & 1;
		if(s[r][c ^ 1] - s[l][c ^ 1] > 0) {
			l = tr[l][c ^ 1], r = tr[r][c ^ 1];
			ans += 1 << i;
		} else {
			l = tr[l][c], r = tr[r][c];
		}
	}
	return ans;
}
void solved() {
	int n, m;	cin >> n >> m;
	rt[0] = 0;
	rt[1] = 1;
	ins(rt[0], rt[1], 0);
	n ++;
	for(int i = 2; i <= n; i ++) {
		cin >> a[i];
		a[i] ^= a[i - 1];	// 现在是前缀 
		rt[i] = ++ tot;
		ins(rt[i - 1], rt[i], a[i]);
	}
	while(m --) {
		char op;	cin >> op;
		if(op == 'A') {
			cin >> a[++ n];	a[n] ^= a[n - 1];
			rt[n] = ++ tot;
			ins(rt[n - 1], rt[n], a[n]);
		} else {
			int l, r, x;	cin >> l >> r >> x;
			cout << ask(rt[l - 1], rt[r], x ^ a[n]) << endl;
		}
	}
}

0 条评论

目前还没有评论...