字典树,ACM培训

                                 142. 前缀统计

目录

142. 前缀统计

143. 最大异或对

144. 最长异或值路径

P8306 【模板】字典树


142. 前缀统计 - AcWing题库

给定 N 个字符串 S1,S2…SN,接下来进行 M 次询问,每次询问给定一个字符串 T,求 S1∼SN中有多少个字符串是 T 的前缀。

输入字符串的总长度不超过 106,仅包含小写字母。

输入格式

第一行输入两个整数 N,M。

接下来 N 行每行输入一个字符串 Si。

接下来 M 行每行一个字符串 T 用以询问。

输出格式

对于每个询问,输出一个整数表示答案。

每个答案占一行。

数据范围

1≤N,M≤105

输入样例:
3 2
ab
bc
abc
abc
efg
输出样例:
2
0
难度:简单
时/空限制:1s / 256MB
总通过数:6869
总尝试数:12801
来源:《算法竞赛进阶指南》
算法标签

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const int  N = 1e6 + 5;
int n, m;
int trie[N][26], tot = 1, E[N];
void insert(const char* str) {
	int len = strlen(str), p = 1;
	for (int k = 0; k < len; k++) {
		int ch = str[k] - 'a';
		if (trie[p][ch] == 0)trie[p][ch] = ++tot;//注意,这里一定要先++
		p = trie[p][ch];
	}
	E[p] ++;
}
int search(const char* str) {
	int len = strlen(str), p = 1, ret = 0;
	for (int k = 0; k < len; k++) {
		p = trie[p][str[k] - 'a'];
		if (E[p])ret+=E[p];
		if (p == 0)return ret;
	}
	return ret;
}
int main() {
	cin >> n >> m;
	string str;
	for (int i = 1; i <= n; i++) {
		cin >> str;
		insert(str.c_str());
	}
	for (int i = 1; i <= m; i++) {
		cin >> str;
		printf("%d\n", search(str.c_str()));
	}
	return 0;
}

                                143. 最大异或对

143. 最大异或对 - AcWing题库

在给定的 N 个整数 A1,A2……AN中选出两个进行 xor(异或)运算,得到的结果最大是多少?

输入格式

第一行输入一个整数 N。

第二行输入 N 个整数 1~AN。

输出格式

输出一个整数表示答案。

数据范围

1≤N≤105,
0≤Ai<231

输入样例:
3
1 2 3
输出样例:
3

 解析:

将每个数以二进制方式存入字典树,找的时候从最高位去找有无该位的异

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>

using namespace std;
typedef long long LL;
const int  N = 40, M = 1e5 + 5;
int n;
int trie[N*M][2], tot = 1, E[N], arr[M];

void insert(int d) {
	int p = 1;
	for (int i = 30; i >= 0; i--) {
		int u = d >> i & 1;
		if (trie[p][u] == 0)trie[p][u] = ++tot;
		p = trie[p][u];
	}
}

int ask(int x) {
	int p = 1, ret = 0;
	for (int i = 30; i >= 0; i--) {
		int u = x >> i & 1;
		if (trie[p][!u]) {
			p = trie[p][!u];
			ret = ret * 2 + 1;
		}
		else {
			p = trie[p][u];
			ret = ret * 2+0;
		}
	}
	return ret;
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &arr[i]);
		insert(arr[i]);
	}
	int mx = 0;
	for (int i = 1; i <= n; i++) {
		//cout << ask(arr[i]) << endl;
		mx = max(mx, ask(arr[i]));
	}
	printf("%d\n", mx);
	return 0;
}

                        144. 最长异或值路径

 144. 最长异或值路径 - AcWing题库

给定一个树,树上的边都具有权值。

树中一条路径的异或长度被定义为路径上所有边的权值的异或和:

formula.png

⊕ 为异或符号。

给定上述的具有 n 个节点的树,你能找到异或长度最大的路径吗?

输入格式

第一行包含整数 n,表示树的节点数目。

接下来 n−1 行,每行包括三个整数 u,v,w表示节点 u 和节点 v 之间有一条边权重为 w

输出格式

输出一个整数,表示异或长度最大的路径的最大异或和。

数据范围

1≤n≤100000
0≤u,v<n
0≤w<231

输入样例:
4
0 1 3
1 2 4
1 3 6
输出样例:
7
样例解释

样例中最长异或值路径应为 0->1->2,值为 7(=3⊕4)

 解析:

先对题目所给的树进行一次dfs,算出所有的D[i]:从0到第i个点的异或和;

然后我们可以发现任意两个点i,j之间的路径的异或和就等于D[i],D[j]的异或和,则将题目转换成立上一道题目

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>

