Pagini recente » Cod sursa (job #2907173) | Cod sursa (job #2748125) | Cod sursa (job #1905107) | Cod sursa (job #671896) | Cod sursa (job #1558371)
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <vector>
#define N 10001
using namespace std;
int A[N];
int n;
int matchesC(int);
// Returns the next position after a matching B starting at position a
int matchesB(int a) {
if (a >= n)
return 0;
if (A[a] == 2)
return matchesB(a + 1);
if (n - a + 1 >= 5 && A[a] == 1 && A[a + 2] == 3)
return matchesC(a + 4);
return 0;
}
// Returns the next position after a matching C starting at position a
int matchesC(int a) {
if (a > n)
return 0;
if (A[a] == 2)
return a + 1;
int len = n - a + 1;
if (len >= 3 && A[a] == 1 && A[a + 1] == 2)
return a + 3;
if (len >= 7 && A[a] == 3)
return matchesC(matchesB(a + 1));
return 0;
}
int main() {
ifstream fin("perle.in");
ofstream fout("perle.out");
int t;
fin >> t;
while (t--) {
fin >> n;
if (n == 1) {
fout << 1 << "\n";
fin >> n;
}
else {
for (int i = 1; i <= n; ++i)
fin >> A[i];
fout << (matchesB(1) || matchesC(1)) << "\n";
}
}
return 0;
}