golang map

Our friend Monk has been made teacher for the day today by his school professors . He is going to teach informatics to his colleagues as that is his favorite subject . Before entering the class, Monk realized that he does not remember the names of all his colleagues clearly . He thinks this will cause problems and will not allow him to teach the class well. However, Monk remembers the roll number of all his colleagues very well . Monk now wants you to help him out. He will initially give you a list indicating the name and roll number of all his colleagues. When he enters the class he will give you the roll number of any of his colleagues belonging to the class. You need to revert to him with the name of that colleague.

Input Format

The first line contains a single integers NN denoting the number of Monk‘s colleagues. Each of the next NN lines contains an integer and a String denoting the roll number and name of the ii th colleague of Monk. The next Line contains a single integer qq denoting the number of queries Monk shall present to you when he starts teaching in class. Each of the next qq lines contains a single integer xx denoting the roll number of the student whose name Monk wants to know.

Output Format

You need to print qq Strings, each String on a new line, indicating the answers to each of Monk‘s queries.

Constrains

1≤N≤1051≤N≤105

1≤RollNumber≤1091≤RollNumber≤109

1≤|Name|≤251≤|Name|≤25

1≤q≤1041≤q≤104

1≤x≤1091≤x≤109

Note

The name of each student shall consist of lowercase English alphabets only. It is guaranteed that the roll number appearing in each query shall belong to some student from the class.

其实就是用到了golang 数据集合map,貌似Map的性能不是太好

package main

import "fmt"

func main() {
	//fmt.Println("Hello World!")
	 mapDic := make(map[int]string)

	var mapCount int

	fmt.Scanln(&mapCount)
	var key int
	var value string
	for i:=0;i<mapCount;i++{
	    fmt.Scanln(&key, &value)
	    mapDic[key] = value
	}

	var searchCount int
    fmt.Scanln(&searchCount)
    var searchKey int
    for j:=0;j<searchCount;j++{
         fmt.Scanln(&searchKey)
         fmt.Println(mapDic[searchKey])
    }

}

  

时间: 2024-11-03 21:04:36

golang map的相关文章

golang map性能测试

golang map是golang的方便操作的key-value package main import ( "fmt" "math/rand" "time" _"strconv" ) var x = make(map[int]int, 100000001) //var x = make(map[string]string, 100000001) func main() { ttime := time.Now().UnixNa

golang map to struct

map映射到结构体,这里只支持简单的数据类型,复杂的需要在拓展 package main import ( "errors" "fmt" "reflect" "strconv" "time" ) type User struct { Name string Age int8 Date time.Time } func main() { data := make(map[string]interface{})

golang map并发读写异常导致服务崩溃

昨天突然接到报警说服务端口丢失,也就是服务崩溃了. 1, 先看错误日志,发现是调用json.Marshal时出错了,错误原因是:concurrent map iteration and map write,即并发读写map. fatal error: concurrent map iteration and map write goroutine 543965 [running]: runtime.throw(0xb74baf, 0x26) /usr/local/go1.10.1/src/run

深度解密Go语言之 map

目录 什么是 map 为什么要用 map map 的底层如何实现 map 内存模型 创建 map 哈希函数 key 定位过程 map 的两种 get 操作 如何进行扩容 map 的遍历 map 的赋值 map 的删除 map 进阶 可以边遍历边删除吗 key 可以是 float 型吗? 总结 参考资料 这篇文章主要讲 map 的赋值.删除.查询.扩容的具体执行过程,仍然是从底层的角度展开.结合源码,看完本文一定会彻底明白 map 底层原理. 我要说明的是,这里对 map 的基本用法涉及比较少,我

Google资深工程师深度讲解Go语言☆

准备工作 ######课程导读: Go语言的历史起源??? 为什么会有Go出现?? Go的发展趋势?? 怎么样学习Go?? ######安装与环境: 视频地址:安装与环境 ######Go的参考资料汇总:1.GO语言零基础入门资料整理2.Restful接口开发(PHP)3.Go入门指南4.Learngobyexample 巩固语法基础的Demo,初学者“抄”(学习)代码的地方5.Go 初学者成长之路6.Go 标准库中文文档7.<编程之法:面试和算法心得>8.目录: 第1章 课程介绍    欢迎

Golang,用map写个单词统计器

Golang中也有实用的泛型编程模板.如map.据Go官方团队称,其实现为Hash表,而非类似cpp或Java的红黑树.所以理论上速度更能快上几个等级(Hash与红黑树的效率对比可以看我的文章C++中各种<string,T>关联方式的速度对比,效率比约为3:1),但有一些区别,就是遍历时,数据是无需且随机的(当然,后文会讲到有序化的方法).接下来,我们先创建一个map对象. dict:=make(map[string]int); 由于map的强类型,所以一切类型是静态的,map也不例外.从ma

Golang 中使用多维 map

http://tnt.wicast.tk/2015/11/02/golang-multiple-dimension-map/ Golang 的 XML/JSON 解析库乍看使用起来很方便,只要构造一样结构的 Struct 就可以一下子导入到变量中去了.其实手工构造 Struct 非常容易出现结构偏差,而且最要命的是 Unmarshal() 执行的时候不是严格导入所以没有任何报错. 于是这两天写了一个给 Golang 用的 XML to Struct 生成器,希望能一劳永逸. 不过在制作过程中有遇

golang的内置类型map的一些事

golang的map类型是一个比较特殊的类型,不同于int, string, byte这样的基本类型,在经过一番探究之后得出了一些结论: 1.golang的map类型虽然是内置类型,但和基本类型有很大区别,更像是一个指针,因为他有着跟指针一样行为. 下面这段代码的第三次输出时,尝试了用通过arr[1].c["aaa"] = "lalala",这种方式去修改这个map元素,发现arr[0]同时被改变了.这也就说明了arr里面保存的其实是指向一块内存的指针,所以在通过a

【GoLang笔记】实例分析GoLang built-in数据结构map的赋值引用行为

备注1:本文旨在介绍Go语言中map这个内置数据结构的引用行为,并用实例来说明如何避免这种引用行为带来的"副作用". 备注2:文末列出的参考资料均来自GoLang.org官方文档,需翻墙访问. 1. map internals map是go中内置的数据结构,关于其语法规则,可以查看language specification中这里的说明,或者查看Effective Go中关于Maps的说明,此处略过. map的底层是用hashmap实现的(底层hashmap源码路径为src/pkg/r