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

题意:

翻译火星文。

题解:

不好写,因为有标点符号!!

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
typedef long long lld;
const int oo=0x3f3f3f3f;
const lld OO=1e18;
const int Mod=1000000007;
const int maxn=5000+5;
const int maxm=12;
int n,m,T;
char mars[12],word[12];
char sense[3005];

void output(char str[],int s,int e)
{
    for(int i=s;i<=e;i++)
        printf("%c",str[i]);
}

struct Trie
{
    struct TrieNode
    {
        int nCount;
        char word[15];
        TrieNode* child[26];
        TrieNode()
        {
            memset(child,NULL,sizeof child);
            nCount=-1;
        }
    }*root;

    Trie()
    {
        root=new TrieNode();
    }

    void Insert(char str[],char word[])
    {
        TrieNode* p=root;
        for(int i=0,k;str[i];i++,p=p->child[k])
        {
            k=str[i]-'a';
            if(!p->child[k])
                p->child[k]=new TrieNode();
        }
        p->nCount=1;
        strcpy(p->word,word);
    }

    int Search(char str[],int s,int e)
    {
        TrieNode* p=root;
        for(int i=s,k;i<=e;i++)
        {
            k=str[i]-'a';
            if(p->child[k])
                p=p->child[k];
            else
                return -1;
        }
        if(p->nCount!=-1)
            printf("%s",p->word);
        return p->nCount;
    }

    void Free(TrieNode* Root)
    {
        TrieNode* p=Root;
        for(int i=0;i<26;i++)
            if(p->child[i])
                Free(p->child[i]);
        delete p;
    }

    ~Trie()
    {
        Free(root);
    }

};
///字典树数据结构到此为止

int main()
{
    Trie tree;
    int cnt=0;
    while(scanf("%s",word)&&word[0]!='E')
    {
        if(word[0]=='S')continue;
        scanf("%s",mars);
        tree.Insert(mars,word);
    }
    int temp,cur,e;
    getchar();
    while(gets(sense))
    {
        if(sense[0]=='E')break;
        if(sense[0]=='S')continue;
        for(int i=0;sense[i];i++)
        {
            if(isalpha(sense[i]))
            {
                for(e=i;sense[e];e++)
                    if(!isalpha(sense[e]))
                        break;
                cur=tree.Search(sense,i,e-1);
                if(cur==-1)
                    output(sense,i,e-1);
                i=e-1;
            }
            else
                printf("%c",sense[i]);
        }
        puts("");
    }
    return 0;
}
/**
1
3 5
46
64448
74
go
in
night
might
gn
*/







你可能感兴趣的:(hdu 1075 What Are You Talking About (字典树))