zoukankan      html  css  js  c++  java
  • HDU 1075 What Are You Talking About (字典树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1075

    题意:先给出一些单词的翻译,然后给出文章,要求把能够翻译单词的全都翻译

    分析:两种方法,都是扫文章,然后如果是字母则为保存一个单词,然后判断,是否能翻译,能就翻译,否则输出原单词,

    符号直接输出。

    法一,用map   法二,用字典树

    先说字典树,主要用字典树来判断该单词是否在字典中,并保存它的翻译。。。

    结点的数据域用了isWord(表示是否存在以该结点为结尾的单词),和ansChar(保存该单词的翻译)

    代码有点乱,主要是思想

    代码:

    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    
    
    struct Node
    {
        struct Node *child[26];
        int perfixNum;
        bool isWord;
        char ansChar[111];
    };
    
    Node *root, *pNode;
    
    
    void Init()
    {
        root = new Node;
        for (int i = 0; i < 26; i++)
        {
            root->child[i] = NULL;
        }
    }
    
    void Insert(char word[], char ansChar[])
    {
        int len = strlen(word);
        Node *pNode = root;
        for (int i = 0; i < len; i++)
        {
            if (pNode->child[word[i] - 'a'] == NULL)
            {
                Node *newNode = new Node;
                newNode->perfixNum = 1;
                newNode->isWord = 0;
                for (int j = 0; j < 26; j++)
                {
                    newNode->child[j] = NULL;
                }
    
                pNode->child[word[i] - 'a'] = newNode;
            }
            else
            {
                pNode->child[word[i] - 'a']->perfixNum++;
            }
            
            pNode = pNode->child[word[i] - 'a'];
            if(i == len - 1)
            {
                pNode->isWord = 1;
                strcpy(pNode->ansChar, ansChar);
            }
        }
    }
    
    bool Find(char word[])
    {
        int len = strlen(word);
        pNode = root;
        int i;
        for (i = 0; i < len; i++)
        {
            if (pNode->child[word[i] - 'a'] != NULL)
            {
                pNode = pNode->child[word[i] - 'a'];
            }
            else
            {
                break;
            }
        }
        
        //字典中存在该单词
        if (i == len && pNode->isWord == 1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    
    
    
    int main()
    {
        char getChar[11];
        gets(getChar);
        char str1[33], str2[33];
        Init();
        while (scanf("%s%s", str1, str2), str1[0] != 'E')
        {
    
            Insert(str2, str1);//插入,单词,单词的翻译
        }
        
        getchar();
        char ansStr[3333];
        char saveStr[33];
        while (gets(ansStr), ansStr[0] != 'E')
        {
            int i;
            int k = 0;
            for (i = 0; i < strlen(ansStr); i++)
            {
                if (ansStr[i] < 'a' || ansStr[i] > 'z')
                {
                    if (ansStr[i - 1] >= 'a' && ansStr[i - 1] <= 'z')
                    {
                        saveStr[k] = '\0';
                        k = 0;
                        if (Find(saveStr))
                        {
                            printf("%s", pNode->ansChar);//如果字典中存在该单词,则输出它的翻译
                        }
                        else
                        {
                            
                            printf("%s", saveStr);//字典中不存在该单词,输出原单词
                        }
                    }
    
                    printf("%c", ansStr[i]);
                    
                }
                else
                {
                    saveStr[k++] = ansStr[i];//保存单词
                }
            }
            puts("");
        }
    
        
        return 0;
    }

    然后到map,

    注意点:

    一,mp.find()是查找mp[], 中括号里的DD是否存在

    二,map <string, string> mp中可以传入char[]的地址

    代码:

    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    int main()
    {
        char getChar[11];
        gets(getChar);
        char str1[33], str2[33];
        map <string, string> mp;
        map <string, string> :: iterator it;
        while (scanf("%s%s", str1, str2), str1[0] != 'E')
        {
            mp[str2] = str1;//可以传入char[]的地址
        }
        
        getchar();
        char ansStr[3333];
        char saveStr[33];
        while (gets(ansStr), ansStr[0] != 'E')
        {
            int i;
            int k = 0;
            for (i = 0; i < strlen(ansStr); i++)
            {
                if (ansStr[i] < 'a' || ansStr[i] > 'z')
                {
                    if (ansStr[i - 1] >= 'a' && ansStr[i - 1] <= 'z')
                    {
                        saveStr[k] = '\0';
                        k = 0;
                        it = mp.find(saveStr);
                        if (it == mp.end())
                        {
                            printf("%s", saveStr);//字典中不存在,则输出原单词
                        }
                        else
                        {
                            cout << it->second;//存在,输出翻译
                        }
                    }
    
                    printf("%c", ansStr[i]);
                    
                }
                else
                {
                    saveStr[k++] = ansStr[i];
                }
            }
            puts("");
        }
    
        
        return 0;
    }
  • 相关阅读:
    基于 kubeadm 搭建高可用的kubernetes 1.18.2 (k8s)集群- 四 部署 dashboard 2.x
    基于 kubeadm 搭建高可用的kubernetes 1.18.2 (k8s)集群- 三 集群可用性测试
    基于 kubeadm 搭建高可用的kubernetes 1.18.2 (k8s)集群二 搭建高可用集群
    基于 kubeadm 搭建高可用的kubernetes 1.18.2 (k8s)集群一 环境准备
    git 多用户多仓库配置
    百度地图结合ECharts实现复杂覆盖物(Overlay)
    【简洁易懂】CF372C Watching Fireworks is Fun (单调队列优化dp)
    博客上线测试
    Google Guava14.0 瓜娃学习笔记
    Lucene 4.7 学习及实例demo
  • 原文地址:https://www.cnblogs.com/qiufeihai/p/2732449.html
Copyright © 2011-2022 走看看