字典树
2016-06-23 11:15
323 查看
字典树
/**** **** **** **** **** ****
* Function Name : 字典树(多路查找树)
* Description : HDOJ 1075 What Are You Talking About
* 易于字符保存,
插入和查找, 时间复杂度都是线性
**** **** **** **** **** ****/
#include <cstdio>
#include <string>
using namespace std;
struct trie
{
trie * next[26];
int index;
};
trie *thead;
char dic[1000000][20];
inline trie * newnode()
{
int i;
trie *t;
t=(trie*)malloc(sizeof(trie));
memset(t,0,sizeof(trie));
return t;
}
void insert(trie * s,char x[],int pos)
{
int i;
trie *t;
for(i=0; x[i] ; i++) {
if( s->next[ x[i]-'a' ] ) s=s->next[ x[i]-'a' ];
else {
t=newnode();
s->next[ x[i]-'a' ]=t;
s=t;
}
}//for
s->index=pos;
}
void deltrie(trie * s)
{
int i;
for(i=0; i < 26 ;i++) {
if( s->next[i] )
deltrie(s->next[i]);
}
free(s);
s=NULL;
}
int find(trie *s, char x[])
{
int i;
if(x[0] == 0) return -1;
for(i=0; x[i] ; i++) {
if( s->next[ x[i]-'a' ] ) s=s->next[ x[i]-'a' ];
else break;
}
if(x[i]==0) return s->index;
else return -1;
}
int main()
{
int t,n,i,j,all;
char word[20],mars[20],ch;
thead=newnode();
while(scanf("%s",word)==1)
if(word[0]=='S') break;
i=1;
while(scanf("%s",dic[i])==1 && dic[i][0]!='E') {
scanf("%s",mars);
insert(thead,mars,i);
i++;
}
all=i;
while(scanf("%s",word)==1)
if(word[0]=='S') break;
getchar(); j=0;
while(scanf("%c",&ch)==1 && ch!='E') {
if(ch>='a' && ch<='z') {
mars[j]=ch; j++;
}
else {
mars[j]=0;
t=find( thead , mars );
j=0;
if(t>0) printf("%s",dic[t]);
else if(mars[0]!=0) printf("%s",mars);
printf("%c",ch);
}
}//while
deltrie(thead);
}
/**** **** **** **** **** ****
* Function Name : 字典树(多路查找树)
* Description : HDOJ 1075 What Are You Talking About
* 易于字符保存,
插入和查找, 时间复杂度都是线性
**** **** **** **** **** ****/
#include <cstdio>
#include <string>
using namespace std;
struct trie
{
trie * next[26];
int index;
};
trie *thead;
char dic[1000000][20];
inline trie * newnode()
{
int i;
trie *t;
t=(trie*)malloc(sizeof(trie));
memset(t,0,sizeof(trie));
return t;
}
void insert(trie * s,char x[],int pos)
{
int i;
trie *t;
for(i=0; x[i] ; i++) {
if( s->next[ x[i]-'a' ] ) s=s->next[ x[i]-'a' ];
else {
t=newnode();
s->next[ x[i]-'a' ]=t;
s=t;
}
}//for
s->index=pos;
}
void deltrie(trie * s)
{
int i;
for(i=0; i < 26 ;i++) {
if( s->next[i] )
deltrie(s->next[i]);
}
free(s);
s=NULL;
}
int find(trie *s, char x[])
{
int i;
if(x[0] == 0) return -1;
for(i=0; x[i] ; i++) {
if( s->next[ x[i]-'a' ] ) s=s->next[ x[i]-'a' ];
else break;
}
if(x[i]==0) return s->index;
else return -1;
}
int main()
{
int t,n,i,j,all;
char word[20],mars[20],ch;
thead=newnode();
while(scanf("%s",word)==1)
if(word[0]=='S') break;
i=1;
while(scanf("%s",dic[i])==1 && dic[i][0]!='E') {
scanf("%s",mars);
insert(thead,mars,i);
i++;
}
all=i;
while(scanf("%s",word)==1)
if(word[0]=='S') break;
getchar(); j=0;
while(scanf("%c",&ch)==1 && ch!='E') {
if(ch>='a' && ch<='z') {
mars[j]=ch; j++;
}
else {
mars[j]=0;
t=find( thead , mars );
j=0;
if(t>0) printf("%s",dic[t]);
else if(mars[0]!=0) printf("%s",mars);
printf("%c",ch);
}
}//while
deltrie(thead);
}
相关文章推荐
- 字典树的基本知识及使用C语言的相关实现
- Trie树_字典树(字符串排序)简介及实现
- Python实现简单字典树的方法
- Python实现简单字典树的方法
- POJ 1056 IMMEDIATE DECODABILITY
- CF 665E 限制异或和的区间计数
- Trie
- Problem A: ZZ买衣服
- codeforces content#260 D A Lot of Games
- sdutoj1500 Message Flood
- 字典树
- Hardwood Species——物种所占比例
- Message Flood——发短信
- HDU 1671 字典树
- hdoj 4287 Intelligent IME 字典树
- 字典树(Trie树、单词查找树、前缀树)
- Immediate Decodability
- 统计难题
- 单词数
- Hat's words