例如:
1. BAACAACCBAAA 连续子串'CBA'中包含了‘A’、‘B’、‘C’各一个,所以为纯洁字符串
2. AABBCCAABB 不存在一个长度为3的连续子串包含‘A’、‘B’、‘C’,所以为暗黑字符串
以下这种解法如何解释?求大神解释一下
#include
#include
#include
using namespace std;
long dp[33][3][3];
int main() {
for (int i = 0; i < 3; i ++) {
for (int j = 0; j < 3; j ++) {
dp[2][i][j] = 1;
}
}
for (int len = 2; len < 30; len ++) {
for (int i = 0; i < 3; i ++) {
for (int j = 0; j < 3; j ++) {
for (int k = 0; k < 3; k ++) {
int s = (1 << i) | (1 << j) | (1 << k);
if (s == 7) {
continue;
}
dp[len + 1][j][k] += dp[len][i][j];
}
}
}
}
int n;
while (cin >> n) {
if (n == 0) {
cout << 1 << endl;
} else if (n == 1) {
cout << 3 << endl;
} else {
long ans = 0;
for (int i = 0; i < 3; i ++) {
for (int j = 0; j < 3; j ++) {
ans += dp[n][i][j];
}
}
cout << ans << endl;
}
}
}