CodeForces 501B - Misha and Changing Handles
有N个改名的动作,输出改完名的最终结果。
拿map做映射
1 #include <iostream> 2 #include <map> 3 #include <string> 4 using namespace std; 5 map<string,string> mp; 6 map<string,int> vis; 7 int n; 8 string s1,s2; 9 int main() 10 { 11 cin>>n; 12 map<string,string>::iterator it; 13 for(int i=1;i<=n;i++) 14 { 15 cin>>s1>>s2; 16 if(!vis[s1]&&!vis[s2]) 17 { 18 vis[s1]=1; vis[s2]=1; mp[s1]=s2; 19 } 20 else if(vis[s1]&&!vis[s2]) 21 { 22 vis[s2]=1; 23 for(it=mp.begin();it!=mp.end();it++) 24 { 25 if(it->second==s1) 26 { 27 it->second=s2; break; 28 } 29 } 30 } 31 } 32 cout<<mp.size()<<endl; 33 for(it=mp.begin();it!=mp.end();it++) 34 { 35 cout<<it->first<<" "<<it->second<<endl; 36 } 37 }
我自倾杯,君且随意