描述
Long long ago you took a crazy trip around the world. You can not remember which cities did you start and finish the trip. Luckily you have all the boarding passes. Can you find out the starting city and ending city?
输入
The first line contains a number N denoting the number of boarding passes. (1 <= N <= 100)
The following N lines each contain a pair of cities (the city name consists of no more than 10 capital letters)
It is guaranteed that there is always a valid solution.
输出
The starting and ending cities separated by a space.
样例输入
4
SFO HKG
PVG BOS
HKG ABC
ABC PVG
样例输出
SFO BOS
题解:字符串基础操作
#include<bits/stdc++.h>
using namespace std;
map<string,int>line1,line2;
int t;
string sr[205],ans1,ans2;
int main()
{
scanf("%d",&t);
for (int i=0;i<2*t;i+=2){
cin>>sr[i]>>sr[i+1];//不能用scanf来输入string
line1[sr[i]]++;
line2[sr[i+1]]++;
}
for (int i=0;i<2*t;i++){
if (line2[sr[i]]-line1[sr[i]]==1)
ans1=sr[i];
if (line1[sr[i]]-line2[sr[i]]==1)
ans2=sr[i];
}
cout<<ans2<<" "<<ans1<<endl;
return 0;
}