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

POJ_2503_Babelfish(map or 字典樹)

編輯:關於C++
Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 34816   Accepted: 14908

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.

Source

Waterloo local 2001.09.22

題意:就是對應翻譯,如果沒有找到翻譯就輸出“eh”。
分析:首選就想到了map去做,然後AC了。最近在做字典樹的題目,於是用字典樹也寫了一遍。
題目鏈接:http://poj.org/problem?id=2503
map代碼:
#include
#include #include #include #include using namespace std; const int MAX = 27 ; const int MAXN = 100000 + 5 ; mapm; int num = 0 , len; char s[MAXN][MAX],str[MAXN]; int main(){ //freopen("liuchu.txt","r",stdin); while(gets(s[num])&&s[num][0]!='\0'){ len = strlen(s[num]); int first = 0, k = 0; for(int i=0;i
trie代碼:
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAX = 27 ;
const int MAXN = 100000 + 5 ;
struct trie{
    int point;
    trie *next[MAX];
};

int number = 0 ;
char s[MAXN][MAX],str[MAX];
trie *root=new trie;

void createTrie(char *s,int n){
    trie *p=root,*q;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL){
            q=new trie;
            for(int j=0;jnext[j]=NULL;
            p->next[pos]=q;
            p=p->next[pos];
        }
        else{
            p=p->next[pos];
        }
    }
    p->point=n;
}

int findTrie(char *s){
    trie *p=root;
    int len=strlen(s),pos;
    for(int i=0;inext[pos]==NULL)
            return -1;
        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(){
    //freopen("liuchu.txt","r",stdin);
    for(int i=0;inext[i]=NULL;
    while(gets(s[number])&&s[number][0]!='\0'){
        int len=strlen(s[number]),k=0 ;
        bool judge=false;
        for(int i=0;i


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