J - Misha and Changing Handles

又是一个思维题,正着不好吗、反着来

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

改名字,n次修改记录,输出是

好几个下面的:

老名字  新名字

#include<bits/stdc++.h> 
#define N 10
#define endl '\n' 
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;  
map<string,string> Map; 
set<string> Set; 
int main(){    
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);    
    int n; cin>>n;  
    while(n--){
        string a,b; int cnt=0;
        cin>>a>>b;
        if(Set.count(a)){
            Set.erase(a),Set.insert(b);
            Map[b]=Map[a];
        }
        else{ 
            Set.insert(b);
            Map[b]=a;
        }
    }
    cout<< Set.size() <<endl;
    for(auto New : Set){
        cout<<Map[New]<<' '<<New<<endl;
    }
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/SunChuangYu/p/12381313.html
今日推荐