社区讨论

有没有喜欢写struct封装过的Trie的dalao

学术版参与者 3已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@mi7d0xaa
此快照首次捕获于
2025/11/20 19:40
4 个月前
此快照最后确认于
2025/11/20 19:40
4 个月前
查看原帖
直角,可以给本蒟蒻一份吗?谢谢!
附上自己一道题用Trie写的丑陋代码
CPP
#include <bits/stdc++.h>
#define re register
using namespace std;
const int N = 500005;
int n, m, num;
struct Node{
	int cnt, vis, son[26];
	Node(){
		memset(son, 0, sizeof(son));
		cnt = vis = 0;
	}
}trie[N];
inline void insert(string s){
	int u = 0, v;
	for (re int i = 0; i < s.size(); ++i){
		v = s[i] - 97;
		if (!trie[u].son[v]){
			trie[u].son[v] = ++num;
		}
		u = trie[u].son[v];
	}
	trie[u].vis = 1;
}
inline int ask(string s){
	int u = 0, v;
	for (re int i = 0; i < s.size(); ++i){
		v = s[i] - 'a';
		if (!trie[u].son[v])return 0;
		else u = trie[u].son[v];
	}
	if (!trie[u].vis)return 0;
	if (!trie[u].cnt){
		++trie[u].cnt;
		return 1;
	}
	return 2;
}
inline int read(){
	int f = 0, x = 0; char ch;
	do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
	do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
	return f ? -x : x;
}
int main(){
	n = read();
	for (re int i = 1; i <= n; ++i){
		string s;
		cin >> s;
		insert(s);
	}
	m = read();
	for (re int i = 1; i <= m; ++i){
		string s;
		cin >> s;
		int judge = ask(s);
		if (!judge){
			puts("WRONG");
		}
		else{
			if (judge == 1)puts("OK");
			else puts("REPEAT");
		}
	}
	return 0;
} 

回复

3 条回复,欢迎继续交流。

正在加载回复...