Pagini recente » Cod sursa (job #1781105) | Cod sursa (job #1454526) | Cod sursa (job #1358861) | Cod sursa (job #1469390) | Cod sursa (job #2567006)
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <set>
using namespace std;
ifstream fin("perle.in");
ofstream fout("perle.out");
const vector<string> transf[3] = {{"1","2","3"},{"2B","1A3AC"},{"2","3BC","12A"}};
int n, l;
bool posibil(string s, string cmp) {
if(s.length() > cmp.length()) return false;
if(s == cmp) return true;
cout << s << ' ';
int index = 0;
while(s[index] == '1' || s[index] == '2' || s[index] == '3' && index < s.length()) {
if(s[index] != cmp[index])
return false;
index++;
}
s = s.substr(index, s.length()-index);
cmp = cmp.substr(index, cmp.length()-index);
cout << s << ' ';
index = 1;
while(s[s.length()-index] == '1' || s[s.length()-index] == '2' || s[s.length()-index] == '3' && index <= s.length()) {
if(s[s.length()-index] != cmp[cmp.length()-index])
return false;
index++;
}
s = s.substr(0, s.length()-index+1);
cmp = cmp.substr(0, cmp.length()-index+1);
//cout << s << '\n';
for(int i = 0; i < s.length(); i++)
if(s[i] == 'A' || s[i] == 'B' || s[i] == 'C')
for(auto x:transf[s[i]-'A']) {
if(posibil(s.substr(0, i)+ x + s.substr(i+1, s.length()-i), cmp))
return true;
}
return false;
}
int main() {
fin >> n;
while(n--) {
fin >> l;
string s;
char q;
while(l--) {
fin >> q;
s += q;
}
if(posibil("A", s)) fout << true << '\n';
else if(posibil("B", s)) fout << true << '\n';
else if(posibil("C", s)) fout << true << '\n';
else fout << false << '\n';
}
}