直接二进制暴,因为分析一下可行解较少。
用位运算可防卡姿势。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> ans;
inline int judge(int v){
ans.push_back(v);
for(int i=1;i*3<=ans.size();i++){
int ok = 1;
for(int j=1;j<=i;j++){
int fir = ans[ans.size()-j];
for(int k=ans.size()-j,cnt=1;k>=0&&cnt<=3;k-=i,cnt++)
{ if(ans[k]!=fir) {ok = 0; break;} }
}
if(ok) {ans.pop_back(); return 0;}
}
ans.pop_back();
return 1;
}
char str[100];
int cnt,n;
void dfs(int p){
if(p == n){
cnt++;
return ;
}
if(str[p]=='0'||str[p]=='1'){
if(judge(str[p]-'0')){
ans.push_back(str[p]-'0');
dfs(p+1);
ans.pop_back();
}
}
else{
for(int i=0;i<=1;i++){
if(judge(i)){
ans.push_back(i);
dfs(p+1);
ans.pop_back();
}
}
}
}
int main()
{
int T = 1;
while(scanf("%d",&n)&&n){
scanf("%s",str);
ans.clear();
cnt = 0;
dfs(0);
printf("Case %d: %d\n",T++,cnt);
}
return 0;
}
1150




被折叠的 条评论
为什么被折叠?



