题意:

思路:

读完题目之后的第一思路就是用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 分)的更多相关文章

  1. 5-45 航空公司VIP客户查询 (25分) HASH

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

  2. 12. The Biggest Safety Threat Facing Airlines 航空公司面临的最大安全威胁

    12. The Biggest Safety Threat Facing Airlines 航空公司面临的最大安全威胁 (1) The biggest safety threat facing air ...

  3. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  4. L2-001 紧急救援 (25 分)

    L2-001 紧急救援 (25 分)   作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...

  5. 1125 Chain the Ropes (25 分)

    1125 Chain the Ropes (25 分) Given some segments of rope, you are supposed to chain them into one rop ...

  6. 1122 Hamiltonian Cycle (25 分)

    1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...

  7. 1113 Integer Set Partition (25 分)

    1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...

  8. 1044 Shopping in Mars (25 分)

    1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay b ...

  9. 1012 The Best Rank (25 分)

    1012 The Best Rank (25 分) To evaluate the performance of our first year CS majored students, we cons ...

随机推荐

  1. 【转】MySQL随机字符串生成

    DROP FUNCTION IF EXISTS rand_string; DELIMITER $$ CREATE FUNCTION rand_string(str_length TINYINT UNS ...

  2. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  3. wordpress 配置(ubuntu)---修改 linux hostname

    使用阿里云服务器的 ubuntu 系统时的 hostname 太扭曲,而且有些命令会受 hostname 的影响不能正常使用,所以,一定要改掉它! 永久修改 hostname: 使用 nano 命令: ...

  4. 为什么使用 use strict

    进入标志: "use script"; 使用方式: 可以在 js 代码的第一行,也可以使用在函数中.但是写在 js 代码第一行不利于文件合并,因此可以写在一个自调用函数的第一行 使 ...

  5. to prof. Choi

    Dear Prof. Choi It is my great pleasure to receive your reply ,but terribly sorry for my late reply ...

  6. LCT教程

    lct是一种动态树,用来维护一些动态加边删边的操作的东西.他主要用到几个操作,其实这个算法和树链刨分有点像,但是不能用线段树简单维护,所以我们要用多棵平衡树来维护树上的一个个子树,然后就进行一些很秀的 ...

  7. PCB Genesis 外形加内角孔实现方法

    在PCB工程制作CAM时,经常会遇到外形拐角处直角的,而客户对内角是要求,比如最大内角要求R0.5mm或者不接受内角, 但成型方式为铣方式,又不是啤板成型,那怎么处理才可以达到要求效果呢,在这里介绍2 ...

  8. cmd执行Java程序

    先创建一个文本,里面内容为 public class hello{ public static void main(String[] arg) { System.out.println("H ...

  9. linux 查看 cpu

    如何获得CPU的详细信息: linux命令:cat /proc/cpuinfo 用命令判断几个物理CPU,几个核等: 逻辑CPU个数:# cat /proc/cpuinfo | grep " ...

  10. JavaScript--String 字符串对象属性

    访问字符串对象的属性length: stringObject.length; 返回该字符串的长度. var mystr="Hello World!"; var myl=mystr. ...