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;
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值