蒟蒻的ACM数据结构(三)-字典树

一、何为字典树

  简单讲,就是把一串字符,将每个字符当做一个节点,建立成一棵树
  举个栗子,我们有:a,ahat,hat,hatword,hziee,word。这么一撮单词,建成什么样子呢?
  如下图:
1

二、代码实现

(一)树本体

typedef struct TreeNode* tn;
struct TreeNode{
	int cnt;
	tn next[54];
	bool exist;
};
tn root;

  root是根,next指向下一个字母。cnt指该字母出现的个数,exist指在这里是否可以形成一个单词。
  像这样:
3

(二)建树

tn New(tn p)
{
	p = new(TreeNode);
	memset(p->next, NULL, sizeof(p->next));
	return p;
}

void CreatTree(tn p,string s)
{
	int len=s.length();
	for(int i=0;i<len;i++){
        int x;
		if('a'<=s[i]&&s[i]<='z')
            x=s[i]-'a';
        else
            x=s[i]-'A'+26;
		if(p->next[x]==NULL)
			p->next[x]=New(p->next[x]);
		p=p->next[x];
		p->cnt++;
	}
	p->exist=1;
}

  简单粗暴,直接把一个字符串从头到尾撸进去即可,递归都不用写。

(三)查找

int Query(tn p,string s)
{
	int len=s.length();
	for(int i=0;i<len;i++){
        int x;
		if('a'<=s[i]&&s[i]<='z')
            x=s[i]-'a';
        else
            x=s[i]-'A'+26;
		if(p->next[x]==NULL)
			return 0;
		p=p->next[x];
	}
	return p->exist&1;   //return p->cnt;
}

  从上往下找,如果没有直接返回,有相应字符则继续。直到找到字符串的末尾,返回字符串末尾的exist,判断这个单词是否出现过。

(四)删除树必写

void close(tn p)
{
	for(int i=0;i<54;i++)
		if(p->next[i]!=NULL&&p->next[i]->cnt)
			close(p->next[i]);
	delete(p);
}

三、例题

Hat’s Words

  A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
  You are to find all the hat’s words in a dictionary.

Input

  Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
  Only one case.

Output

  Your output should contain all the hat’s words, one per line, in alphabetical order.

Sample Input

a
ahat
hat
hatword
hziee
word

Sample Output

ahat
hatword

解析

  字符串数组开太小是罪
2
  比较典型的题,问你能不能找到两个词,组成一个词。字典树储存,然后暴力拆一遍所有字符串来判断。

#include<iostream>
#include<cstring>
#include<cstring>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

typedef long long ll;
typedef struct TreeNode* tn;
struct TreeNode{
	int cnt;
	tn next[54];
	bool exist;
};
tn root;
string s[50000];
tn New(tn p)
{
	p=new(TreeNode);
	memset(p->next,NULL,sizeof(p->next));
    return p;
}
void CreatTree(tn p,string s)
{
	int len=s.length();
	for(int i=0;i<len;i++){
        int x;
		if('a'<=s[i]&&s[i]<='z')
            x=s[i]-'a';
        else
            x=s[i]-'A'+26;
		if(p->next[x]==NULL)
			p->next[x]=New(p->next[x]);
		p=p->next[x];
		p->cnt++;
	}
	p->exist=1;
}
int Query(tn p,string s)
{
	int len=s.length();
	for(int i=0;i<len;i++){
        int x;
		if('a'<=s[i]&&s[i]<='z')
            x=s[i]-'a';
        else
            x=s[i]-'A'+26;
		if(p->next[x]==NULL)
			return 0;
		p=p->next[x];
	}
	return p->exist&1;   //return p->cnt;
}
void find(string s)
{
    int len=s.length();
    for(int i=1;i<len-1;i++)
        if(Query(root,s.substr(0,i))&&Query(root,s.substr(i,len-i))){
            cout<<s<<endl;
            break;
        }
}
void close(tn p)
{
	for(int i=0;i<54;i++)
		if(p->next[i]!=NULL&&p->next[i]->cnt)
			close(p->next[i]);
	delete(p);
}
int main()
{
    int tot=0;
	root=New(root);
    while(cin>>s[tot]){
        CreatTree(root,s[tot]);
        tot++;
    }
    for(int i=0;i<tot;i++)
        find(s[i]);
	close(root);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值