您的位置:首页 > 其它

hdu 1075 What Are You Talking About ( trie + 模拟 )

2015-03-14 21:37 429 查看

What Are You Talking About

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

Total Submission(s): 14973 Accepted Submission(s): 4811



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.




Author
Ignatius.L

题目大意:先读入字典,然后做翻译

题目分析:搞吧,小模拟,注意前缀的处理......容易wrong,必须完整匹配才能做翻译

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MAX 100007

using namespace std;

char word[MAX*10][12];
char s[15];
char sentence[4007];

struct Node
{
    Node* b[27];
    int id;
    Node ( )
        : id(-1)
    {
        memset ( b , 0 ,sizeof (b));
    }
};

int cc;

int main ( )
{
    int cnt = 0;
    scanf ( "%s" , s );
    cc = 0;
    Node* Root = new Node ( );
    while ( scanf ( "%s" , s ) )
    {
        if ( s[0] == 'E' ) break;
        //scanf ( "%s" , word[cnt] );
        int len = strlen(s);
        for ( int i = 0 ; i < len ; i++ )
            word[cnt][i] = s[i];
        word[cnt][len] = 0;
        scanf ( "%s" , s );
       // cout << word[cnt] << " " << s << endl;
        len = strlen(s);
        Node * root = Root;
        for ( int i = 0; i < len; i++ )
        {
            if ( !(root->b[s[i]-'a']) )
                root->b[s[i]-'a'] = new Node();
            root = root->b[s[i]-'a'];
            //cout << i << " ";
        }
       // cout << endl;
        root->id = cnt++; 
       // cout << root->id << endl;
       // cout << " cc : " << cc << endl;
       // cout << "----------------------------" << endl;
    }
   // cout << "YES : " << cnt << endl;
    scanf ( "%s" , s );
    getchar ( );
    while ( gets(sentence) )
    {
       // cout << "YES : " << sentence << endl;
        if ( sentence[0] == 'E' ) break;
        int len = strlen (sentence);
        int i = 0;
        while ( i < len )
        {
            if ( !islower(sentence[i]) ) 
            {
                printf ( "%c" , sentence[i] );
                i++;
                continue;
            }
            Node *root = Root;
            int st = i , ed = i;
            while ( islower(sentence[ed] ) ) ed++;
           /* cout << endl;
            cout << "st : " << st << " " << ed << endl;
            cout << "-----------sentence -------------" << endl;
            for ( int j = st ; j < ed ; j++ )
                printf ( "%c" , sentence[j] );
            puts ("");
            cout << "---------------------------------" << endl;
            cout << endl;
           */
            int j;
            for ( j = st ; j < ed ; j++ )
            {
                if ( !(root->b[sentence[j]-'a']) ) break;
                else  
                    root = root->b[sentence[j]-'a'];
            }
           // cout << "-----id----- : " << root->id << endl;
            if ( j == ed && root->id >= 0 ) printf ( "%s" , word[root->id] );
            else
                for ( int j = st ; j < ed ; j++ )
                    printf ( "%c" , sentence[j] );
            i = ed;   
        }
        puts ("");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: