zoukankan      html  css  js  c++  java
  • hdu-1800

    思路:

    这题被坑的不轻。

    首先花了一段时间想明白了思路是要找出现次数最多数字,以为这题就这样解决了,结果发现每个数字的最大长度是30,long long都装不下,因此就要用字符串来保存处理。然后在insert的时候进行一下计数就可以了

    最关键的地方是struct内的构造函数中,在初始化next数组的时候,一定要面面俱到!不然漏掉哪个,如果没有初始化,就相当于为空指针,就会出现内存泄露,就因为这点被卡了好久。

    AC代码:

    #include <iostream>
    #include <cstring>
    #define max(a,b) (a)>(b)?(a):(b)
    using namespace std;
    
    struct node
    {
        int e;
        node* next[11];
        node():e(0){
            for(int i = 0;i <= 10;i++)
                next[i] = 0;
        }
    };
    node* root;
    int ans;
    
    void insert(char* s)
    {
        int n;
        node* p = root;
        for(;*s!='';s++)
        {
            n = *s-'0';
            if(p->next[n] == 0)
                p->next[n] = new node();
            p = p->next[n];
        }
        p->e++;
        ans = max(ans,p->e);
    }
    
    int main()
    {
        int j;
        int n;
        while(cin>>n)
        {
            ans = 0;
            root = new node();
            char str[37];
            for(int i = 1;i <= n;i++) {
                cin>>str;
                for(j = 0;j < strlen(str);j++)
                    if(str[j] != '0') 
                        break;
                insert(&str[j]);
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    java8 Stream排序字段为空排序方法
    SpringBoot使用token简单鉴权的具体实现方法
    性能调优
    TestNG最简单的测试
    TestNG异常测试
    TestNG中如何执行测试
    TestNG的基本注解
    TestNG介绍
    TestNG 环境搭建
    python第四课笔记
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/5202916.html
Copyright © 2011-2022 走看看