字典树基础题总结

HDU 1251 统计难题

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 26

struct Trie{
    int val;
    Trie *next[MAX];
};

Trie *root;

void Insert(char *s){
    int n = strlen(s);
    Trie *pre = root,*cur;
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        if(pre->next[id] == NULL){
            cur = new Trie();
            cur->val = 1;
            for(int j = 0; j < MAX; j++) cur->next[j] = NULL;
            pre->next[id] = cur;
            pre = pre->next[id];
        }else{
            pre = pre->next[id];
            pre->val++;
        }
    }
}

int Find(char *s){
    int n = strlen(s);
    Trie *pre = root;
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        if(pre->next[id] == NULL) return 0;
        pre = pre->next[id];
    }
    return pre->val;
}

void Delete(Trie *rt){
    if(NULL == rt) return ;
    for(int i = 0; i < MAX; i++){
        if(rt->next[i] != NULL) Delete(rt->next[i]);
    }
    delete rt;
    rt = NULL;
}

int main(){
    char s[15];
    root = new Trie();
    for(int i = 0; i < MAX; i++)
        root->next[i] = NULL;
    while(gets(s) && strcmp(s,"") != 0){
        Insert(s);
    }

    while(gets(s) != NULL){
        printf("%d\n", Find(s));
    }
    Delete(root);
    return 0;
}

HDU 1305Immediate Decodability

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 10

struct Trie{
    int val;
    Trie *next[MAX];
};

Trie *root;

bool Insert(char *s){
    int n = strlen(s);
    Trie *pre = root,*cur;
    int flag = 1;
    for(int i = 0; i < n; i++){
        int id = s[i] - '0';
        if(pre->next[id] == NULL){
            flag = 0;
            cur = new Trie();
            cur->val = 0;
            for(int j = 0; j < MAX; j++) cur->next[j] = NULL;
            pre->next[id] = cur;
            pre = pre->next[id];
        }
        else{
            pre = pre->next[id];
            if(pre->val) return false;
        }
    }
    pre->val = 1;
    if(flag) return false;
    else return true;
}

void Delete(Trie *rt){
    if(NULL == rt) return ;
    for(int i = 0; i < MAX; i++){
        if(rt->next[i] != NULL) Delete(rt->next[i]);
    }
    delete rt;
    rt = NULL;
}

int main(){
    char s[15];
    root = new Trie();
    for(int i = 0; i < MAX; i++)
        root->next[i] = NULL;
    int kase = 1,flag = 1;
    while(scanf("%s", s) != EOF){
        if(s[0] == '9'){
            if(flag == 1)
                printf("Set %d is immediately decodable\n", kase++);
            else
                printf("Set %d is not immediately decodable\n", kase++);
            Delete(root);
            flag = 1;
            root = new Trie();
            for(int i = 0; i < MAX; i++)
                root->next[i] = NULL;
        }
        if(!Insert(s)) flag = 0;

    }
    return 0;
}

HDU 1075 What Are You Talking About

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX  26

struct Trie{
    int val;
    char str[12];
    Trie *next[MAX];
};

Trie *root;

void Insert(char *s1, char *s2){
    int n = strlen(s1);
    Trie *pre = root, *cur;
    for(int i = 0; i < n; i++){
        int id = s1[i]-'a';
        if(pre->next[id] == NULL){
            cur = new Trie();
            cur->val = 0;
            for(int j = 0; j < MAX; j++) cur->next[j] = NULL;
            pre->next[id] = cur;
        }
        pre = pre->next[id];
    }
    pre->val = 1;
    strcpy(pre->str, s2);
}

bool Search(char *s, char *ans){
    Trie *pre = root;
    int n = strlen(s);
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        if(pre->next[id] == NULL) return false;
        pre = pre->next[id];
    }
    if(pre->val){
        strcpy(ans, pre->str);
        return true;
    }
    return false;
}

void Delete(Trie *rt){
    if(NULL == rt) return ;
    for(int i = 0; i < MAX; i++)
        if(rt->next[i] != NULL) Delete(rt->next[i]);
    delete rt;
    rt = NULL;
}

