DS哈希查找--Trie树(C++)两种代码

代码1:

#include<iostream>
#include<queue>
using namespace std;
struct Node{
	int *v;//是否有字母
	char c;
	Node **k;
	Node(){
		c='0';
	k=new Node*[26];
	v=new int[26];
	for(int i=0;i<26;i++){
	v[i]=0;
	}
	}

};
int sum;
void Sum(Node*&p){
	int i,j=0;
	for(i=0;i<26;i++){
		if(p->v[i]==1){
			j++;
			Sum(p->k[i]);
		}
	}
	if(j==0)
	sum++;
}
int main(){
	int n,m,i,j,K;
	Node *r=new Node;
	Node *p;
	char c,ch;
	while((ch=getchar())!='\n'){
	//换行符表示所有单词输入结束;外层循环:ch为单词的第一个字母
		if(r->v[ch-97]==0){
		r->v[ch-97]=1;
		r->k[ch-97]=new Node;
		r->k[ch-97]->c=ch;
		}
		p=r->k[ch-97];
     	while((ch=getchar())!=' '){
		 if(ch=='\n')
		 break;
     	if(p->v[ch-97]==0){
	    	p->v[ch-97]=1;
	    	p->k[ch-97]=new Node;
	    	p->k[ch-97]->c=ch;
		 }
	     	p=p->k[ch-97];
     	}
     	if(ch=='\n'){
     	break;
		}
	}//层次遍历
	queue<Node*> qu;
	for(i=0;i<26;i++){
		if(r->v[i]==1){
    	qu.push(r->k[i]);
		}
	}
	while(!qu.empty()){
		Node*p=qu.front();
		qu.pop();
		cout<<p->c;
		for(i=0;i<26;i++)
		if(p->v[i]==1){
			qu.push(p->k[i]);
		}
	}
	cout<<endl;
	cin>>n;
	string s;
	int length;
	while(n--){
		sum=0;
		cin>>s;
		length=s.length();
		i=0;
		Node *p=r;
		while(i<length){
			K=0;
			for(j=0;j<26;j++){
				if(p->k[j]&&p->k[j]->c==s[i]){
					K=1;
				break;
				}
			}
			if(K==0)
			break;
			else{
				p=p->k[j];
				i++;
			}
		}
		if(K==0){
         	cout<<0<<endl;
         	continue;
		}
		Sum(p);
		cout<<sum<<endl;
	}
	return 0;
}

代码2:

#include<iostream>
#include<string>
#include<queue>
using namespace std;
int sum;
class Trie {
public:
    bool flag = false;
    Trie* next[26] = { nullptr };
    friend void BFS();
 
    void insert(string str) {
        Trie* node = this;
        for (int i = 0; i < str.length(); i++) {
            char c = str[i];
            if (node->next[c - 'a'] == nullptr) {
                node->next[c - 'a'] = new Trie();
            }
            node = node->next[c - 'a'];
        }
        node->flag = true;
    }
 
    int searchStart(string pre) {
        Trie* node = this;
        sum=0;
        for (int i = 0; i < pre.length(); i++) {
            char c = pre[i];
            if (node->next[c - 'a'] == nullptr)  return 0;
            node = node->next[c - 'a'];
        }
        DFS(node);
        return sum;
    }
    void DFS(Trie* Node) {
        Trie* tr = new Trie();
        tr = Node;
        int temp=0;
        for (int i = 0; i < 26; i++) {
            if (tr->next[i])
            {
                DFS(tr->next[i]);
                temp = 1;
            }   
        }
        if (temp == 0) {
            sum++;
            return;
        }
    }
    
};
 
void BFS(Trie* Tree) {
    queue<Trie*>q;
    q.push(Tree);
    while (!q.empty()) {
        Trie* t = q.front();
        q.pop();
        for (int i = 0; i < 26; i++) {
            if (t->next[i]) {
                cout <<char( 'a' + i);
                q.push(t->next[i]);
            }
        }
    }
}
 
int main() {
    string arr;
    Trie* tree=new Trie();
    int t;
    while (1) {
        cin >> arr;
        if (arr[0] >= '0' && arr[0] <= '9') {
            t = arr[0] - '0';
            break;
        }
        tree->insert(arr);
    }
    BFS(tree);
    cout << endl;
    while (t--) {
        string test; cin >> test;
        cout << tree->searchStart(test)<<endl;
    }
}

 

 

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草海桐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值