hdu 1800 Flying to the Mars (trie樹)

小記:這題其實是比較水的,尤其是數據,看discuss裏別人都是用int存的,30位的數int能存的下??!!


思路:trie樹來hash,記錄每一個值出現的次數,次數最多的就是答案。

這裏要注意的就是0, 如果用trie樹, 它輸入000和00 你必須判斷是同一個數0. 或者002和000002.

所以這裏要必須去掉前置0,然後再插入


代碼:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8
#define MAX 10

const int MAX_ = 100010;
const int N = 500000;
const int INF = 0x7fffffff;

char str[MAX_];


typedef struct Node{
    int isStr;
    struct Node *next[MAX];
    Node():isStr(0){
        memset(next, NULL, sizeof(next));
    }
    ~Node(){
        for(int i = 0;i < MAX; ++i)
            if(next[i] != NULL)
                delete next[i];
    }
}TrieNode,*Trie;

Trie root;

int Insert(char *s){
    while(*s == '0')++s;
    TrieNode *p = root;

    while(*s){
        if(p ->next[*s-'0'] == NULL){
            p ->next[*s-'0'] = new TrieNode;
        }
        p = p ->next[*s-'0'];
        s++;
    }
    p->isStr ++;
    return p->isStr;
}

int main() {
    int T, n, m, numa = 0, numb = 0,ans;
    bool flag = 0;
    string s = "";
    while(~scanf("%d", &n)) {
        root = new TrieNode;
        ans = 0;
        REP(i, 0, n) {
            scanf("%s", str);
            m = Insert(str);
            ans = max(ans, m);
        }
        printf("%d\n", ans);

        delete root;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章