c++ stl-map改名字题解

Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.

Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.

Input

The first line contains integer q (1 ≤ q ≤ 1000), the number of handle
change requests.

Next q lines contain the descriptions of the requests, one per line.

Each query consists of two non-empty strings old and new, separated by
a space. The strings consist of lowercase and uppercase Latin letters
and digits. Strings old and new are distinct. The lengths of the
strings do not exceed 20.

The requests are given chronologically. In other words, by the moment
of a query there is a single person with handle old, and handle new is
not used and has not been used by anyone.

Output

In the first line output the integer n — the number of users that
changed their handles at least once.

In the next n lines print the mapping between the old and the new
handles of the users. Each of them must contain two strings, old and
new, separated by a space, meaning that before the user had handle
old, and after all the requests are completed, his handle is new. You
may output lines in any order.

Each user who changes the handle must occur exactly once in this
description.

Examples
Input

5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov

Output

3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

归纳一下就是改名字,最后要求输出最初始的名字和最末尾名字配对输出

老师给的例题中有这个,但还是决定写一个题解,给自己理理思路吧
先说说自己的经过,有点惨,开始上课老师讲的这个题,当时沉迷在老师的出色讲课(误),事后自己跑发现跑不出来?!!就开始无限的死循环,卡了好几个小时,果然。。菜是原罪。思路是map为主这个是没问题的,但是map只能对value下手,而无法改变key值,开始我自己的想法是定义一个for循环借助迭代器来确定,然后立马否定了,复杂度太高,如果数据过大肯定要出事,然后就又打算用两个map来进行初始名字和末尾名字的配对,但是老师的例题答案的一个map实现全部操作还是令我很向往,但无奈结果跑不出来,就陷入了这个死循环。。
老师例题给的代码其实是中循环的第一个赋值出了问题(还是问别人问出来的,自己找到自闭),下面上正确代码

#include <iostream>
#include <cstdio>
#include <map>
#include <string>
using namespace std;

int main()
{
	int n;
	cin>>n;
    string s1,s2;
    map<string,string> handle;
    for(int i=0;i<n;i++){
        cin>>s1>>s2;
        if (!handle.count(s1))
            handle[s1]=s1;
        handle[s2]=handle[s1];
        handle.erase(s1);
    }
    map<string,string>::iterator it;
    cout << handle.size() << endl;
    for (it=handle.begin();it!=handle.end();it++){
        cout << it->second << " " << it->first << endl;
	}
	return 0;
}

核心的玩意就是

 if (!handle.count(s1))
                handle[s1]=s1;
            handle[s2]=handle[s1];
            handle.erase(s1);

这点玩意,理解其实自己肯动动笔就会好很多,比如借助一下给的范例

5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov

然后一点点的去理解,写一写,就是

m[Misha]=Misha;
m[ILovecodeforces]=Mish;
m[Misha]=
m[Vasya]=Vasya
m[Petrov]=Vasya
m[Vasya]=
m[VasyaPatrov]=Vasya
m[Petrov]=
m[

这个时候就很好理解了,一个小知识点,map如果被erase了value,计算size等玩意的时候就不带他了。

总结一下教训就是总觉得自己听懂了,课后没有马上实例演算,结果拖了一天直接把自己拖死了

猜你喜欢

转载自blog.csdn.net/qq_43141958/article/details/88206239
今日推荐