char s[3005],s1[12],s2[12],ans[12],temp[12];

int main(){
    root = new Trie();
    for(int i = 0; i < MAX; i++) root->next[i] = NULL;
    while(scanf("%s", s1) != EOF){
        if(s1[0] == 'S') continue;
        if(s1[0] == 'E'){
            getchar();
            while(gets(s)){
                if(s[0] == 'S') continue;
                if(s[0] == 'E'){
                    Delete(root);
                    root = new Trie();
                    for(int i = 0; i < MAX; i++) root->next[i] = NULL;
                    break;
                }
                int i,j,len = strlen(s);
                for(i = 0; i < len; i++){
                    if(s[i] >= 'a' && s[i] <= 'z'){
                        for(j = i; j < len; j++)
                            if(s[j] < 'a' || s[j] > 'z') break;
                        int cnt = 0;
                        for(int k = i; k < j; k++)
                            temp[cnt++] = s[k];
                        temp[cnt] = '\0';
                        if(Search(temp,ans)) printf("%s", ans);
                        else printf("%s", temp);
                        i = j-1;
                    }
                    else printf("%c", s[i]);
                }
                printf("\n");
            }
        }
        scanf("%s", s2);
        Insert(s2,s1);
    }
    return 0;
}

HDU 1247 Hat’s Words

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 26
char s[50005][105],s1[105],s2[105];

struct Trie{
    int val;
    Trie *next[MAX];
};

Trie *root;

void Insert(char *s){
    int n = strlen(s);
    Trie *pre = root,*cur;
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        if(pre->next[id] == NULL){
            cur = new Trie();
            cur->val = 0;
            for(int j = 0; j < MAX; j++) cur->next[j] = NULL;
            pre->next[id] = cur;
        }
        pre = pre->next[id];
    }
    pre->val = 1;
}

bool Search(char *s){
    int n = strlen(s);
    Trie *pre = root;
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        if(pre->next[id] == NULL) return false;
        pre = pre->next[id];
    }
    if(pre->val) return true;
    else return false;
}

void solve(char s[50005][105],int n){
    for(int i = 0; i < n; i++){
        int m = strlen(s[i]);
        for(int j = 1; j < m; j++){
            memset(s1, '\0', sizeof(s1));
            memset(s2, '\0', sizeof(s2));
            strncpy(s1, s[i], j);
            strcpy(s2, s[i]+j);
            if(Search(s1) && Search(s2)){
                printf("%s\n", s[i]);
                break;
            }
        }
    }
}

void Delete(Trie *rt){
    if(NULL == rt) return ;
    for(int i = 0; i < MAX; i++) Delete(rt->next[i]);
    delete rt;
    rt = NULL;
}

int main(){
    int i = 0;
    root = new Trie();
    root->val = 0;
    for(int j = 0; j < MAX; j++) root->next[i] = NULL;
    while(scanf("%s", s[i]) != EOF){
        Insert(s[i]);
        i++;
    }
    solve(s, i);
    Delete(root);
    return 0;
}

HDU 1671 Phone List

> 静态建树

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int maxnode = 500000;
const int sigma_size = 11;

struct Trie
{
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int sz;
    Trie()
    {
        sz = 1 ;
        memset(ch[0],0,sizeof(ch[0]));
    }
    int idx(char c)
    {
        return c - '0';
    }
    int Insert(char *s)
    {
        int u = 0,n = strlen(s);
        int flag;
        for(int i = 0;i < n ;i++)
        {
            int c = idx(s[i]);
            flag = 1;
            if(!ch[u][c])
            {
                flag = 0;
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
            if(val[u]) return 0;
        }
        if(flag) return 0;
        val[u] = 1;
        return 1;
    }
    void Delete()
    {
        memset(ch,0,sizeof(ch));
        memset(val,0,sizeof(val));
        sz = 1 ;
    }
};
char s[45];
Trie tree;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int N;
        scanf("%d",&N);
        int flag = 1;
        while(N--)
        {
            scanf("%s",s);
            if(tree.Insert(s) == 0)
                flag = 0;
        }
        tree.Delete();
        if(flag)  printf("YES\n");
        else   printf("NO\n");
    }
}

