codeforces 501 B Misha and Changing Handles 【map】
题意:给出n个名字变化,问一个名字最后变成了什么名字
先用map顺着做的,后来不对, 发现别人是将变化后的那个名字当成键值来做的,最后输出的时候先输出second,再输出first
写一下样例就好理解了
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<algorithm> 11 using namespace std; 12 13 typedef long long LL; 14 const int INF = (1<<30)-1; 15 const int mod=1000000007; 16 const int maxn=100005; 17 18 string a,b; 19 20 int main(){ 21 // freopen("in.txt","r",stdin); 22 // freopen("out.txt","w",stdout); 23 int n; 24 map<string,string> mp; 25 cin>>n; 26 while(n--){ 27 cin>>a>>b; 28 if(!mp.count(a)) mp[a]=a; 29 mp[b]=mp[a]; 30 mp.erase(a); 31 } 32 33 cout<<mp.size()<<"\n"; 34 for(map<string,string>::iterator it=mp.begin();it!=mp.end();++it) 35 cout<<it->second<<" "<<it->first<<"\n"; 36 return 0; 37 }
唉,这么久才补,真是太懒了-------
加油啊-
goooooooo-------------------------------------------