zoukankan      html  css  js  c++  java
  • hdu 1251 统计难题

    汉语提,题意清楚。当初还卡了我一个上午。哎。

    ac代码:

    View Code
    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <set>
    
    using namespace std;
    
    const int maxn=26;
    
    struct node
    {
        int cnt;
        node *next[maxn];
        node()
        {
            cnt=0;
            for(int i=0;i<maxn;i++)
                next[i]=NULL;
        }
        ~node()
        {
            for(int i=0;i<maxn;i++)
                if(next[i]!=NULL) delete next[i];
        }
    };
    
    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->next[num]->cnt++;
                location=location->next[num];
            }
        }
        int 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 0;
                location=location->next[num];
            }
            return location->cnt;
        }
    }t;
    
    int main()
    {
        char ss[20];
        string str;
        while(cin.getline(ss,20) )
        {
            str = ss;
            if(str== "") break;
    //        for(int i = 0 ;i < strlen(ss); i ++) str += ss[i];
    //        cout << "str == " << str  << endl;
            t.Insert(str);
    //        getchar();
    //        ch = getchar();
        }
        while(cin.getline(ss,20))
        {
    //        cout << "fff " << str << endl;
            str=ss;
            cout<<t.Search(str)<<endl;
        }
        return 0;
    }

    欢迎批评指正。谢谢!

    勸君惜取少年時&莫待無花空折枝
  • 相关阅读:
    kubernetes上安装MongoDB-3.6.5集群副本集方式
    kubernetes Metrics-server 安装
    kubenetes 应用更新
    filebeat+logstash通过zabbix微信报警
    Redis持久化及复制
    kubernetes rabbitmq 集群安装配置
    kubernetes elasticsearch2.4 集群安装
    kubernetes --> kube-dns 安装
    详解Javascript中的Object对象
    提高代码质量:如何编写函数
  • 原文地址:https://www.cnblogs.com/RainingDays/p/2766579.html
Copyright © 2011-2022 走看看