美文网首页
poj1056 Trie

poj1056 Trie

作者: 暖昼氤氲 | 来源:发表于2019-11-03 15:49 被阅读0次
/*
Time:2019.11.3
Author: Goven
type:Trie字典树 
err:
ref:
知识点:字典树:https://blog.csdn.net/johnny901114/article/details/80711441 
*/
/*
数据结构:孩子表示法——二叉树 
ref: https://www.cnblogs.com/pdev/p/4022680.html
*/ 
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define MAX_SZ 80
using namespace std;

bool flag;
int sz; //表示节点的数量
int a[MAX_SZ][2]; //a[i][0-1]表示第i号节点的两个孩子的编号
bool tag[MAX_SZ]; //表示节点是否为结尾

void Trie() {
    sz = 0;
    memset(tag, 0, sizeof(tag));
    memset(a, 0, sizeof(a));
}
void insert(string s) {//插入
    int l = s.length();
    int t = 0, c; //t表示双亲节点的编号 
    for (int i = 0; i < l; i++) {
        c = s[i] - '0';
        if (a[t][c] == 0) {
            a[t][c] = ++sz;
            
        }
        t = a[t][c];
    }
    tag[t] = true;
} 
bool query(string s) {//前缀查询 
    int l = s.length();
    int t = 0, c; //t表示双亲节点的编号 
    for (int i = 0; i < l; i++) {
        c = s[i] - '0';
        if (a[t][c] == 0) return true;
        t = a[t][c];
        if (tag[t]) return false;
    }
    return false;
}

int main()
{
    string s;
    flag = true;
    Trie();
    int n = 0;
    while (cin >> s) {
        if (s == "9") {
            n++;
            if (flag) cout << "Set " << n << " is immediately decodable" << endl;
            else cout << "Set " << n << " is not immediately decodable" << endl;
            flag = true;
            Trie();
        }
        
        if (query(s) == false) flag = false;
        if (flag) insert(s);
        
    }
    return 0;
}

/*错误版,但是找不出错误
//数据结构:孩子表示法——二叉树 
//ref: https://www.cnblogs.com/pdev/p/4022680.html
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define MAX_SZ 80
using namespace std;

bool flag;
int sz; //表示节点的数量
int a[MAX_SZ][2]; //a[i][0-1]表示第i号节点的两个孩子的编号
bool tag[MAX_SZ]; //表示节点是否为结尾

void Trie() {
    sz = 0;
    memset(tag, 0, sizeof(tag));
    memset(a, 0, sizeof(a));
}
void insert(string s) {////插入与判断前缀在一起
    int l = s.length();
    int t = 0, c; //t表示双亲节点的编号 
    for (int i = 0; i < l; i++) {
        c = s[i] - '0';
        if (a[t][c] == 0) {
            a[t][c] = ++sz;
        }
        else if (tag[a[t][c]]) {
            flag = false;
//          cout << 1 <<endl;
            return;
        }
        t = a[t][c];
    }
    if (a[t][0] || a[t][1]) flag = false;
    tag[t] = true;
} 

int main()
{
    string s;
    flag = true;
    Trie();
    int n = 0;
    while (cin >> s) {
        if (s == "9") {
            n++;
            if (flag) cout << "Set " << n << " is immediately decodable" << endl;
            else cout << "Set " << n << " is not immediately decodable" << endl;
            flag = true;
            Trie();
        }
        if (flag) insert(s);
        
    }
    return 0;
}
*/

/*
数据结构:链表 
ref: https://blog.csdn.net/xiaofengcanyuexj/article/details/9731677:
*/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

struct Trie {
    bool flag;//标志是否为结尾
    Trie *next[2];//指向子节点
    
    Trie() {
        flag = 0;
        next[0] = next[1] = NULL;
    } 
     
};
bool flag;
Trie* root;
void insert(string s) {//插入与判断前缀在一起 
    Trie *t = root;
    int l = s.length();
    for (int i = 0; i < l; i++) {
        if (t -> next[s[i] - '0'] == NULL) {
            t -> next[s[i] - '0'] = new Trie();
        }
        else if ((t -> next[s[i] - '0'] -> flag) || (i + 1 == l)){
            flag = false;
            return;
        }   
        t = t -> next[s[i] - '0'];  
            
    }
    t -> flag = 1;
}
void mfree(Trie *r) {
    if (!r) return;
    mfree(r -> next[0]);
    mfree(r -> next[1]);
    free(r);
}
int main()
{
    string s;
    int n = 0;
    while (cin >> s) {
        flag = true;
        root = new Trie();
        insert(s);
        n++;
        
        while (cin >> s) {
            if (s == "9") break;
            if (flag) insert(s);
        }
        if (flag) cout << "Set " << n << " is immediately decodable" << endl;
        else cout << "Set " << n << " is not immediately decodable" << endl;
        mfree(root);
    }
    return 0;
}

 
//暴力 my
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

bool cmp (string a, string b) {
    return a.length() < b.length();
}

int main()
{
    string a[10];
    int n = 1;
    while (cin >> a[0]) {
        int cnt = 1;
        while (cin >> a[cnt]) {
            if (a[cnt] == "9") break;
            cnt++;
        }
        sort(a, a + cnt, cmp);
        int flag = 0;
        for (int i = 0; i < cnt; i++) {
            for (int j = i + 1; j < cnt; j++) {
                if (a[i] == a[j].substr(0,a[i].length())) {
                    flag = 1;
                    break;
                }
            }
        }
        if (flag) cout << "Set " << n << " is not immediately decodable" << endl;
        else cout << "Set " << n << " is immediately decodable" << endl;
        n++;
    } 
    return 0;
}

*/


/*//暴力 
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    string a[10];
    bool flag = false;
    int n = 0, cnt = 0;
    while (cin >> a[cnt]) {
        if (a[cnt] == "9") {
            n++;
            if (flag) cout << "Set " << n << " is not immediately decodable" << endl;
            else cout << "Set " << n << " is immediately decodable" << endl;
            cnt  = 0; flag = false;
        }
        else {
            int l1 = a[cnt].length();
            for (int i = 0; i < cnt; i++) {
                int l2 = a[i].length();
                if (l1 < l2 && a[cnt] == a[i].substr(0, l1)) {
                    flag = true; break;
                }
                else if (l1 > l2 && a[cnt].substr(0, l2) == a[i]) {
                    flag = true; break;
                } 
            }
            cnt++;
        }
    }
    return 0;
}

相关文章

网友评论

      本文标题:poj1056 Trie

      本文链接:https://www.haomeiwen.com/subject/rqxlbctx.html