程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU—What Are You Talking About(字典樹)

HDU—What Are You Talking About(字典樹)

編輯:關於C++

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 15100 Accepted Submission(s): 4850



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.
 

題意:作者遇到了美膩的Mars小姐,但是苦逼的是他聽不懂她說的話,於是Mars小姐給他一本Mars-English字典。現在給出好多對English-Mars單詞對,要你把Mars句子翻譯
成英文。Space(' '), tab('\t'), enter('\n')還有標點符號不要翻譯,還有不能翻譯的火星文也不要翻譯。
分析:直接利用字典樹求解,句子中處理出單詞花點心思即可。
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1075
代碼清單:
#include
#include
#include
#include
using namespace std;
const int MAX = 27 ;
const int maxn = 30005 ;
const int Max = 1000005;
struct trie{
    int point;
    trie *next[MAX];
};

trie *root=new trie;
char s[maxn],ss[MAX];
char anStr[Max][15];
int slen,k=1,ak,aj;

void createTrie(char *s,int pose){
    trie *p=root,*q;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL){
            q=new trie;
            q->point=0;
            for(int j=0;jnext[j]=NULL;
            p->next[pos]=q;
            p=p->next[pos];
        }
        else{
            p=p->next[pos];
        }
    }
    p->point=pose;   //字符串結尾標志,並且大小為單詞所在數組下標。
}

int findTrie(char *s){
    trie *p=root;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL)
            return 0;
        p=p->next[pos];
    }
    return p->point;
}

void delTrie(trie *Root){
    for(int i=0;inext[i]!=NULL)
            delTrie(Root->next[i]);
    }
    free(Root);
}

int main(){
    for(int i=0;inext[i]=NULL;
    while(gets(s)){
        if(strcmp(s,"END")==0) break;
        if(strcmp(s,"START")==0) continue;
        slen=strlen(s);
        for(int i=0;i='a'&&s[i]<='z')||s[i]=='\''){
                ss[ak++]=s[i];
                aj=0;
            }
            else{
                if(!aj){
                    int judge=findTrie(ss);
                    if(judge) printf("%s",anStr[judge]);
                    else printf("%s",ss);
                    memset(ss,'\0',sizeof(ss));
                    ak=0;
                }
                aj++;
                printf("%c",s[i]);
            }
        }printf("\n");
    }
    delTrie(root);
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved