0/1字符串问题

描述

编程找出符合下列条件的字符串:①字符串中仅包含0和1两个字符;②字符串的长度为n;③字符串中不含有三个连续的相同子串。

输入格式

输入文件仅包含一个整数n(0<n≤35),表示字符串的长度。

输出格式

输出文件仅包含一个整数,表示符合上述条件的字符串的总数。

测试样例1

输入

2

输出

4

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int res[40],cnt,n;
int check(int k)
{
    for(int i=1;i<=k/3;i++)//枚举长度
    {
        int j=k-3*i+1;//起点
        int flag=1;
        for(int l=1;l<=i;l++)
        {
            if(res[j+l-1]!=res[j+l-1+i]||res[j+l-1]!=res[j+l-1+i+i])
            {
                flag=0;break;
            }
        }
        if(flag) return 0;
    }
    return 1;
}
void dfs(int step)
{
    if(step==n+1)
    {
        cnt++;return ;
    }
    res[step]=0;
    if(check(step)) dfs(step+1);
    res[step]=1;
    if(check(step)) dfs(step+1);
}
int main()
{
    while(scanf("%d",&n)==1)
    {
        cnt=0;
        //memset(res,0,sizeof(res));
        res[1]=0;
        dfs(2);
        printf("%d\n",cnt*2);
    }
    return 0;
}


你可能感兴趣的:(DFS)