您的位置:首页 > 其它

hdu 1075(字典树)

2017-09-30 13:25 295 查看

What Are You Talking About

[b]Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)

Total Submission(s): 24812    Accepted Submission(s): 8357
[/b]

[align=left]Problem Description[/align]
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?

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
In this problem, you have to output the translation of the history book.

 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

hello, i'm from mars.
i like earth!

Hint

Huge input, scanf is recommended.


题意:
给你一些key_value对  然后在给你一些乱码  让你翻译   如果乱码中有对应的单词 就翻译过来  没有就按原样输出~~
翻译的问题可以用字典树解决  但是这道题恶心人的地方在读入
我用了两种方法读入 第一种是gets  然后拆单词  第二种是 迭代getchar() 来拼单词 两种都能过
但是因为我没有memset  tree数组 导致我浪费了近半天的时间  我还以为是什么特殊情况  疯狂想各种奇奇怪怪的测试样例  都是泪~~~

先贴一个gets过的吧
这份代码刚开始写的比较简洁  但是因为各种bug  删掉了很多东西 写的很繁琐

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdio>
#include <queue>
#define bug printf("What the fuck~~~~\n");
using namespace std;
const int N = 1000005;
int tree
[26];
bool nums
;
int cnt;
char key_value
[11];
char Input_str[10000][3005];
void Insert(char *str)
{
int i = 0, k = 0;
while (str[i]) {
//	printf(".");
int pos = str[i++] - 'a';
if (tree[k][pos] == 0)
tree[k][pos] = cnt ++;
k = tree[k][pos];
}
nums[k] = true;
}

int Find(string str)
{
int i = 0, k = 0;
int length_str = str.length();
while (i < length_str) {
int pos = str[i++] - 'a';
if (tree[k][pos] == 0)
return 0;
k = tree[k][pos];
}
if (nums[k])
return k;
else return 0;
}

int main()
{
char str[3005];
scanf("%s",str);
cnt = 1;
getchar();
memset(tree , 0, sizeof(tree));
memset(nums , false , sizeof(nums));//初始化非常重要!!!!
while (true) {
char str_first[11], str_second[11];
//	gets(str);
scanf("%s",str_first);
if (str_first[0] == 'E' && str_first[1] == 'N' && str_first[2] == 'D') {
break;
}
//	printf("----%s-----\n",str);

//sscanf(str, "%s %s", str_first, str_second);
//	printf("%s %s\n",str_first, str_second);
scanf("%s",str_second);
Insert(str_second);
strcpy(key_value[cnt - 1] , str_first);
}
//
scanf("%s",str);
getchar();
int cnt_input = 0;
while (true) {
gets(Input_str[cnt_input]);
if (!strcmp(Input_str[cnt_input] , "END"))//把读到的字符串都存起来 不然没法拆单词  这一步很蠢  我也知道~~
break;
cnt_input++;
}//printf("GG");
//	printf("cntt == %d\n",cnt_input);
for (int i = 0; i < cnt_input; i ++) {//bug;;
int pos = 0;
//	printf("The string is %s\n",Input_str[i]);
int length_input = strlen(Input_str[i]);
while (pos < length_input) {
string str_input;
while ((Input_str[i][pos] <= 'z' && Input_str[i][pos] >= 'a') && pos < length_input) {//用isalpha更简洁
str_input += Input_str[i][pos++];
}
int key = Find(str_input);
if (!key && str_input.length()) {
//	pos++;
cout << str_input;
}else {
printf("%s",key_value[key]);
}
while (true) {
if ((Input_str[i][pos] > 'z' || Input_str[i][pos] < 'a') && pos < length_input) {
printf("%c",Input_str[i][pos++]);
}
else
break;
}
//	printf("---pos == %d---\n",length_input);
}puts("");
}

}


再贴一个因为各种情况gets过不了 然后 用了getchar写的代码  就输入有点变化 其他都还好  getchar写的更快

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdio>
#include <queue>
#define bug printf("What the fuck~~~~\n");
using namespace std;
const int N = 1000005;
int tree
[26];
bool nums
;
int cnt;
char key_value
[11];
char Input_str[9000][3005];
void Insert(char *str)
{
int i = 0, k = 0;
while (str[i]) {
//	printf(".");
int pos = str[i++] - 'a';
if (tree[k][pos] == 0)
tree[k][pos] = cnt ++;
k = tree[k][pos];
}
nums[k] = true;
}

int Find(string str)
{
int i = 0, k = 0;
int length_str = str.length();
while (i < length_str) {
int pos = str[i++] - 'a';
if (tree[k][pos] == 0)
return 0;
k = tree[k][pos];
}
if (nums[k])
return k;
else return 0;
}

bool judge(char str)
{
if (str >= 'a' && str <= 'z')
return true;
return false;
}
int main()
{
char str[3005];
scanf("%s",str);
cnt = 1;
getchar();
memset(tree , 0, sizeof(tree));
memset(nums , false , sizeof(nums));
while (true) {
gets(str);
if (str[0] == 'E' && str[1] == 'N' && str[2] == 'D') {
break;
}
//	printf("----%s-----\n",str);
char str_first[11], str_second[11];
sscanf(str, "%s %s", str_first, str_second);
//	printf("%s %s\n",str_first, str_second);
Insert(str_second);
strcpy(key_value[cnt - 1] , str_first);
}
//
scanf("%s",str);
getchar();
char temp_str;
while (true) {
temp_str = getchar();
if (temp_str == 'E')
break;
string S;
S += temp_str;
if (judge(temp_str)) {
while (true) {
temp_str = getchar();
if (judge(temp_str))
S += temp_str;
else
break;
}
int where = Find(S);
if (!where) {
cout << S ;
}
else {
printf("%s",key_value[where]);
}
printf("%c",temp_str);
}
else {
printf("%c",temp_str);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: