zoukankan      html  css  js  c++  java
  • hdu 1247 Hat’s Words

    字典树的题是我一个月前做的了,到现在依然记得,他是我ac的第一个字典树题目。嘻嘻。

    ac代码:

    View Code
    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <set>
    
    using namespace std;
    
    const int maxn=26;
    const int maxlen=100;
    
    char ss1[50005][maxlen],ss2[maxlen],ss3[maxlen];
    
    struct node
    {
        bool flg;
        node *next[maxn];
        node()
        {
            flg=false;
            for(int i=0;i<maxn;i++)
                next[i]=NULL;
        }
        ~node()
        {
            for(int i=0;i<maxn;i++)
                if(next[i]!=NULL) 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->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];
            }
            return location->flg;
        }
    }t;
    
    int main()
    {
        string str1,str2;
        int line=0;
        memset(ss1,' ',sizeof(ss1));
        while(cin.getline(ss1[line],maxlen))
        {
            str1=ss1[line++];
            t.Insert(str1);
        }
        for(int i=0;i<line;i++)
        {
            for(int j=1;j<strlen(ss1[i]);j++)
            {
                str1=str2="";
                memset(ss2,' ',sizeof(ss2));
                memset(ss3,' ',sizeof(ss3));
                strncpy(ss2,ss1[i],j);
                ss2[j]='\0';
                strncpy(ss3,ss1[i]+j,strlen(ss1[i])-j);
                ss3[strlen(ss1[i])-j]='\0';
    //            cout<<ss2<<' '<<ss3<<endl;
                str1=ss2,str2=ss3;
                if(t.Search(str1)&&t.Search(str2))
                {
                    cout<<ss1[i]<<endl;
    //                system("pause");
                    break;
                }
            }
    
        }
        return 0;
    }

    欢迎批评指正。谢谢!

    勸君惜取少年時&莫待無花空折枝
  • 相关阅读:
    团队项目—第二次冲刺计划
    第一阶段冲刺总结
    团队绩效评估
    回复其他小组对我们的评价
    软件项目评价
    maven 之分模块构建web项目 及 聚合与继承特性
    Log4j 使用总结
    freemarker 总结
    freemarker入门 之 脱离容器实现hello word
    读取txt文件 统计“java”出现的次数(大小写不敏感)
  • 原文地址:https://www.cnblogs.com/RainingDays/p/2766571.html
Copyright © 2011-2022 走看看