[백준 14171번] Cities ans States
문제
To keep his cows intellectually stimulated, Farmer John has placed a large map of the USA on the wall of his barn. Since the cows spend many hours in the barn staring at this map, they start to notice several curious patterns. For example, the cities of Flint, MI and Miami, FL share a very special relationship: the first two letters of “Flint” give the state code (“FL”) for Miami, and the first two letters of “Miami” give the state code (“MI”) for Flint.
Let us say that two cities are a “special pair” if they satisfy this property and come from different states. The cows are wondering how many special pairs of cities exist. Please help them solve this amusing geographical puzzle!
city의 앞에서 2글자와 state가 서로 크로스가 되는 쌍이 special pair가 된다.
정답 코드
#include <iostream>
#include <map>
using namespace std;
void print(map<pair<string, string>, int> &m)
{
for (auto it : m)
{
cout << it.first.first << ": " << it.first.second << ", " << it.second << endl;
;
}
}
int main()
{
int n;
int cnt = 0;
cin >> n;
map<pair<string, string>, int> city_list;
for (int i = 0; i < n; i++)
{
string city, special;
cin >> city >> special;
city = city.substr(0, 2);
if (special == city)
{
continue;
}
city_list[{special, city}]++;
// city, special 쌍이 cnt += 을했을 때, count가 된다는 것은 이미 그 전에 값이 있었다는 의미이다.
cnt += city_list[{city, special}];
cout << "cnt: " << cnt << endl;
print(city_list);
}
cout << "cnt: " << cnt << endl;
}
>>>
6
MIAMI FL
cnt: 0
FL: MI, 1
MI: FL, 0
DALLAS TX
cnt: 0
DA: TX, 0
FL: MI, 1
MI: FL, 0
TX: DA, 1
FLINT MI
cnt: 1
DA: TX, 0
FL: MI, 1
MI: FL, 1
TX: DA, 1
CLEMSON SC
cnt: 1
CL: SC, 0
DA: TX, 0
FL: MI, 1
MI: FL, 1
SC: CL, 1
TX: DA, 1
BOSTON MA
cnt: 1
BO: MA, 0
CL: SC, 0
DA: TX, 0
FL: MI, 1
MA: BO, 1
MI: FL, 1
SC: CL, 1
TX: DA, 1
ORLANDO FL
cnt: 1
BO: MA, 0
CL: SC, 0
DA: TX, 0
FL: MI, 1
FL: OR, 1
MA: BO, 1
MI: FL, 1
OR: FL, 0
SC: CL, 1
TX: DA, 1
cnt: 1
출력해보면 이런 느낌?
댓글남기기