1. 程式人生 > 實用技巧 >HDU What Are You Talking About (字典樹 or map)

HDU What Are You Talking About (字典樹 or map)

題面

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.
[/hint]

思路

字典樹板子題,不過發現好多字典樹的題目都可以用map或者set來做,其原因在於字典樹的很多題目在於字串匹配,而map等stl也具有匹配和查重功能,但是字典樹的效率顯然是要比stl要快的。emmmm,怎麼說呢,我其實不是很會字串的題目,因為這個輸入輸出和處理實在有點搞。補一下基礎吧,如果我們開的是string的話,那麼儘量用cin輸入,因為scanf雖然快,但是它並不熟悉string型別,字元陣列的話就用scanf會快一點,相對應的讀一整行的話,getline支援cin,而gets對應字元陣列。我們注意,在我們讀入的時候,cin是不會讀入最後一個回車的,所以通常我們可以看到,在cin之後我們會新增一個getchar去把回車符吸收掉。另外getline和gets是不會讀入回車的。思路,我們可以用map存下字典,然後在逐個字元去查詢,注意細節。字典樹也是一樣,如果這個火星字串曾經被儲存在樹裡面,那麼我們可以直接用英文去替換,思路都差不多。

程式碼實現

#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=5000;
map <string ,string> m;
int main () {
    string a,b;
    cin>>a;
    while (cin>>a&&a!="END") {
        cin>>b;
        m[b]=a;
    }
    cin>>a;
    getchar ();
    string ans;
    ans.clear ();
    while (getline (cin,a)) {
        if (a=="END") break;
        int len=a.length ();
        for (int i=0;i<len;i++) {
            if (isalpha(a[i])) {
                ans+=a[i];
                if (!isalpha(a[i+1])) {
                   if (m[ans]!="") cout<<m[ans];
                   else cout<<ans;
                   ans.clear ();
                }
            }
            else {
                cout<<a[i];
            }
        }
        cout<<endl;
    }
    return 0;
}
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
const int maxn=50010;
int ch[maxn][26];
int cnt=1,vis[maxn];
string a[maxn],b;
int num;
inline void insert (string s) {
    int len=s.length ();
    int u=1;
    for (int  i=0;i<len;i++) {
        int c=s[i]-'a';
        if (!ch[u][c]) {
           ch[u][c]=++cnt;
           u=ch[u][c];
        }
    }
    vis[u]=num;
}
inline bool search (string s) {
    int cnt=1;
    int len=s.length();
    int u=1;
    for (int i=0;i<len;i++) {
        int c=s[i]-'a';
        if (!ch[u][c]) return false;
        u=ch[u][c];
    }
    return vis[u];
}
int main () { 
    string s;
    cin>>s;
    while (cin>>a[++num]) {
        if (a[num]=="END") break;
        cin>>b;
        insert (b);
    }
    cin>>s;
    getchar();
    while (getline(cin,b)) {
        if (b=="END") break;
        string ans;
        ans.clear();
        for (int i=0;i<b.length();i++) {
            if (isalpha(b[i])) {
               ans+=b[i];
               if (!isalpha(b[i+1])) {
                   int t=search (ans);
                   if (t==0) {
                        cout<<ans;
                   }
                   else {
                     cout<<a[t];
                   }
                   ans.clear ();
               }
        }
        else cout<<b[i];
    }
    cout<<endl;
}
    return 0;
}