题意:给出一个字符串,包括0、1、*,其中×是可以替换成0或者1的,如果字符串的某个子串S有SSS这样的连续重复3次出现,不是Triple-free串,问给出的字符串可以形成多少个非Triple-free串。
题解:因为串长度最多31,所以可以暴力枚举每一位,边枚举边判断。
#include <stdio.h>
#include <string.h>
const int N = 35;
char str[N], str2[N];
int n;
long long res;
bool judge(int cur) {
for (int i = 1; i * 3 <= (cur + 1); i++) {
int e = cur - i * 3, cnt2 = 0;
for (int j = cur; j > cur - i; j--) {
int cnt = 0;
for (int k = j; k > e; k -= i)
if (str2[j] != str2[k])
break;
else
cnt++;
if (cnt == 3)
cnt2++;
else
break;
}
if (cnt2 == i)
return false;
}
return true;
}
void dfs(int cur) {
if (cur == n) {
res++;
return;
}
if (cur == 0 || cur == 1) {
if (str[cur] == ‘0‘ || str[cur] == ‘1‘) {
str2[cur] = str[cur];
dfs(cur + 1);
}
else {
str2[cur] = ‘0‘;
dfs(cur + 1);
str2[cur] = ‘1‘;
dfs(cur + 1);
}
return;
}
str2[cur] = ‘0‘;
if (judge(cur)) {
if (str[cur] == ‘0‘ || str[cur] == ‘*‘)
dfs(cur + 1);
}
str2[cur] = ‘1‘;
if (judge(cur)) {
if (str[cur] == ‘1‘ || str[cur] == ‘*‘)
dfs(cur + 1);
}
}
int main() {
int cas = 1;
while (scanf("%d", &n) == 1 && n) {
scanf("%s", str);
res = 0;
dfs(0);
printf("Case %d: %lld\n", cas++, res);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-09 08:51:16