HDU 1075 What Are You Talking About(字典树)

题意:给出每个英语单词对应的火星单词,然后给你一串火星句子,让你翻译成英文句子。

思路:用火星文构造一颗字典树,节点位置存对应的英文单词。然后从句子里找单词再从字典树find即可。标点直接输出就行。

AC代码:

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
using namespace std;
const int mx=1000005;
const int inf=0x3f3f3f3f;
int num,nm,cas,n,m,T;
char w1[500005][25],w2[500005][25],w3[500005][15];//要开足够大,给的内存足够大,不用怕MLE的问题
struct node{
    int ch[mx][26];
    int v[mx];
    char *str[mx];
    int sz;
    void add(char *s)
    {
     int u=0,n=strlen(s);
     for(int i=0;i<n;i++)
     {
      int id=s[i]-'a';
      if(ch[u][id]==0)
      {
       ch[u][id]=sz;
       v[sz]=0;
       memset(ch[sz],0,sizeof(ch[sz]));
       sz++;
      }
      u=ch[u][id];
     }
     v[u]=1;
     str[u]=w2[num];
     //printf("**%s**",str[u]);
    }
    bool find(char *s)
    {
     int u=0,n=strlen(s);
     for(int i=0;i<n;i++)
     {
      int id=s[i]-'a';
      if(ch[u][id]==0) {return 0;}
      u=ch[u][id];
     }
     if(v[u]) {printf("%s",str[u]);return 1;}
     else return 0;
     }
     void init(){
     memset(ch[0],0,sizeof(ch[0]));
     v[0]=0;
     sz=1;
     }
}a;
int main()
{
    //ios::sync_with_stdio(false);
    cas=0;
    char s[3005];
    num=1;
    nm=1;
    while(gets(s))
    {
        if(strcmp(s,"START")==0)//判断与给定字符串是否相同
        {
            a.init();//注意初始化的位置
            while(gets(s))
            {
                if(strcmp(s,"END")==0) {break;}
                sscanf(s,"%s%s",w2[num],w1[num]);//sscanf直接从s中取出两个字符串,非常好用,这是第二次用了
                a.add(w1[num]);
                num++;
            }
            while(gets(s))
            {
                if(strcmp(s,"START")==0) {continue;}
                if(strcmp(s,"END")==0) {break;}
                int i=0;
                int l=strlen(s);
                int pre=0;
                while(i<l){
                for(;i<l;i++)
                {
                    if(s[i]<'a'||s[i]>'z') break;
                    w3[nm][i-pre]=s[i];
                }
                if(a.find(w3[nm])==0)
                {
                 printf("%s",w3[nm]);//找不到就直接输出
                }
                nm++;
                while((i<l)&&(s[i]<'a'||s[i]>'z')) {printf("%c",s[i]);i++;}
                pre=i;
                }
                puts("");
            }
        }
    }
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值