zoukankan      html  css  js  c++  java
  • CF 501B Misha and Changing Handles (map & set)

    http://codeforces.com/problemset/problem/501/B

    https://vjudge.net/problem/CodeForces-501B#author=fishdog

    ^▽^神秘的三角洲里还有一个传说中的谜题等你来解开!
    三角洲里的小学生小明是个小天才,知天文晓地理还能夜观星象算命,好多疯狂的小朋友都想去请他给自己换个好听的名字。但是天才小明,他总是在思考31维宇宙空间的奥秘,神游天外,所以在给小朋友们敲他们想要的名字的时候,偶尔会取出一些不那么完美的名字。有的小朋友们换了名字以后不太满意,于是它们又会顶着改好的新名字来找小明。
    可怜的小明,他沉迷宇宙的奥秘,有的小朋友名字换的次数太多,他已经快要记不清他们初始的名字和最后的名字是什么了,再这样下去,小朋友们会不满意,他取名字的业务就要滞销了!!!请你帮他整理一下每个小朋友的初始名字和最后敲定的名字。

    Input

    第一行表示有q个请小明帮忙改名字的请求。1<=q<=1000.
    接下来q行,每行有两个字符串,代表这次来的小朋友现在的名字,和小明这回给它起的名字,这两个字符串由1个空格分开,字符串有大小写字母和数字,长度不超过20.

    Output

    输出一个n,代表来找过小明修改名字的人数。
    接下来n行,每行一个人的最初始的名字,再输出小明最终给它起的名字。 (人的顺序随意)

    Examples

    Input
    5
    Misha ILoveCodeforces
    Vasya Petrov
    Petrov VasyaPetrov123
    ILoveCodeforces MikeMirzayanov
    Petya Ivanov
    Output
    3
    Petya Ivanov
    Misha MikeMirzayanov
    Vasya VasyaPetrov123
     
    z这道题是map的应用。这个根据键值搜索实在是太好用了。这里有两种算法。一种是利用set查重,并以更改的名字作为键值,每次更新。另一种是遍历查找,以未更改的名字作键值效率较前一种较低。
    第一种题解:
     1 #include <bits/stdc++.h>
     2 
     3 #define N 100010
     4 #define maxn 200010
     5 
     6 using namespace std;
     7 
     8 typedef long long int ll;
     9 
    10 int main()
    11 {
    12     set<string> temp;
    13     map<string, string> a;
    14 //    map<string, string>::iterator ite;
    15     int n;
    16     cin>>n;
    17     for(int i=0; i<n; i++){
    18         string s1, s2;
    19         cin>>s1>>s2;
    20         if(temp.find(s1)!=temp.end()){
    21             a[s2]=a[s1];    //保留原名字
    22             a.erase(s1);  //清除一下原来的对应,其实不清也没问题
    23             temp.erase(s1);  //这里就必须清除一下了
    24             temp.insert(s2);
    25         }
    26         else{
    27             a[s2]=s1;
    28             temp.insert(s2);
    29         }
    30     }
    31     cout<<temp.size()<<'
    ';
    32     set<string>::iterator ite;
    33     for(ite=temp.begin(); ite!=temp.end(); ite++){
    34         cout<<a[*ite]<<' '<<*ite<<'
    ';
    35     }
    36     return 0;
    37 }

    第二种:

     1 #include <cmath>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <climits>
     6 #include <map>
     7 #include <set>
     8 #include <queue>
     9 #include <stack>
    10 #include <vector>
    11 #include <string>   //
    12 #include <iostream>
    13 #include <algorithm>
    14 //#include <bits/stdc++.h>
    15 
    16 #define N 100010
    17 #define maxn 200010
    18 
    19 using namespace std;
    20 
    21 typedef long long int ll;
    22 
    23 int main()
    24 {
    25     map<string, string> a;
    26     map<string, string>::iterator ite;
    27     int n;
    28     cin>>n;
    29     int cnt=0;
    30     string s1, s2;
    31     for(int i=0; i<n; i++) {
    32         cin>>s1>>s2;
    33         bool flag=false;
    34         for(ite=a.begin(); ite!=a.end(); ite++){  //遍历map中的元素查找是否记录过
    35             if(ite->second==s1){
    36                 flag=true;
    37                 break;
    38             }
    39         }
    40         if(flag){
    41             ite->second=s2;
    42         }
    43         else{
    44             a[s1]=s2; cnt++;
    45         }
    46     }
    47     cout<<cnt<<'
    ';
    48     for(ite=a.begin(); ite!=a.end(); ite++){
    49         cout<<ite->first<<' '<<ite->second<<'
    ';
    50     }
    51     return 0;
    52 }

    顺序不弄错就没什么大问题。



  • 相关阅读:
    jmeter实现multipart/form-data类型请求
    jmeter(正则提取器、json提取器)做接口关联
    windows使用ubuntu启动linux服务
    Jmeter+Ant+Jenkins环境搭建
    ubuntu网卡驱动的安装
    Ubuntu 挂载U盘
    Linux下安装jdk8步骤详述(转载)
    springboot项目:项目部署
    springboot项目:Redis缓存使用
    springboot项目:Redis分布式锁的使用(模拟秒杀系统)
  • 原文地址:https://www.cnblogs.com/Arrokoth/p/12213244.html
Copyright © 2011-2022 走看看