#include <iostream>
#include <vector>
#include <string>
using namespace std;
//multimea de caractere
const vector<char> X = {'a','b','c','1','2','3'};
bool suntAlaturateValidare(char c1, char c2) {
return (c1 == '1' && (c2 == '2' || c2 == '3')) ||
(c1 == '2' && (c2 == '1' || c2 == '3')) ||
(c1 == '3' && (c2 == '1' || c2 == '2'));
}
void genereazaCuvinte(vector<char>&cuvant, int pozitie, vector<bool> &folosit, vector<string>& rezultate) {
if(pozitie == X.size()) {
//a1c2b3
//cuvant[0] = 'a'
//cuvant[1] = '1'
//cuvant[2] = 'c'
//cuvant[3] = '2'
//cuvant[4] = 'b'
//cuvant[5] = '3'
rezultate.push_back(string( cuvant.begin(), cuvant.end()));
return;
}
for(int i = 0; i < X.size(); ++i) {
if(!folosit[i] && !suntAlaturateValidare(cuvant[ pozitie-1 ], X[ i ] )) {
cuvant[pozitie] = X[i];
folosit[i] = true;
genereazaCuvinte(cuvant, pozitie + 1, folosit, rezultate);
folosit[i] = false;
}
}
}
int main(int argc, char const *argv[]) {
//vectorul pentru a stoca cuvantul generat
vector<char> cuvant(6);
//vector pentru a marca daca un caracter este folosit
vector<bool> folosit(X.size(), false);
vector<string> rezultate;
genereazaCuvinte(cuvant, 0, folosit, rezultate);
cout<<"URmatoarea permutate(cuvant)" << rezultate[0]<<endl;
cout<<"URmatoarea permutate(cuvant)" << rezultate[1]<<endl;
cout<<"URmatoarea permutate(cuvant)" << rezultate[2]<<endl;
cout<<"URmatoarea permutate(cuvant)" << rezultate[3]<<endl;
cout<<"URmatoarea permutate(cuvant)" << rezultate[4]<<endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RyaW5nPgogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKLy9tdWx0aW1lYSBkZSBjYXJhY3RlcmUKY29uc3QgdmVjdG9yPGNoYXI+IFggPSB7J2EnLCdiJywnYycsJzEnLCcyJywnMyd9OwogCmJvb2wgc3VudEFsYXR1cmF0ZVZhbGlkYXJlKGNoYXIgYzEsIGNoYXIgYzIpIHsKIAogICAgIHJldHVybiAoYzEgPT0gJzEnICYmIChjMiA9PSAnMicgfHwgYzIgPT0gJzMnKSkgfHwKICAgICAgICAgICAgKGMxID09ICcyJyAmJiAoYzIgPT0gJzEnIHx8IGMyID09ICczJykpIHx8CiAgICAgICAgICAgIChjMSA9PSAnMycgJiYgKGMyID09ICcxJyB8fCBjMiA9PSAnMicpKTsKfQogCiAKdm9pZCBnZW5lcmVhemFDdXZpbnRlKHZlY3RvcjxjaGFyPiZjdXZhbnQsIGludCBwb3ppdGllLCB2ZWN0b3I8Ym9vbD4gJmZvbG9zaXQsIHZlY3RvcjxzdHJpbmc+JiByZXp1bHRhdGUpIHsKIAogICAgICAgICAgICAgIGlmKHBveml0aWUgPT0gWC5zaXplKCkpIHsKIAogICAgICAgICAgICAgICAgLy9hMWMyYjMKICAgICAgICAgICAgICAgIC8vY3V2YW50WzBdID0gJ2EnCiAgICAgICAgICAgICAgICAvL2N1dmFudFsxXSA9ICcxJwogICAgICAgICAgICAgICAgLy9jdXZhbnRbMl0gPSAnYycKICAgICAgICAgICAgICAgIC8vY3V2YW50WzNdID0gJzInCiAgICAgICAgICAgICAgICAvL2N1dmFudFs0XSA9ICdiJwogICAgICAgICAgICAgICAgLy9jdXZhbnRbNV0gPSAnMycKIAogICAgICAgICAgICAgICAgICAgIHJlenVsdGF0ZS5wdXNoX2JhY2soc3RyaW5nKCBjdXZhbnQuYmVnaW4oKSwgY3V2YW50LmVuZCgpKSk7CiAKICAgICAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgICAgfQogCiAgICAgICAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IFguc2l6ZSgpOyArK2kpIHsKIAogICAgICAgICAgICAgICAgICAgICAgIGlmKCFmb2xvc2l0W2ldICYmICFzdW50QWxhdHVyYXRlVmFsaWRhcmUoY3V2YW50WyBwb3ppdGllLTEgXSwgWFsgaS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