hdu 1075 What Are You Talking About

类似hash的题吧,翻译文稿。字典树搞的。

ac代码:

View Code
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <set>

using namespace std;

const int maxn=26;
const int maxlen=3001;

string tt,sss;

struct node
{
    bool flg;
    string str;
    node *next[maxn];
    node()
    {
        flg=false;
        str="";
        for(int i=0;i<maxn;i++)
            next[i]=NULL;
    }
};

class Trie
{
    public :
    node *root;
    Trie()
    {
        root=NULL;
    }
    void Insert(string str)
    {
        if(!root)
            root=new node();
        node *location=root;
        for(int i=0;i<str.length();i++)
        {
            int num=str[i]-'a';
            if(location->next[num]==NULL)
                location->next[num]=new node();
            location=location->next[num];
        }
        location->str=tt;
        location->flg=true;
    }
    bool Search(string str)
    {
        node *location=root;
        for(int i=0;i<str.length();i++)
        {
            int num=str[i]-'a';
            if(location->next[num]==NULL)
                return false;
            location=location->next[num];
        }
        if(location->flg)
        {
            sss=location->str;
            return true;
        }
        else
            return false;
    }
}t;

int main()
{
    char tmp[maxlen],ss[maxlen*2];
    string s;
    gets(tmp);
//    puts(tmp);
    while(cin.getline(ss,maxlen*2))
    {
        tt=s="";
        for(int i=0;i<strlen(ss);i++)
        {
            if(ss[i]==' ')
            {
                strncpy(tmp,ss+i+1,strlen(ss)-i);
                s=tmp;
                break;
            }
            else
                tt+=ss[i];
        }
//        cout<<tt<<"fff"<<s<<endl;
        if(tt=="END") break;
        else t.Insert(s);
    }
    gets(tmp);
    while(gets(ss))
    {
        if(!strcmp(ss,"END")) break;
        s="";
        for(int i=0;i<strlen(ss);i++)
        {
            if(ss[i]>='a'&&ss[i]<='z')
            {
                s+=ss[i];
            }
            else
            {
//                cout<<s<<endl;
//                system("pause");
                if(s=="")
                    cout<<ss[i];
                else if(t.Search(s))
                {
                    cout<<sss<<ss[i];
                    s="";
                    continue;
                }
                else
                {
//                    system("pause");
                    cout<<s<<ss[i];
                    s="";
                }
            }
        }
        cout<<endl;
    }
    return 0;
}

欢迎批评指正。谢谢!

勸君惜取少年時&莫待無花空折枝
原文地址:https://www.cnblogs.com/RainingDays/p/2766565.html