动态建树

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 10

struct Trie{
    int val;
    Trie *next[MAX];
};

Trie *root;

bool Insert(char *s){
    int n = strlen(s);
    Trie *pre = root,*cur;
    int flag = 1;
    for(int i = 0; i < n; i++){
        int id = s[i] - '0';
        if(pre->next[id] == NULL){
            flag = 0;
            cur = new Trie();
            cur->val = 0;
            for(int j = 0; j < MAX; j++) cur->next[j] = NULL;
            pre->next[id] = cur;
            pre = pre->next[id];
        }
        else{
            pre = pre->next[id];
            if(pre->val) return false;
        }
    }
    pre->val = 1;
    if(flag) return false;
    else return true;
}

void Delete(Trie *rt){
    if(NULL == rt) return ;
    for(int i = 0; i < MAX; i++){
        if(rt->next[i] != NULL) Delete(rt->next[i]);
    }
    delete rt;
    rt = NULL;
}

int main(){
    char s[15];
    int T,n;
    scanf("%d", &T);
    while(T--){
        root = new Trie();
        for(int i = 0; i < MAX; i++)
            root->next[i] = NULL;
        scanf("%d", &n);
        int flag = 1;
        while(n--){
            scanf("%s", s);
            if(!Insert(s)) flag = 0;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        Delete(root);
    }
    return 0;
}

HDU 2846 Repository

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 26

struct Trie{
    int val,flag;
    Trie *next[MAX];
};

Trie *root;

void Insert(char *s, int log){
    int n = strlen(s);
    for(int i = 0; i < n; i++){
        Trie *pre = root, *cur;
        for(int j = i; j < n; j++){
            int id = s[j] - 'a';
            if(pre->next[id] == NULL){
                cur = new Trie();
                cur->val = 1;
                cur->flag = log;
                for(int k = 0; k < MAX; k++) cur->next[k] = NULL;
                pre->next[id] = cur;
                pre = pre->next[id];
            }
            else{
                pre = pre->next[id];
                if(pre->flag != log){
                    pre->flag = log;
                    pre->val++;
                }
            }
        }
    }
}

int Search(char *s){
    int n = strlen(s);
    Trie *pre = root;
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        if(pre->next[id] == NULL) return 0;
        pre = pre->next[id];
    }
    return pre->val;
}

void Delete(Trie *rt){
    if(rt == NULL) return ;
    for(int i = 0; i < MAX; i++)
        if(rt->next[i] != NULL) Delete(rt->next[i]);
    delete rt;
    rt = NULL;
}

char s[25];

int main(){
    root = new Trie();
    root->val = 0;
    for(int i = 0; i < MAX; i++) root->next[i] = NULL;
    int P,Q;
    scanf("%d", &P);
    for(int i = 1; i <= P; i++){
        scanf("%s", s);
        Insert(s, i);
    }
    scanf("%d", &Q);
    while(Q--){
        scanf("%s", s);
        printf("%d\n", Search(s));
    }
    Delete(root);
    return 0;
}

HDU 5687 Problem C

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
const int maxnode = 3000000;
const int sigma_size = 27;

struct Node
{
    int x,y;
    Node(int a,int b):x(a),y(b){}
    Node() {}
};

struct Trie
{
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int sz;
    Trie() {sz = 1; memset(ch[0], 0, sizeof(ch[0]));}
    int idx(char c) {return c - 'a';}
    void Insert(char *s)
    {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++)
        {
            int c = idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 1;
                ch[u][c] = sz++;
                u = ch[u][c];
            }
            else
            {
                u = ch[u][c];
                val[u]++;
            }
        }
    }

    bool Search(char *s)
    {
        int u = 0,n = strlen(s);
        for(int i = 0; i < n; i++)
        {
            int c = idx(s[i]);
            if(!ch[u][c])
                return false;
            else u = ch[u][c];
        }
        return true;
    }

    void Delete(char *s)
    {
        stack <Node> S;
        while(!S.empty()) S.pop();
        int u = 0,n = strlen(s),f,c;
        for(int i = 0; i < n; i++)
        {
            c = idx(s[i]);
            S.push(Node(u,c));
            if(!ch[u][c])
                return;
            else
            {
                f = u;
                u = ch[u][c];
            }
        }
        ch[f][c] = 0;
        if(val[f] - val[u] == 0)
        {
            S.pop();
            while(!S.empty())
            {
                Node node = S.top();
                S.pop();
                int temp = ch[node.x][node.y];
 //               printf("%d %d\n", node.x,node.y);
                if(val[temp] - val[u]) break;
                else ch[node.x][node.y] = 0;
            }
        }
    }
};

