Poj 2503 babelfish

Source: Internet
Author: User
Tags cmath

Dictionary tree problems (MAP water available)


I will give you a dictionary and ask you to translate a passage.


Find the corresponding link and then output it. The dictionary tree is used to return the first few unknown words that appear and then correspond to each other.

You can use map to water the past.


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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.