Peck Chen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用STL做A了...

用字典树RE了, 不知道为什么

后来把要翻译的行的单词读取方式改为第一种方法的读取方式就A了....

呼...比用STL做快...haha

 

2010-11-22 12:23:14    Accepted    1075    1687MS    37576K    799 B    C++    Y

 

代码
#include <iostream>
#include
<map>
#include
<string>
using namespace std;

map
<string, string> dict; /* 第一个string是火星文, 第二个是英文翻译 */
string mars, english;

int main()
{
int i, len;
string tmp;

cin
>> english;
while (cin >> english, english != "END")
{
cin
>> mars;

dict[mars]
= english;
}

cin
>> mars;
getchar();
/* 后面有'\n' */
while (getline(cin, mars) && mars != "END")
{
len
= mars.size();
tmp
= "";

for (i = 0; i < len; i++)
{
if (mars[i] >= 'a' && mars[i] <= 'z')
{
tmp
+= mars[i];
}
else
{
if (dict[tmp].size() == 0)
{
cout
<< tmp;
}
else
{
cout
<< dict[tmp];
}
tmp
= "";
cout
<< mars[i];
}
}
putchar(
'\n');
}

return 0;
}

 

 


2010-11-22 13:14:33    Accepted    1075    234MS    59812K    3538 B    C    Y

 

代码
#include <stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define ALPH_LEN 26 /* 26个字母 */
#define CHAR_LEN 12 /* 每个单词最多10个字符 */

const int ZERO = 0;
const char FIRST_CHAR = 'a';
const char EXIST = '1';
const char NON_EXIST = '0';
char english[CHAR_LEN]; /* 英文 */
char mars[CHAR_LEN]; /* 火星文 */

/* 字典树, str用于存储当前火星文的英文翻译 */
typedef
struct node {
struct node *child[ALPH_LEN];
char str[CHAR_LEN]; /* 用于保存翻译 */
char exist; /* 字典中是否存在些火星文 */
}node,
*Node;
Node root;
/* 字典树根结点 */
/* 插入单词 */
void insert(const char *mars, const char *english)
{
int i, index, len;
Node current
= NULL, newnode = NULL;

len
= strlen(mars);
if (ZERO == len) /* 此单词长度为零, 不需要插入 */
{
return;
}

current
= root; /* 每次插入都要从根结点开始 */
for (i = 0; i < len; i++)
{
index
= mars[i] - FIRST_CHAR; /* 获取当前字符要插入的下标 */
if (NULL != current->child[index]) /* 当前字符已存在字典树中 */
{
current
= current->child[index];
}
else
{
if ((newnode=(Node) calloc (1, sizeof(node))) == NULL) {
printf(
"空间分配失败!\n");
exit(
-1);
}

current
->child[index] = newnode; /* 插入新结点 */
current
= newnode; /* 修改当前结点的位置 */
}
}

current
->exist = EXIST;

len
= strlen( english );
strcpy(current
->str, english); /* 保存英文翻译 */
}
/* 查找单词 */
char find(const char *str)
{
int i, index, len;
Node current
= NULL;

len
= strlen(str);
if(ZERO == len) /* 此单词长度为零, 不存在此单词 */
{
return NON_EXIST;
}

current
= root;/* 每次查找都从要根结点开始 */
for (i = 0; i < len; i++)
{
index
= str[i] - FIRST_CHAR;
if (NULL != current->child[index]) /* 当前字符存在字典树中 */
{
current
= current->child[index]; /* 修改当前位置 */
}
else
{
return NON_EXIST;
}
}

if (current->exist == EXIST) /* 存在此单词 */
{
printf(
"%s", current->str); /* 翻译 */

return EXIST;
}

return NON_EXIST;
}
/* 释放内存 */
void release(Node root)
{
int i;

for(i = 0; i < ALPH_LEN; i++) /* 根结点下一共26个子树 */
{
if (NULL == root->child[i]) /* 此子树要释放 */
{
release(root
->child[i]); /* 释放此子树 */
}
}

free( root );
/* 清理资源 */
root
= NULL; /* 指针置空 */
}
/* 建立字典树 */
void build_trie(Node *root)
{
if (((*root)=(Node) calloc (1, sizeof(node))) == NULL) {
printf(
"空间分配失败!\n");
exit(
-1);
}

while (scanf("%s", english), strcmp(english, "END")) {
if (strcmp(english, "START") == ZERO) {
continue;
}

scanf(
"%s", mars);
insert(mars, english);
}
/* 输入 */
}
/* 翻译 */
void translate()
{
int i, len, index;
char c, word[30001], punc;
char tmp[CHAR_LEN];

scanf(
"%s", tmp);
getchar();
strcpy(tmp,
"");
index
= 0;
while (gets(word), strcmp(word, "END")) {
len
= strlen(word);
for (i = 0; i < len; i++)
{
if (word[i] >= 'a' && word[i] <= 'z')
{
tmp[index
++] = word[i];
}
else
{
tmp[index]
= '\0';
index
= 0;
if (find( tmp ) == EXIST) /* 存在字典树中 */
{
;
}
else
{
printf(
"%s", tmp);
}
putchar(word[i]);
strcpy(tmp,
"");
}
}

putchar(
'\n');
strcpy(word,
"");
}
/* 计算并输出 */
}

int main()
{
build_trie(
&root ); /* 建立火星文字典树 */
translate();
/* 翻译 */

return 0;
}

 

 

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 mot 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.

posted on 2010-11-22 12:30  PeckChen  阅读(600)  评论(0编辑  收藏  举报