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;
    }

    欢迎批评指正。谢谢!

    勸君惜取少年時&莫待無花空折枝
  • 相关阅读:
    《.Net之美》样章 1.1 理解泛型(转载)
    jQuery&JSON~~
    TreeView绑定XML
    TreeView 的 CheckBox 被点击时的引发页面回发事件
    开发中巧用Enum枚举类型
    今天开始学习Python
    获取天气预报
    ORACLE数据库备份
    Eclipse配置Tomcat
    ORACLE常见错误及解决办法
  • 原文地址:https://www.cnblogs.com/RainingDays/p/2766579.html
Copyright © 2011-2022 走看看