CodeForces 501B - Misha and Changing Handles

时间:2022-06-06 10:41:20

有N个改名的动作,输出改完名的最终结果。

拿map做映射

 #include <iostream>
#include <map>
#include <string>
using namespace std;
map<string,string> mp;
map<string,int> vis;
int n;
string s1,s2;
int main()
{
cin>>n;
map<string,string>::iterator it;
for(int i=;i<=n;i++)
{
cin>>s1>>s2;
if(!vis[s1]&&!vis[s2])
{
vis[s1]=; vis[s2]=; mp[s1]=s2;
}
else if(vis[s1]&&!vis[s2])
{
vis[s2]=;
for(it=mp.begin();it!=mp.end();it++)
{
if(it->second==s1)
{
it->second=s2; break;
}
}
}
}
cout<<mp.size()<<endl;
for(it=mp.begin();it!=mp.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
}