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

 

posted on 2012-10-20 17:56  [S*I]SImMon_WCG______*  阅读(263)  评论(0编辑  收藏  举报

导航