Cod sursa(job #3327215)
| Utilizator | Data | 2 decembrie 2025 20:04:19 | |
|---|---|---|---|
| Problema | Perle | Scor | 50 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva de probleme | Marime | 2.07 kb |
#include <fstream>
#include <stack>
using namespace std;
#define N 100//00
int v[N+1];
int main()
{
ifstream cin ("perle.in");
ofstream cout ("perle.out");
int t, n, i, ok;
char c;
cin >> t;
while (t--){
cin >> n;
for (i=1; i<=n; i++)
cin >> v[i];
if (n == 1)
cout << 1 << '\n';
else if (n == 3 && v[1] == 1 && v[2] == 2)
cout << 1 << '\n';
else{
stack <char> s;
if (v[1] == 1 || v[1] == 2)
s.push('B');
else
s.push('C');
ok = 1;
for (i=1; i<=n && ok; i++){
if (s.empty()){
ok = 0;
break;
}
c = s.top();
s.pop();
if (c == 'B'){
if (v[i] == 1){
s.push('C');
s.push('A');
s.push('3');
s.push('A');
s.push('1');
}else if (v[i] == 2){
s.push('B');
s.push('2');
}else{
ok = 0;
break;
}
i--;
}else if (c == 'C'){
if (v[i] == 1){
s.push('A');
s.push('2');
s.push('1');
}else if (v[i] == 2)
s.push('2');
else{
s.push('C');
s.push('B');
s.push('3');
}
i--;
}else if (c != 'A' && c - '0' != v[i]){
ok = 0;
break;
}
}
if (ok == 1)
ok = s.empty();
cout << ok << '\n';
}
}
return 0;
}
