From f6af6a77a4bf43098c2d7f05992b6c7a416dc40f Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Sat, 7 May 2016 20:06:34 +0800 Subject: [PATCH] Create 1075_cnblogs.cpp From http://www.cnblogs.com/qiufeihai/archive/2012/10/20/2732449.html --- HDOJ/1075_cnblogs.cpp | 142 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 HDOJ/1075_cnblogs.cpp diff --git a/HDOJ/1075_cnblogs.cpp b/HDOJ/1075_cnblogs.cpp new file mode 100644 index 0000000..be9fbcf --- /dev/null +++ b/HDOJ/1075_cnblogs.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +using namespace std; + + + +struct Node +{ + struct Node *child[26]; + int perfixNum; + bool isWord; + char ansChar[111]; +}; + +Node *root, *pNode; + + +void Init() +{ + root = new Node; + for (int i = 0; i < 26; i++) + { + root->child[i] = NULL; + } +} + +void Insert(char word[], char ansChar[]) +{ + int len = strlen(word); + Node *pNode = root; + for (int i = 0; i < len; i++) + { + if (pNode->child[word[i] - 'a'] == NULL) + { + Node *newNode = new Node; + newNode->perfixNum = 1; + newNode->isWord = 0; + for (int j = 0; j < 26; j++) + { + newNode->child[j] = NULL; + } + + pNode->child[word[i] - 'a'] = newNode; + } + else + { + pNode->child[word[i] - 'a']->perfixNum++; + } + + pNode = pNode->child[word[i] - 'a']; + if(i == len - 1) + { + pNode->isWord = 1; + strcpy(pNode->ansChar, ansChar); + } + } +} + +bool Find(char word[]) +{ + int len = strlen(word); + pNode = root; + int i; + for (i = 0; i < len; i++) + { + if (pNode->child[word[i] - 'a'] != NULL) + { + pNode = pNode->child[word[i] - 'a']; + } + else + { + break; + } + } + + //字典中存在该单词 + if (i == len && pNode->isWord == 1) + { + return 1; + } + else + { + return 0; + } +} + + + + +int main() +{ + char getChar[11]; + gets(getChar); + char str1[33], str2[33]; + Init(); + while (scanf("%s%s", str1, str2), str1[0] != 'E') + { + + Insert(str2, str1);//插入,单词,单词的翻译 + } + + getchar(); + char ansStr[3333]; + char saveStr[33]; + while (gets(ansStr), ansStr[0] != 'E') + { + int i; + int k = 0; + for (i = 0; i < strlen(ansStr); i++) + { + if (ansStr[i] < 'a' || ansStr[i] > 'z') + { + if (ansStr[i - 1] >= 'a' && ansStr[i - 1] <= 'z') + { + saveStr[k] = '\0'; + k = 0; + if (Find(saveStr)) + { + printf("%s", pNode->ansChar);//如果字典中存在该单词,则输出它的翻译 + } + else + { + + printf("%s", saveStr);//字典中不存在该单词,输出原单词 + } + } + + printf("%c", ansStr[i]); + + } + else + { + saveStr[k++] = ansStr[i];//保存单词 + } + } + puts(""); + } + + + return 0; +}