HHFHHHF Notes

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include <iostream>

#include <algorithm>
using namespace std;

bool safe(string s) {
string c = "codechef";
for (int i = 0; i < 8; i++) {
if (s[i] == c[i]) {
return false;
}
}
return true;
}

int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;

if (!safe(s)) {
// Sort the characters of s to get the smallest possible permutation.
sort(s.begin(), s.end());

int cnt = 0;
// Find the first safe permutation of s, breaking after 40320
iterations.
while (next_permutation(s.begin(), s.end()) && cnt < 40320) {
if (safe(s)) {
break;
}
cnt++;
}

if (cnt >= 40320) {


cout << "-1" << endl;
continue; // Move on to the next test case.
}
}

cout << s << endl;


}
return 0;
}

You might also like