首页 > 技术文章 > codeforces 831B. Keyboard Layouts 解题报告

windysai 2017-07-22 12:13 原文

题目链接:http://codeforces.com/contest/831/problem/B

题目意思:给出两个长度为26,由小写字母组成的字符串s1和s2,对于给出的第三个字符串s3,写出对应s1中字符在s2中的映射。如果是大写字母,最终输出也是大写字母;如果是数字,保留不做任何更改

题目解析:这里主要用了map<int, int> 来记录s1中的key,value 值,key是字母经处理后的对应数字形式,value是字母对应的位置

  调试得比较久,用 map用得少,不熟悉哇~~   >_< 

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <ctype.h>
 7 #include <map>
 8 using namespace std;
 9 
10 map<int, int> my_map;
11 const int maxn = 26 + 2;
12 const int N = 1000 + 2;
13 const int len = 26;
14 char s1[maxn], s2[maxn];
15 char s3[N];
16 
17 int main(){
18     #ifndef ONLINE_JUDGE
19        freopen("in.txt", "r", stdin);
20     #endif // ONLINE_JUDGE
21 
22     while(scanf("%s%s%s", s1, s2, s3) != EOF) {
23        my_map.clear();
24 
25        for (int i = 0; i < len; i++) {
26            my_map[s1[i]-'a'] = i;
27        }
28 
29        int len_s3 = strlen(s3);
30        for (int i = 0; i < len_s3; i++) {
31             if (isupper(s3[i])) {
32                 char up = char(s2[my_map[s3[i]-'A']]);
33                 cout << char(up-'a'+'A');
34 
35             }
36             else if (islower(s3[i])){
37                 cout << char(s2[my_map[s3[i]-'a']]);
38             }
39             else
40                 cout << s3[i];
41        }
42        puts("");
43     }
44     return 0;
45 }

 

附带一个别人写的,用map<char, char>  ,短小精悍,值得学习~~~ ^____^

 

推荐阅读