0/1字符串问题

这篇博客详细介绍了0/1字符串问题,包括问题描述、输入和输出格式,并提供了测试样例1的输入和输出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

编程找出符合下列条件的字符串:①字符串中仅包含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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值