using namespace std;
typedef long long LL;
const int  N = 1e5 + 5;
int n;
vector<pair<int, int>>G[N];
int D[N];
int trie[N*40][2], tot = 1, E[N];


void dfs(int t,int fa) {
	for (int i = 0,j,d; i < G[t].size(); i++) {
		j = G[t][i].first;
		d = G[t][i].second;
		if (fa == j)
			continue;
		D[j] = D[t] ^ d;
		dfs(j, t);
	}
}

void insert(int x) {
	int p = 1;
	for (int i = 31,t; i >= 0; i--) {
		t = x >> i & 1;
		if (trie[p][t] == 0)trie[p][t] = ++tot;
		p = trie[p][t];
	}
}

LL ask(int x) {
	LL p = 1, ret = 0;
	for (int i = 31,t; i >= 0; i--) {
		t = x >> i & 1;
		if (trie[p][!t]) {
			p = trie[p][!t];
			ret = 2 * ret + 1;
		}
		else {
			p = trie[p][t];
			ret = 2 * ret + 0;
		}
	}
	return ret;
}

int main() {
	scanf("%d", &n);
	for (int i = 1,a,b,t; i < n; i++) {
		scanf("%d%d%d", &a, &b, &t);
		G[a].push_back({ b,t });
		G[b].push_back({ a,t });
	}
	dfs(0,-1);
	LL mx = 0;
	for (int i = 0; i < n; i++) {
		insert(D[i]);
	}
	for (int i = 0; i < n; i++) {
		mx = max(mx, ask(D[i]));
	}
	cout << mx << endl;
	return 0;
}

                        P8306 【模板】字典树

 P8306 【模板】字典树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目描述

给定 n 个模式串 1,2,…,s1​,s2​,…,sn​ 和 q 次询问,每次询问给定一个文本串 ti​,请回答 s1​∼sn​ 中有多少个字符串 sj​ 满足 ti​ 是 sj​ 的前缀

一个字符串 t 是 s 的前缀当且仅当从 s 的末尾删去若干个(可以为 0 个)连续的字符后与 t 相同。

输入的字符串大小敏感。例如,字符串 Fusu 和字符串 fusu 不同。

输入格式

本题单测试点内有多组测试数据

输入的第一行是一个整数,表示数据组数 T。

对于每组数据,格式如下:
第一行是两个整数,分别表示模式串的个数 n 和询问的个数 q。
接下来 n 行,每行一个字符串,表示一个模式串。
接下来 q 行,每行一个字符串,表示一次询问。

输出格式

按照输入的顺序依次输出各测试数据的答案。
对于每次询问,输出一行一个整数表示答案。

输入输出样例

输入 #1复制

3
3 3
fusufusu
fusu
anguei
fusu
anguei
kkksc
5 2
fusu
Fusu
AFakeFusu
afakefusu
fusuisnotfake
Fusu
fusu
1 1
998244353
9

输出 #1复制

2
1
0
1
2
1

说明/提示

数据规模与约定

对于全部的测试点,保证 1≤T,n,q≤105,且输入字符串的总长度不超过3×106。输入的字符串只含大小写字母和数字,且不含空串。

说明

std 的 IO 使用的是关闭同步后的 cin/cout,本题不卡常。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>

using namespace std;
typedef long long LL;
const int  N = 3e6 + 5;

int n, m;
int trie[N][65], tot = 1, E[N];
char str[N];
int getnum(char x) {
	if (x >= 'A' && x <= 'Z')
		return x - 'A';
	else if (x >= 'a' && x <= 'z')
		return x - 'a' + 26;
	else
		return x - '0' + 52;
}
void insert(const char* str) {
	int len = strlen(str), p = 1, t;
	for (int i = 0; i < len; i++) {
		t = getnum(str[i]);
		if (trie[p][t] == 0)trie[p][t] = ++tot;
		p = trie[p][t];
		E[p]++;
	}
}

int ask(const char* str) {
	int len = strlen(str), p = 1, t;
	for (int i = 0; i < len; i++) {
		t = getnum(str[i]);
		if (!trie[p][t])return 0;
		p = trie[p][t];

	}
	return E[p];
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		for (int i = 0; i <= tot; i++) {//这里用memset会超时
			for (int j = 0; j < 65; j++)
				trie[i][j] = 0;
		}
		for (int i = 0; i <= tot; i++)
			E[i] = 0;
		tot = 1;
		scanf("%d%d", &n, &m);

		for (int i = 1; i <= n; i++) {
			scanf("%s", str);
			//cin >> str;这里不能用cin,可以用scanf()
			insert(str);
		}
		for (int i = 0; i < m; i++) {
			scanf("%s", str);
			//cin >> str;
			printf("%d\n", ask(str));
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值