PTA 7-15 航空公司VIP客户查询

不少航空公司都会提供优惠的会员服务,当某顾客飞行里程累积达到一定数量后,可以使用里程积分直接兑换奖励机票或奖励升舱等服务。现给定某航空公司全体会员的飞行记录,要求实现根据身份证号码快速查询会员里程积分的功能。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6 + 10;
const int maxsize = 15;
struct Trie {
    int tot, child[maxn][maxsize];
    int val[maxn];
    void init() {
        tot = 0;
        memset(child[0], -1, sizeof(child[0]));
        memset(val, 0, sizeof(val));
    }
    void Insert(char *s, int v) {
        int root = 0;
        for (int i = 0; s[i]; i++) {
            int index;
            if (s[i] == 'x' || s[i] == 'X') index = 10;
            else index = s[i] - '0';
            int u = child[root][index];
            if (u == -1) {
                u = ++tot;
                memset(child[u], -1, sizeof(child[u]));
                child[root][index] = u;
            }
            root = u;
        }
        val[root] += v;
    }
    int Query(char *s) {
        int root = 0;
        for (int i = 0; s[i]; i++) {
            int index;
            if (s[i] == 'x' || s[i] == 'X') index = 10;
            else index = s[i] - '0';
            int u = child[root][index];
            if (u == -1) return -1;
            root = u;
        }
        return val[root];
    }
}trie;
int main() {
    trie.init();
    char str[20];
    int n, k, x; scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%s%d", str, &x);
        if (x < k) x = k;
        trie.Insert(str, x);
    }
    int q; scanf("%d", &q);
    for (int i = 1; i <= q; i++) {
        scanf("%s", str);
        x = trie.Query(str);
        if (x != -1) printf("%d\n", x);
        else printf("No Info\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值