Trie tree;

int main()
{
    int N;
    scanf("%d", &N);
    char op[10],s[35];
    while(N--)
    {
        scanf("%s %s", op, s);
        if(op[0] == 'i')
            tree.Insert(s);
        else if(op[0] == 's')
        {
            if(tree.Search(s)) printf("Yes\n");
            else printf("No\n");
        }
        else if(op[0] == 'd')
            tree.Delete(s);
    }
    return 0;
}

POJ 2503 Babelfish

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX  26

struct Trie{
    int val;
    char str[15];
    Trie *next[MAX];
};

Trie *root;

void Insert(char *s1, char *s2){
    int n = strlen(s1);
    Trie *pre = root, *cur;
    for(int i = 0; i < n; i++){
        int id = s1[i]-'a';
        if(pre->next[id] == NULL){
            cur = new Trie();
            cur->val = 0;
            for(int j = 0; j < MAX; j++) cur->next[j] = NULL;
            pre->next[id] = cur;
        }
        pre = pre->next[id];
    }
    pre->val = 1;
    strcpy(pre->str, s2);
}

bool Search(char *s, char *ans){
    Trie *pre = root;
    int n = strlen(s);
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        if(pre->next[id] == NULL) return false;
        pre = pre->next[id];
    }
    if(pre->val){
        strcpy(ans, pre->str);
        return true;
    }
    return false;
}

char s[35],s1[15],s2[15],ans[15];

int main(){
    root = new Trie();
    for(int i = 0; i < MAX; i++) root->next[i] = NULL;
    while(gets(s) && strcmp(s, "")){
        for(int i = 0; i < (int)strlen(s); i++){
            if(s[i] == ' '){
                strcpy(s2, s+i+1);
                strncpy(s1, s, i);
                Insert(s2, s1);
                break;
            }
        }
    }
    while(gets(s) != NULL){
        if(Search(s, ans))
            printf("%s\n", ans);
        else
            printf("eh\n");
    }
    return 0;
}

POJ 2001 Shortest Prefixes

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 26

struct Trie{
    int val;
    Trie *next[MAX];
};

Trie *root;

void Insert(char *s){
    int n = strlen(s);
    Trie *pre = root, *cur;
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        if(pre->next[id] == NULL){
            cur = new Trie();
            cur->val = 1;
            for(int j = 0; j < MAX; j++) cur->next[j] = NULL;
            pre->next[id] = cur;
            pre = pre->next[id];
        }
        else{
            pre = pre->next[id];
            pre->val++;
        }
    }
}

void Search(vector<char>& ans, char *s){
    int n = strlen(s);
    Trie *pre = root;
    for(int i = 0; i < n; i++){
        int id = s[i] - 'a';
        ans.push_back(s[i]);
        pre = pre->next[id];
        if(pre->val == 1) break;
    }
}

void Delete(Trie *rt){
    if(NULL == rt) return ;
    for(int i = 0; i < MAX; i++)
        if(rt->next[i] != NULL) Delete(rt->next[i]);
    delete rt;
    rt = NULL;
}

char s[1005][25];
vector <char> ans;

int main(){
    int i = 0;
    root = new Trie();
    root->val = 0;
    for(int j = 0; j < MAX; j++) root->next[j] = NULL;
    while(scanf("%s", s[i]) != EOF){
        Insert(s[i++]);
    }
    for(int j = 0; j < i; j++){
        ans.clear();
        Search(ans, s[j]);
        printf("%s ", s[j]);
        for(int k = 0; k < ans.size(); k++)
            printf("%c", ans[k]);
        printf("\n");
    }
    Delete(root);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值