题目
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
Input
First line of input contains two names (length of each of them doesn’t exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn’t exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
Output
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
Examples
input
ross rachel
4
ross joey
rachel phoebe
phoebe monica
monica chandler
output
ross rachel
joey rachel
joey phoebe
joey monica
joey chandler
input
icm codeforces
1
codeforces technex
output
icm codeforces
icm technex
代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
string str;
string str1;
string str2;
string str3;
cin >> str >> str1;
int day = 0;
cin >> day;
cout << str << " " << str1 << endl;
for(day;day > 0;day--)
{
cin >> str2 >> str3;
if(str2 == str)
{
str = str3;
}
if(str2 == str1)
{
str1 = str3;
}
cout << str << " " << str1 << endl;
}
return 0;
}
理解
这道题说的是,一个杀手每天要在两个人中杀掉一个,然后先输入第一天的两个人,再输入天数和这一天杀掉的人和一个顶替他的人,让你输出每天活下来的人。
这里,我们就用string来存储,每天来进行判断,把杀掉的人名用新的覆盖,然后输出每天活下来的就可以了。