zoukankan      html  css  js  c++  java
  • 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;
    }

    欢迎批评指正。谢谢!

    勸君惜取少年時&莫待無花空折枝
  • 相关阅读:
    Linux系统 关机/重启/用户切换/注销,用户管理(用户创建/修改,用户组增加/删除),Linux中 / 和 ~ 的区别
    jQuery的ajax技术
    jQuery事件
    动态轮播图
    焦点式轮播图
    小米官网案例
    选项卡嵌套
    jQuery筛选方法
    导航条案例
    jQuery位置属性
  • 原文地址:https://www.cnblogs.com/RainingDays/p/2766565.html
Copyright © 2011-2022 走看看