字典树 Trie

#include<string>
#include<iostream>

using namespace std;

struct node {
	node* nxt[26];
	int flag;
	node() {
		for (int i = 0; i < 26; i++) nxt[i] = nullptr;
	}
};

node* root;

void init() {
	root = new node();
}

void ins(string s) {
	int n = s.size();
	node* now = root;

	for (int i = 0; i < n; i++) {
		int to = s[i] - 'a';
		if (now->nxt[to] == nullptr) now->nxt[to] = new node();

		now = now->nxt[to];
	}

	now->flag ++;
}
int fid(string t) {
	int n = t.size();
	node* now = root;
	for (int i = 0; i < n; i++) {
		int to = t[i] - 'a';
		if (now->nxt[to] == nullptr) return -1;
		now = now->nxt[to];
	}
	return now->flag;

}



//做题///
#include<string>
using namespace std;


void trie() {
	int nxt[100000][26], cnt;
	bool exist[100000]; //

	void insert(string s) {
		int n = s.size();
		int p = 0;
		for (int i = 0; i < n; i++) {
			int c = s[i] - 'a';
			if (!nxt[p][c]) nxt[p][c] = ++cnt;
			p = nxt[p][c];
		}
		exist[p] = 1;

	}

	bool find(string t) {
		int p = 0;
		int n = t.size();
		for (int i = 0; i < n; i++) {
			int c = t[i] - 'a';
			if (!nxt[p][c]) return false;;
			p = nxt[p][c];
		}
		return exist[p];
	}
}
上一篇:#2742. 「JOI Open 2016」销售基因链


下一篇:【YBTOJ】【Luogu P2444】[POI2000]病毒