7-13 航空公司VIP客户查询 (25 分)
题意:

思路:
读完题目之后的第一思路就是用map将客户的id(string类型)与里程road(int类型)形成映射,然后直接用id查找添加里程或输出里程。但是400ms的限制妥妥的超时了。然后意识到要用哈希做,但是用哈希就有一点不好解决,每个客户的里程怎么保存,考虑了很长时间无果,搜了一下博客,发现可以用结构体来保存,平常用数组模拟链表的时候都是直接开的一维数组,所以当每个客户的信息多了后就没有结构体来的理解方便了。
第一次尝试:这个题N的上限是1e5,所以将每个客户的id转换成long long类型,然后对1e5+7取余,结果作为下标对应到数组里边存里程,完美过样例,提交果断WA。。。。。。。
第二次尝试:又看了一遍题目,思考了一下,哈希会出现冲突,需要处理冲突。所以数组模拟链表处理冲突,提交AC。
代码:
#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin) using namespace std;
typedef long long ll;
typedef pair<int,string> P;
const int maxn = 1e5+;
int head[maxn];
struct Peo{
char name[];
int road;
int next;
}p[maxn];
int cnt = ; int GetId(char* str) {
ll res = ;
for(int i = ; i<strlen(str); i++) {
if(isdigit(str[i])) {
res = res* + str[i]-'';
} else {
res = res* + ;
}
}
return (int)(res%maxn);
} void Add_Node(char* str,int id,int temp){
bool ok = true;
for(int i = head[id]; ~i; i = p[i].next){
if(strcmp(str,p[i].name) == ){
p[i].road += temp;
ok = false;
}
}
if(ok){
int i = ;
p[cnt].road += temp;
for(i = ; str[i]; i++){
p[cnt].name[i] = str[i];
}
p[cnt].name[i] = '\0';
p[cnt].next = head[id];
head[id] = cnt++;
}
return;
} bool ToFind(char* str,int id){
for(int i = head[id]; i!=-; i = p[i].next){
if(strcmp(str, p[i].name) == ){
cout<<p[i].road<<endl;
return true;
}
}
return false;
} int main() {
char str[];
int n,k,temp;
cin>>n>>k;
memset(head,-,sizeof(head));
for(int i = ; i<n; i++) {
cin>>str>>temp;
if(temp<k) temp = k;
int index = GetId(str);
Add_Node(str,index,temp);
}
int m;
cin>>m;
for(int i = ; i<m; i++) {
cin>>str;
int index = GetId(str);
if(!ToFind(str,index)){
printf("No Info\n");
}
}
return ;
}
7-13 航空公司VIP客户查询 (25 分)的更多相关文章
- 5-45 航空公司VIP客户查询 (25分) HASH
不少航空公司都会提供优惠的会员服务,当某顾客飞行里程累积达到一定数量后,可以使用里程积分直接兑换奖励机票或奖励升舱等服务.现给定某航空公司全体会员的飞行记录,要求实现根据身份证号码快速查询会员里程积分 ...
- 12. The Biggest Safety Threat Facing Airlines 航空公司面临的最大安全威胁
12. The Biggest Safety Threat Facing Airlines 航空公司面临的最大安全威胁 (1) The biggest safety threat facing air ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- L2-001 紧急救援 (25 分)
L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...
- 1125 Chain the Ropes (25 分)
1125 Chain the Ropes (25 分) Given some segments of rope, you are supposed to chain them into one rop ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- 1044 Shopping in Mars (25 分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay b ...
- 1012 The Best Rank (25 分)
1012 The Best Rank (25 分) To evaluate the performance of our first year CS majored students, we cons ...
随机推荐
- C#邮件发送(含附件)
class SendEmail { static void Main(string[] args) { string from = "发件人@yingu.com"; string ...
- Nginx的alias与root的用法区别和location匹配规则
1.alias与root的用法区别 最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录. location /abc/ { ...
- Tornado异步模式
先介绍下背景:由于工作需要,前段时间又写了一段爬虫去获取和更新一些数据.之前爬虫主要用Scrapy框架批量爬取一些页面数据,或者用Gevent调用目标站点的接口.偶然看到了Tornado,听说这个框架 ...
- iis 服务器而配置php运行环境
第一步 下载php 下载压缩包就可以了 第二步 解压缩php到某个目录,比如D:\php php目录里面有两个php.ini,一个是php.ini-dist,比较适合开发用:一个是php.ini-re ...
- .Net application,Session,Cache简单比较
Application 对象用于存储和访问来自任何页面的变量,类似于 session 对象.不同之处在于,所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对 ...
- HDU3949 XOR(线性基第k小)
Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base num ...
- Android彻底组件化demo发布
今年6月份开始,我开始负责对"得到app"的android代码进行组件化拆分,在动手之前我查阅了很多组件化或者模块化的文章,虽然有一些收获,但是很少有文章能够给出一个整体且有效的方 ...
- Android sensor 系统框架 (一)
这几天深入学习了Android sensor框架,以此博客记录和分享分析过程,其中难免会有错误的地方,欢迎指出! 这里主要分析KERNEL->HAL->JNI这3层的流程.主要从以下几方面 ...
- Android项目实战_手机安全卫士手机防盗界面
#安全卫士手机防盗# ###1.Activity的任务栈 1.类似一个木桶,每层只能放一个木块,我们放入木块和取出木块的时候只能从最上面开始操作 ###2.Android中的坐标系![](http:/ ...
- Storm 入门一:基本知识+网上资源链接
Strom首页:http://storm.apache.org/ 下载地址:http://www.apache.org/dyn/closer.lua/storm/apache-storm-1.1.0/ ...