Fork me on GitHub

HDU 1075 还是Trie树

http://acm.hdu.edu.cn/showproblem.php?pid=1075

What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 5996 Accepted Submission(s): 1830


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!

Hint

Huge input, scanf is recommended.

 说实话,字符串的处理还真是让人有点纠结。。。。

 AC Code

#include <stdio.h>
#include <memory.h>
#include <string.h>

const int BranchNumber=26;

class TrieNode
{
public:
bool isStr;
char* value;
TrieNode* next[BranchNumber];

TrieNode():isStr(false),value(NULL)
{
memset(next,NULL,sizeof(next));
}
};

class TrieTree
{
public :
TrieTree()
{
root=new TrieNode;
}
~TrieTree();

void Insert(const char* word,const char* key);
char* Search(const char* key);
void DeleteTrieTree(TrieNode* t);

private:
TrieNode* root;
};

TrieTree::~TrieTree()
{
DeleteTrieTree(root);
}

void TrieTree::Insert(const char* word,const char* key)
{
if(root==NULL)
root=new TrieNode;

TrieNode* location=root;
while(*key)
{
if(location->next[*key-'a']==NULL)
location->next[*key-'a']=new TrieNode;
location=location->next[*key-'a'];
key++;
}
location->isStr=true;
location->value=new char[strlen(word)+1];
strcpy(location->value,word);
}

char* TrieTree::Search(const char* key)
{
TrieNode* location=root;
while(*key && location)
{
location=location->next[*key-'a'];
key++;
}

if(location!=NULL&& location->isStr)
return location->value;
else
return NULL;
}

void TrieTree::DeleteTrieTree(TrieNode* t)
{
if(t!=NULL)
{
for(int i=0;i<BranchNumber;i++)
DeleteTrieTree(t->next[i]);
delete t->value;
delete t;
t=NULL;
}
}


void Solution()
{
TrieTree trieTree;
char english[12], martian[12], word[12];
int cnt;
char ch;
char *p;
scanf("%s", english); //接受START
while(scanf("%s", english) && english[0] != 'E')
{
scanf("%s", martian);
trieTree.Insert(english,martian);
}
scanf("%s", english); //接收START
getchar(); //接收回车符
cnt = 0;
while( (ch = getchar()) != 'E' )
{
if(ch >= 'a' && ch <= 'z')
word[cnt++] = ch;
else
{
if(cnt)//word[]含有字符
{
word[cnt] = '\0';
p = trieTree.Search(word);
if(p)
printf("%s", p);
else
printf("%s",word);
}
cnt = 0;
putchar(ch);
}
}
getchar();
getchar();
}

int main()
{
Solution();
return 0;
}


 

posted @ 2012-03-21 10:42  _Lei  阅读(378)  评论(0编辑  收藏  举报