其他分享
首页 > 其他分享> > 字典树(前缀树)

字典树(前缀树)

作者:互联网

二维数组实现字典树 比较方便

#include <iostream>
#include <string>

using namespace std;

bool tri[100000][26] = { false };

void Insert(string& str) {
	int n = str.length();
	for (int i = 0; i < n; i++) {
		auto idx = str[i] - 'a';
		tri[i][idx] = true;
	}
}

bool research(string& str) {
	int n = str.length();
	for (int i = 0; i < n; i++) {
		auto idx = str[i] - 'a';
		if (!tri[i][idx])return false;
	}
	return true;
}


int main() {
	string str1 = "", str2 = "";
	while (cin >> str1 >> str2) {
		Insert(str1);
		if (research(str2))cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}

标签:tri,前缀,idx,int,str2,str1,str,字典
来源: https://blog.csdn.net/My_Joker/article/details/118367310