hdu 1075-What Are You Talking About(Map&&字典树)

来源:互联网 发布:油菜花粉 知乎 编辑:程序博客网 时间:2024/05/18 00:11

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075
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!

这道题 曾经 用 Map写过 还是很简单的 之前用到了 map迭代器,这次既然再写 就不用了 之间 find查找 关键字就好 。
这道题 这次 是练习 字典树的 所以 字典树代码 贴前面 Map 代码 贴后面 自己看喽

顺便 补充一个 知识点 :

ios_base::sync_with_stdio(0)
cin>>a;

另外发现ios_base::sync_with_stdio(0)这句话是关闭IO同步的 ,可以降低 cin所用的时间 只比scanf 快了 一点点 几乎可以省略

如果把cin和gets混用就不同步

字典树的过了 2天才来发代码 ,找了2天的错 才把程序改好,难受
主要就是 2个错误
1 访问下一指针域时候 代码写在 if 条件内部了 忘记如果不是空 也要访问下一指针域
2 在判断当前字符串是否 存在于字典树时, 判断条件写成了指向的 对应字符串是否存在 , 应该写成标记是否为1 ,否则runtime error 在这坑了1天
字典树代码:

#include <iostream>#include <bits/stdc++.h>using namespace std;struct node{    int t;  //标记    node *child[26];    char * trans;  //对应的翻译    node()    {        t=0;        for (int i=0; i<26; i++)            child[i] = NULL;    }};node *root;void build(char *b, char *a){    node *p;    node * newnode;    int i, m;    p = root;    for (i=0; i<strlen(b); i++)    {        m = b[i]-'a';        if (p->child[m]==NULL)        {            newnode = new node;            p->child[m] = newnode;        }        p = p->child[m];    }    p->t=1;    p->trans = new char[11];    strcpy (p->trans, a);   //将对应翻译存入}void finded(string b){    node *p, *newnode;    int i;    p = root;    for (i=0; i<b.length() ; i++)    {         int m = b[i]-'a';         if (p->child[m]==NULL)        {            cout<<b;            return;        }        p = p->child[m];    }    if (p->t)  //不能写成 p->trans 要不指针就是越界        cout<<p->trans;    else        cout<<b;}void ended(node *head)   //删除字典树{    for (int i=0; i<2; i++)    {        if (head->child[i]!=NULL)            ended(head->child[i]);    }    delete head;}int main(){    char a[3010],b[3010],s[3010];    scanf("%s",a);    //getchar();    memset(a,0,sizeof(a));    root = new node;    while (scanf("%s",a)!=EOF)    {        if (!strcmp(a,"END"))            break;        scanf("%s",b);        //getchar();        build(b,a);        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));    }    //getchar();    cin>>a;    getchar();    while (1)    {        memset(s,0,sizeof(s));        gets(s);        //getchar();        if (!strcmp(s, "END"))            break;        string gd = "";        for (int i=0; i<strlen(s); i++)        {            if (s[i]>='a'&&s[i]<='z')            {                //cout<<s[i]<<endl;                gd += s[i];            }            else            {                finded(gd);                cout<<s[i];                gd="";            }        }        cout<<endl;    }    ended(root);    return 0;}

Map代码:

#include <iostream>#include <bits/stdc++.h>using namespace std;map<string, string> st;int main(){    string a, b;    char s[3010];    cin >> a;    while (cin >> a)    {        if (a == "END")            break;        cin >> b;        st[b] = a;    }    cin >> a;    getchar();    while (1)    {        gets(s);        if (!strcmp(s, "END"))            break;        int len = strlen(s);        string t="";        for (int i=0; i<len; i++)        {            if (s[i]>='a'&&s[i]<='z')                t+=s[i];            else            {                if (st.find(t)!=st.end())   //如果能找到先用的key                    cout<<st[t];   //输出 对应的value                else                    cout<<t;     //如果找不到, 就输出原单词                t = "";                cout<<s[i];     //如果 不是单词形式  就输出标点或空格            }        }        cout<<endl;    }    return 0;}
原创粉丝点击