zoukankan      html  css  js  c++  java
  • HDU1075

    题目大意

    给定一些火星文单词以及对应的英语单词,然后给你一些火星文,要求你翻译成对应的英文

    题解

    第一次写Trie树!

    把所有火星文单词插入到Trie树中,并且每个火星文单词结尾的节点记录相应英文单词的位置,然后进行查询即可~~~~

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <vector>
    using namespace std;
    const int maxnode = 3000005;
    const int sigma_size = 26;
    char dic[maxnode][15];
    struct Trie
    {
        int ch[maxnode][sigma_size];
        int val[maxnode];
        int sz;
        Trie(){sz=1;memset(ch[0],0,sizeof(ch[0]));val[0]=-1;}
        int idx(char c){return c-'a';}
        void insert(char *s,int v)
        {
            int u=0,n=strlen(s);
            for(int i=0;i<n;i++)
            {
                int c=idx(s[i]);
                if(!ch[u][c])
                {
                    memset(ch[sz],0,sizeof(ch[sz]));
                    val[sz]=-1;
                    ch[u][c]=sz++;
                }
                u=ch[u][c];
            }
            val[u]=v;
        }
        int find(const char *s)
        {
            int u=0;
            for(int i=0;s[i]!='';i++)
            {
                int c=idx(s[i]);
                if(!ch[u][c])
                    return -1;
                u=ch[u][c];
            }
            return val[u];
        }
    
    };
    Trie trie;
    int main()
    {
        char word[15];
        int cnt=0;
        scanf("%s",word);
        while(scanf("%s",word)!=EOF)
        {
            if(word[0]=='E') break;
            strcpy(dic[cnt],word);
            scanf("%s",word);
            trie.insert(word,cnt);
            cnt++;
        }
        scanf("%s",word);
        getchar();
        char ch;
        int len=0;
        while(scanf("%c",&ch)!=EOF&&ch!='E')
        {
            if(islower(ch))
                word[len++]=ch;
            else
            {
                word[len]='';
                int pos=trie.find(word);
                if(pos>=0)
                    printf("%s",dic[pos]);
                else
                    if(word[0]!='')
                        printf("%s",word);
                 printf("%c",ch);
                 len=0;
            }
        }
        return 0;
    }
  • 相关阅读:
    安装k8s和NVIDIA环境
    docker使用阿里云加速器
    Ubuntu16安装NVIDIA驱动后重复登录 简单粗暴
    Ubuntu TTY 字体大小 目录颜色 中文乱码 设置
    vim使用记录
    k8s内运行ubuntu容器
    IRelationalOperator接口
    IArea接口(计算多边形面积)
    ITopologicalOperator接口
    通过ArcEngine接口建立凸包(一)
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/3317709.html
Copyright © 2011-2022 走看看