Professional Documents
Culture Documents
HHFHHHF Notes
HHFHHHF Notes
HHFHHHF Notes
#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++;
}