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(' '), enter(' ') 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;
}
原文地址:https://www.cnblogs.com/hhlya/p/13304775.html