Hey whoever is reading this Topic,
Can you please help me with “Cities and States”, Silver December 2016 Problem 2?
I have written my code, and to me, it seems perfectly fine, but it did not completely pass the test cases. Could you post anything errors that you found in my code below?
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
bool exist[2626][2626];
string cit[200000], sta[200000];
int num(string s) {
int a = (s[0]-65)*100, b = (s[1]-65);
return a+b;
}
int main()
{
long long N, pairs = 0; cin >> N;
for (int n = 0; n < N; n++) {
cin >> cit[n] >> sta[n];
exist[num(cit[n])][num(sta[n])] = true;
}
for (int i = 0; i < N; i++) {
if (num(cit[i]) != num(sta[i])) {
if (exist[num(cit[i])][num(sta[i])]) {
if (exist[num(sta[i])][num(cit[i])]) {
pairs++;
exist[num(cit[i])][num(sta[i])] = false;
exist[num(sta[i])][num(cit[i])] = false;
}
}
}
} cout << pairs << endl;
}
If you did not fully understand what my code did there, I created a sort of boolean map from a 2D array, called “exists”.
exists[num(city_name)][num(state_name)]
is whether or not there exists a city called “city_name” that is in the state, “state_name”.
Thanks in advance!
Yours,
peppersniffer