Ultraviolet A 1385-billing tables (Dictionary tree)

Source: Internet
Author: User

Link: Ultraviolet A 1385-billing tables

Given n phone prefixes, each prefix is the prefix of a region. Now a new phone bill is generated, that is, for each phone number, the old phone bill is traversed from the front to the back, if the prefix matches, the phone number corresponds to the current zone number, and the new phone number must be as small as possible.

Solution: Use DFS to create a dictionary tree. All vertices in the interval range correspond to corresponding area numbers. Note that if the values are 70, 71, 72, and so on ,... if 79 is Sb, it can be merged into 7 and the corresponding area code is sb.
Note that the merging condition is the same as the area code, which does not mean that the matching location of the corresponding old phone bill is the same.
Note This set of data: 0-9 All

#include <cstdio>#include <cstring>#include <vector>#include <string>#include <map>#include <iostream>#include <algorithm>using namespace std;const int sigma_size = 10;typedef pair<string, string> pii;struct Node {    int val;    Node* next[sigma_size];    Node() {         val = 0;        memset(next, 0, sizeof(next));    }};void clear(Node* &p);void insert (Node* &p, int d, int L, int R, int tig);void dfs(Node* p, string str);int pushup(Node* &p, int tig);map<string, int> g;int n, m;string l, r, name[105];Node* root = NULL;vector<pii> vec;void init () {    vec.clear();    g.clear();    string tmp;    for (int i = 1; i <= m; i++) {        cin >> l >> tmp >> r >> name[i];        tmp = "";        int d = l.length() - r.length();        for (int i = 0; i < d; i++)            tmp += l[i];        tmp += r;        r = tmp;        if (l > r)            continue;        int k = i;        if (g.count(name[i]))            k = g[name[i]];        else            g[name[i]] = i;        insert(root, 0, 1, 1, k);    }}int main () {    int cas = 0;    while (cin >> m) {        if (cas++)            cout << endl;        init();        pushup(root, m+1);        dfs(root, "");        clear(root);        printf("%lu\n", vec.size());        for (int i = 0; i < vec.size(); i++)            cout << vec[i].first << " " << vec[i].second << endl;    }    return 0;}int pushup (Node* &p, int tig) {    if (p == NULL) {        p = new Node;        return p->val = tig;    }    if (p->val)        tig = p->val;    int k = pushup(p->next[0], tig);    for (int i = 1; i < sigma_size; i++) {        if (k != pushup(p->next[i], tig))            k = 0;    }    return p->val = k;}void dfs (Node* p, string str) {    if (p != root && p->val) {        if (p->val <= m && name[p->val] !=  "invalid")            vec.push_back(make_pair(str, name[p->val]));        return ;    }    for (int i = 0; i < sigma_size; i++) {        if (p->next[i] != NULL) {            char ch = ‘0‘ + i;            dfs(p->next[i], str + ch);        }    }}void insert (Node* &p, int d, int L, int R, int tig) {    if (p == NULL)        p = new Node;    if (p->val)        tig = p->val;    if (d >= l.length()) {        p->val = tig;        return;    }    if (L == 0 && R == 0) {        p->val = tig;        return;    } else if (L == 0) {        insert(p->next[r[d]-‘0‘], d + 1, 0, 1, tig);        for (int i = 0; ‘0‘ + i < r[d]; i++)            insert(p->next[i], d + 1, 0, 0, tig);    } else if (R == 0) {        insert(p->next[l[d]-‘0‘], d + 1, 1, 0, tig);        for (int i = l[d] - ‘0‘ + 1; i < sigma_size; i++)            insert(p->next[i], d + 1, 0, 0, tig);    } else if (r[d] == l[d]) {        insert(p->next[l[d]-‘0‘], d + 1, 1, 1, tig);    } else {        insert(p->next[l[d]-‘0‘], d + 1, 1, 0, tig);        insert(p->next[r[d]-‘0‘], d + 1, 0, 1, tig);        for (int i = l[d] + 1; i < r[d]; i++)            insert(p->next[i-‘0‘], d + 1, 0, 0, tig);    }}void clear(Node* &p) {    for (int i = 0; i < sigma_size; i++) {        if (p->next[i] != NULL)            clear(p->next[i]);    }    delete p;    p = NULL;}

Ultraviolet A 1385-billing tables (Dictionary tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.