字典树详解(附题:HDU 1075 What are you talking about)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Apple_hzc/article/details/50587051

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 18313    Accepted Submission(s): 5972



Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 

Output
In this problem, you have to output the translation of the history book.
 

Sample Input
 
  
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 

Sample Output
hello,i'm from mars.
i like earth!
       字典树是一种比较重要的储存方式,又称单词查找树,Trie。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来节约存储空间,最大限度地减少无谓的字符串比较,查询效率比哈希表高。
       字典树与字典很相似,当你要查一个单词是不是在字典树中,首先看单词的第一个字母是不是在字典的第一层,如果不在,说明字典树里没有该单词,如果在就在该字母的孩子节点里找是不是有单词的第二个字母,没有说明没有该单词,有的话用同样的方法继续查找.字典树不仅可以用来储存字母,也可以储存数字等其它数据。
       首先,我们来看看字典树是如何定义的。
typedef struct TrieNode
{
    int count;
    struct TrieNode *next[MAX];
}TrieNode; 
       其中,count一般表示字典树在此有多少相同前缀数目的单词,当然根据题目需要进行适当改变。而TrieNode *next数组则表示下面节点的地址。
        Trie的查找(最主要的操作):
(1) 每次从根结点开始一次搜索;
(2) 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续进行检索;   
(3) 在相应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树进行检索。   
(4) 迭代过程……   
(5) 在某个结点处,关键词的所有字母已被取出,则读取附在该结点上的信息,即完成查找。
       下面给出字符串插入字典树的代码:
void Insert(char *word,TrieNode *root)
{
    int i;
    TrieNode *cur=root;
    if(word[0]=='\0')
        return;
    for(i=0;word[i]!='\0';i++)
    {
        if(cur->next[word[i]]==NULL)
        {
            TrieNode *newNode = (TrieNode *)malloc(sizeof(TrieNode));
            memset(newNode,0,sizeof(TrieNode));
            cur->next[word[i]]=newNode;
        }
        cur=cur->next[word[i]];
    }

    cur->count++;
    return;
}
       当然在给新的节点赋予空间时,既可以用malloc函数动态分配内存,也可以利用数组静态分配内存,相对来说前者内存可以随时释放,但时间长,后者时间短,但内存过大可能会造成Memory Limit,所以根据个人习惯运用。
       查找单词的代码:
bool Find(TrieNode *root,char *word)
{
    int i;
    TrieNode *cur;
    cur=root;
    for(i=0;word[i]!='\0';i++)
    {
        if(cur->next[word[i]]==NULL)
        {
            return false;
        }
        cur=cur->next[word[i]];
    }

    if(cur->count)
        return true;
    else
        return false;
}
       这个题题意:给你一个字典,里面是一些相互对应的字符串,再给你几个长字符串,求翻译后的字符串。

    题解:字典树,先把字典中的每对字符串编号,并把字符串对的前者和对应的字符串对编号放入map中方便最后输出的查找,把字符串对的后者和对应的字符串对编号放入字典树中。之后进行基本的查找即可。

    AC代码:


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
 struct node *child[26];
 char *str;
}*root;
void insert(char *c1,char *c2)
{
 int len=strlen(c2);
 struct node *cur,*newnode;
 cur=root;
 for(int i=0;i<len;i++)
 {
  if(cur->child[c2[i]-'a']!=0)
        cur=cur->child[c2[i]-'a'];
  else
  {
   newnode=new struct node;
   cur->child[c2[i]-'a']=newnode;
   for(int j=0;j<26;j++)
          newnode->child[j]=0;
   newnode->str=NULL;
   cur=newnode;      
  }     
 }
 cur->str=new char[15];
 strcpy(cur->str,c1);
}
void find_print(char *c2)
{
 int len=strlen(c2);
 struct node *cur;
 cur=root;
 if(!len) return ;
 for(int i=0;i<len;i++)
 {
  if(c2[i]<'a'||c2[i]>'z'||cur->child[c2[i]-'a']==0)
        {
          printf("%s",c2);
          return ;
        }
        else cur=cur->child[c2[i]-'a'];             
 }
 if(cur->str!=NULL)//如果找到了,且相应节点上存放着字符串,则输出,即所查找的单词不是前缀
       printf("%s",cur->str);
 else printf("%s",c2);    
}
int main()
{
 char s[15],temp[3005],c1[15],c2[15];
 root=new struct node;
 for(int i=0;i<26;i++)
         root->child[i]=0;
     scanf("%s",temp);
  while(scanf("%s",c1)&&strcmp(c1,"END")!=0)
  {
   scanf("%s",c2);
   insert(c1,c2);
  }   
  scanf("%s",temp);
  getchar();
  while(gets(temp)&&strcmp(temp,"END")!=0)
  {
   int len=strlen(temp);
   int num=0;
   bool flag=false;
   for(int i=0;i<len;i++)
   {
    if(temp[i]>='a'&&temp[i]<='z')
    {
     if(flag==false)
                     flag=true;
               s[num++]=temp[i];     
      }
      else
      {
       if(flag)
                {
                 flag=false;
                 s[num]='\0';
                 num=0;
                    find_print(s);
    }
    printf("%c",temp[i]);
   }
        }
        if(flag)
        {
         s[num]='\0';
         find_print(s);
  }
  printf("\n"); 
  }
  return 0;
}





猜你喜欢

转载自blog.csdn.net/Apple_hzc/article/details/50587051