For 23 Jan Silver Qn1, the solution is in Java and Python. Does anyone have a C++ solution? Thanks in advance
Here is the analysis code converted to C++ (courtesy of ChatGPT):
#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
int letterToNode(char letter) {
if ('a' <= letter && letter <= 'z') {
return letter - 'a';
} else {
return 26 + (letter - 'A');
}
}
int main() {
int t;
cin >> t;
while (t--) {
string before, after;
cin >> before >> after;
vector<int> becomes(52, -1);
bool possible = true;
unordered_set<char> set;
for (int j = 0; j < before.length(); j++) {
int b = letterToNode(before[j]);
int a = letterToNode(after[j]);
set.insert(after[j]);
if (becomes[b] != -1 && becomes[b] != a) { possible = false; }
becomes[b] = a;
}
if (set.size() == 52) { possible = false; }
if (before == after) { possible = true; }
int answer = 0;
if (possible) {
vector<int> inDegree(52);
for (int a = 0; a < 52; a++) {
if (becomes[a] != -1 && becomes[a] != a) {
inDegree[becomes[a]]++;
}
}
for (int a = 0; a < 52; a++) {
if (becomes[a] != -1 && becomes[a] != a) { answer++; }
}
vector<int> seen(52);
for (int r = 0; r < 52; r++) {
if (seen[r] == 0) {
int a = r;
while (a != -1 && seen[a] == 0) {
seen[a] = r + 1;
a = becomes[a];
}
if (a != -1 && a != becomes[a] && seen[a] == r + 1) {
int s = a;
bool freePass = false;
do {
seen[a] = 2;
if (inDegree[a] > 1) { freePass = true; }
a = becomes[a];
} while (a != s);
if (!freePass) { answer++; }
}
}
}
} else {
answer = -1;
}
cout << answer << endl;
}
return 0;
}
1 Like
omg tyyyy