CF94A Restoring Password-题解
看见又没有人用 map ,我又来水一波
map 的基本用法
map 相当于一个下标可以是任何类型的数组。
-
#include <map>map 头文件。 -
map <A,B> ds建立一个叫ds的,下标类型为A,元素类型为B的映射表。 -
ds[t1]=t2把下标为t1对应的值改为t2。 -
ds[A]访问下标为A的元素。那么,如何做本题呢?
用
map存储1-9 每个数字对应的密码,例如题中样例#2:
| 数字 | 对应的密码 |
|---|---|
1001000010 |
|
1101111001 |
|
1001000110 |
|
1010110111 |
|
0010110111 |
|
1101001101 |
|
1011000001 |
|
1110010101 |
|
1011011000 |
|
0110001000 |
在拆分密码串即可,拆分密码串使用 string 的 .substr(pos,len) 函数实现。用法:s.substr(pos,len):截取字符串
有了这些知识,轻松得出代码:
Code:
#include <iostream>
#include <map>
#include <string>
using namespace std;
map <string,int> ds;
int main(){
string s,a;
cin>>s;
for(int i=0;i<10;i++){
cin>>a;
ds[a]=i;//存储密码
}
for(int i=0;i<80;i+=10){
string tmp=s.substr(i,10);//挨个截取字符串
cout<<ds[tmp];
}
return 0;
}
结语
使用 map 可以轻松解决多种问题,对于其他题目,也要从多个角度思考。
希望本文可以让你掌握 map !