POJ 2503 Babelfish

字典树问题(map可水)


题意是:给你一本字典,叫你翻译一段话。


先把相应关系找到,然后输出就能够。字典树用于返回这是出现的第几个未知单词,然后相应。

能够用map水过去。


Trie:

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
struct Trie
{
    int word[100001][26];
    int sz,cot;
    int ex[1000001];
    Trie()
    {
        sz=1;
        cot=1;
        memset(word,0,sizeof(word));
        memset(ex,0,sizeof(ex));
    }
    int insert(char *s)
    {
        int u=0,c,len=strlen(s);
        for(int i=0; i<len; i++)
        {
            c=s[i]-'a';
            if(!word[u][c])
            word[u][c]=sz++;
            u=word[u][c];
        }
        if(ex[u]==0)ex[u]=cot++;
        return ex[u];
    }
    int search(char *s)
    {
        int u=0,c,len=strlen(s);
        for(int i=0;i<len;i++)
        {
            c=s[i]-'a';
            if(word[u][c])
                u=word[u][c];
            else
                return 0;
        }
        return ex[u];
    }
}wo;
char str[100001][11];
int main()
{
    char tmp[25];
    char a[11],b[11];
    while(gets(tmp),*tmp)
    {
        sscanf(tmp,"%s %s",a,b);
        int m=wo.insert(b);
        sscanf(a,"%s",str[m]);
    }
    while(scanf("%s",a)!=EOF)
    {
        int m=wo.search(a);
        if(m==0)puts("eh");
        else
            puts(str[m]);
    }
}

map:


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
map<string,string>word;

int main()
{
    string a;
    char str[51];
    char sa[11],sb[11];
    while(gets(str),*str)
    {
        sscanf(str,"%s %s",sa,sb);
        word[sb]=sa;
    }
    while(cin>>a)
    {
        if(word.find(a)==word.end())
            puts("eh");
        else
            cout<<word[a]<<endl;
    }
}




posted @ 2017-06-21 14:12  gccbuaa  阅读(125)  评论(0编辑  收藏  举报