UVA10815 - Andy's First Dictionary

题目

简单题目
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1756
使用模板,trie树处理

解题时觉得比较复杂度过高,改用trie树。
因为存在不规范字符,所以使用了scan函数。
当然可以使用
scanf("%[^A-Za-z]",s);
scanf("%[A-Za-z]",s);
处理

代码

具体代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
//#include<vector>
#include<algorithm>


#ifdef L_JUDGE
#pragma warning(disable:4996)
#endif

using namespace std;

#define MAXN 26

struct Node{
    struct Node *nodes[MAXN];
    bool bleaf;
    Node(){
        for (int i = 0; i<MAXN; i++){
            nodes[i] = NULL;
        }
        bleaf = false;
    }
};

Node *root;
char path[250];
void init(){
    root = (Node*)malloc(sizeof(Node));
    for (int i = 0; i<MAXN; i++)
        root->nodes[i] = NULL;
    root->bleaf = false;
}

bool Search(char* s, struct Node* root){
    if (*s == 0)return root->bleaf;

    int temp = s[0] - 'a';
    if (!(root->nodes[temp]))
        return Search(s + 1, root->nodes[temp]);
    return false;
}

bool Traverse(struct Node *root, int pi){
    if (root->bleaf){
        path[pi] = 0;
        printf("%s\n", path);
    }
    for (int i = 0; i<MAXN; i++){
        if (root->nodes[i]){
            path[pi] = i + 'a';
            Traverse(root->nodes[i], pi + 1);
        }
    }
    return true;
}

int Insert(char* s, Node* root){
    if (0 == *s){
        root->bleaf = true;
        return 0;
    }
    if ((*s) >= 'A' && (*s) <= 'Z'){
        s[0] = s[0] + 32;
    }
    int temp = s[0] - 'a';
    if (!root->nodes[temp]){
        root->nodes[temp] = new Node();
    //  root->nodes[temp]->bleaf = false;
    }
    Insert(s + 1, root->nodes[temp]);
    return 0;
}

int Free(Node* root){
    if (!root)return 0;
    for (int i = 0; i<MAXN; i++){
        if (root->nodes[i]){
            Free(root->nodes[i]);
            root->nodes[i] = NULL;
        }
    }
    free(root);
    return 0;
}

char in[250];

bool scan(){
    int ch;
    int ini = 0;
    in[0] = 0;
    while ((ch = getchar()),
        !((ch >= 'A'&&ch <= 'Z') || (ch >= 'a'&&ch <= 'z'))){
        if (EOF == ch)return false;
    }

    in[ini++] = ch;
    while (ch = getchar(),
        (ch >= 'A'&&ch <= 'Z') || (ch >= 'a'&&ch <= 'z')){
        in[ini++] = ch;
    }
    in[ini] = 0;
    return true;
}

int main(){
#ifdef L_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    init();
    while (1){
        scan();
        Insert(in, root);
        if (0 == in[0])break;
    }
    root->bleaf = false;
    Traverse(root, 0);

#ifdef L_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("out.txt");
#endif

    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
//#include<vector>
#include<algorithm>



#ifdef L_JUDGE
#pragma warning(disable:4996)
#endif

using namespace std;

#define MAXN 26

struct Node{
    struct Node *nodes[MAXN];
    bool bleaf;
    Node(){
        for (int i = 0; i<MAXN; i++){
            nodes[i] = NULL;
        }
        bleaf = false;
    }
};

Node *root;
char path[250];
void init(){
    root = (Node*)malloc(sizeof(Node));
    for (int i = 0; i<MAXN; i++)
        root->nodes[i] = NULL;
    root->bleaf = false;
}

bool Search(char* s, struct Node* root){
    if (*s == 0)return root->bleaf;

    int temp = s[0] - 'a';
    if (!(root->nodes[temp]))
        return Search(s + 1, root->nodes[temp]);
    return false;
}

bool Traverse(struct Node *root, int pi){
    if (root->bleaf){
        path[pi] = 0;
        printf("%s\n", path);
    }
    for (int i = 0; i<MAXN; i++){
        if (root->nodes[i]){
            path[pi] = i + 'a';
            Traverse(root->nodes[i], pi + 1);
        }
    }
    return true;
}

int Insert(char* s, Node* root){
    if (0 == *s){
        root->bleaf = true;
        return 0;
    }
    if ((*s) >= 'A' && (*s) <= 'Z'){
        s[0] = s[0] + 32;
    }
    int temp = s[0] - 'a';
    if (!root->nodes[temp]){
        root->nodes[temp] = new Node();
    //  root->nodes[temp]->bleaf = false;
    }
    Insert(s + 1, root->nodes[temp]);
    return 0;
}

int Free(Node* root){
    if (!root)return 0;
    for (int i = 0; i<MAXN; i++){
        if (root->nodes[i]){
            Free(root->nodes[i]);
            root->nodes[i] = NULL;
        }
    }
    free(root);
    return 0;
}

char in[250];
/*
bool scan(){
    int ch;
    int ini = 0;
    in[0] = 0;
    while ((ch = getchar()),
        !((ch >= 'A'&&ch <= 'Z') || (ch >= 'a'&&ch <= 'z'))){
        if (EOF == ch)return false;
    }

    in[ini++] = ch;
    while (ch = getchar(),
        (ch >= 'A'&&ch <= 'Z') || (ch >= 'a'&&ch <= 'z')){
        in[ini++] = ch;
    }
    in[ini] = 0;
    return true;
}*/

int main(){
#ifdef L_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    init();
    while (EOF!=scanf("%[^A-Za-z]",in)){
        if (EOF == scanf("%[A-Za-z]", in)){
            break;
        }
        Insert(in, root);
    }
    root->bleaf = false;
    Traverse(root, 0);

#ifdef L_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("out.txt");
#endif

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值