我是靠谱客的博主 个性水杯,最近开发中收集的这篇文章主要介绍PTA 航空公司VIP客户查询 (25分) map或者字典树,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

输入首先给出两个正整数N(≤10​5​​)和K(≤500)。其中K是最低里程,即为照顾乘坐短程航班的会员,航空公司还会将航程低于K公里的航班也按K公里累积。随后N行,每行给出一条飞行记录。飞行记录的输入格式为:18位身份证号码(空格)飞行里程。其中身份证号码由17位数字加最后一位校验码组成,校验码的取值范围为0~9和x共11个符号;飞行里程单位为公里,是(0, 15 000]区间内的整数。然后给出一个正整数M(≤10​5​​),随后给出M行查询人的身份证号码。
输出格式:

对每个查询人,给出其当前的里程累积值。如果该人不是会员,则输出No Info。每个查询结果占一行。
输入样例:

4 500
330106199010080419 499
110108198403100012 15000
120104195510156021 800
330106199010080419 1
4
120104195510156021
110108198403100012
330106199010080419
33010619901008041x

输出样例:

800
15000
1000
No Info

map或者字典树查询即可

  • cin记得开读入挂ios::sync_with_stdio(false);
#define debug
#ifdef debug
#include <time.h>
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <unordered_map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e6+7)
#define ll long long 
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) 
do { 
cout << "33[31;1m " << #x << " -> "; 
err(x); 
} while (0)
void err() { cout << "33[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO {
char print_f[105];
void read() { }
void print() { putchar('n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K, tot;
int tree[MAXN][16], ans[MAXN];
#define ch(x) (x>='0'&&x<='9' ? x-'0' : 10)
#define chi (ch(s[i]))
void insert(char* s, int val) {
val = max(val, m);
int now = 0;
for(int i=0; s[i]; i++) {
if(!tree[now][chi]) tree[now][chi] = ++ tot;
now = tree[now][chi];
}
ans[now] += val;
}
int search(char* s) {
int now = 0;
for(int i=0; s[i]; i++) {
if(!tree[now][chi]) return -1;
now = tree[now][chi];
}
return ans[now];
}
signed main() {
#ifdef debug
freopen("test", "r", stdin);
clock_t stime = clock();
#endif
#if 0
//使用STL::map
ios::sync_with_stdio(false);
cin >> n >> m;
int x;
string str;
unordered_map<string, int> mp;
for(int i=1; i<=n; i++) {
cin >> str >> x;
x = max(x, m);
mp[str] += x;
}
cin >> Q;
while(Q--) {
cin >> str;
if(!mp.count(str))
cout << "No Info" << endl;
else {
cout << mp[str] << endl;
}
}
#else
//使用`字典树`
char str[32];
scanf("%d %d ", &n, &m);
int x;
for(int i=1; i<=n; i++) {
scanf("%s %d ", str, &x);
insert(str, x);
}
scanf("%d ", &Q);
while(Q--) {
scanf("%s ", str);
int ret = search(str);
if(~ret)
printf("%dn", ret);
else
printf("No Infon");
}
#endif
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
return 0;
}

最后

以上就是个性水杯为你收集整理的PTA 航空公司VIP客户查询 (25分) map或者字典树的全部内容,希望文章能够帮你解决PTA 航空公司VIP客户查询 (25分) map或者字典树所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部