Home >Backend Development >PHP Tutorial >Phone List(HDOJ-1671) (tire tree)_PHP tutorial

Phone List(HDOJ-1671) (tire tree)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:54:251327browse

Phone List(HDOJ-1671) (tire tree)

The correct solution is a dictionary tree, a data structure implemented using linked lists. The construction method is similar to the binary tree in the purple book. Because the memory for this question is relatively tight, the memory problem needs to be solved. However, recursively releasing the memory will lead to low efficiency. The solution is to open a memory pool (array) and reuse it every time the subscript is updated.

#include<cstdio>
#include<cstring>
#include<iostream>
#include
using namespace std;
int T,n,k;
struct pa{
    char s[15];
    int len;
};
bool cmp(pa a,pa b){
    return a.len>b.len;
}
struct trie{
    trie *next[15];
};
trie *root;
trie all_trie[1000000];
bool built(char *s,int len) {
    bool ok = true;
    trie *p = root, *q;
    for(int i=0;i<len;i++){ id="s[i]-'0';" int="" p-="">next[id]==NULL) {
            ok = false;
            q = &all_trie[k++];
            for(int j=0;j<10;j++) q->next[j] = NULL;
            p->next[id] = q;
            p = p->next[id];
        }
        else {
            p = p->next[id];
        }
    }
    return ok;
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        pa s[10005];
        k = 0;
        bool ok = true;
        root = &all_trie[k++];
        for(int i=0;i<10;i++) root->next[i] = NULL;
        for(int i=0;i<n;i++){ .len="strlen(s[i].s);" else="" i="0;i<n;i++)" int="" ok="false;" pre="" return=""><p> </p></n;i++){></len;i++){></algorithm></iostream></cstring></cstdio>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/997428.htmlTechArticlePhone List(HDOJ-1671) (tire tree) The correct answer is a dictionary tree, a data structure implemented using a linked list , the construction method is similar to the binary tree in the purple book. Because the memory for this question is relatively small...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn