HDU 1075 아니면 트 리?

10013 단어 자바php
http://acm.hdu.edu.cn/showproblem.php?pid=1075
What Are You Talking AboutTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 5996 Accepted Submission(s): 1830Problem DescriptionIgnatius 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?InputThe 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('') 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.OutputIn this problem, you have to output the translation of the history book.Sample InputSTARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i fiiwj fnnvk!ENDSample Outputhello, i'm from mars.i like earth!HintHuge input, scanf is recommended.
 솔직히 문자열 처리 가 좀 꼬 이 네요...
 AC Code
#include 
#include
#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 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;
}

 
다음으로 전송:https://www.cnblogs.com/-Lei/archive/2012/03/21/2409408.html

좋은 웹페이지 즐겨찾기