Triple-Free Binary Strings UVA - 11127

问题

https://vjudge.net/problem/UVA-11127

分析

#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=30+5;
int n,kase=0;
char s[maxn],temp[maxn];

inline bool check(int lim){
    int M=lim/3;
    for(int len=1;len<=M;++len){
        int l3=lim-len,l2=l3-len,l1=l2-len,flag=1;
        for(int i=0;i<len;++i){
            if(s[l1+i]!=s[l2+i] || s[l2+i]!=s[l3+i]){
                flag=0;
                break;
            }
        }
        if(flag) return false;
    }
    return true;
}

inline int solve(int d){
    if(d==n){
        return 1;
    }
    int ans=0;
    if(s[d]=='*'){
        s[d]='0';
        if(check(d+1)) ans+=solve(d+1);
        s[d]='1';
        if(check(d+1)) ans+=solve(d+1);
        s[d]='*';
    }else{
        //10 **01**01**  考虑前面对后面的影响
        if(check(d+1)) ans+=solve(d+1);
    }
    return ans;
}

int main(void){
    while(scanf("%d",&n)==1 && n){
        scanf(" %s",s);
        int ans=0;
        ans=solve(0);
        printf("Case %d: %d\n",++kase,ans);
    }
    return 0;
}
发布了180 篇原创文章 · 获赞 3 · 访问量 3456

猜你喜欢

转载自blog.csdn.net/zpf1998/article/details/105032516