tyvj 1170 0/1字符串问题 字符串中仅包含0和1两个字符;②字符串的长度为n;③字符串中不含有三个连续的相同子串 搜索

 
From Admin
0/1字符串问题
 
     
     
  描述 Description  
  编程找出符合下列条件的字符串:①字符串中仅包含0和1两个字符;②字符串的长度为n;③字符串中不含有三个连续的相同子串。
     
     
  输入格式 Input Format  
  输入文件仅包含一个整数n(0<n≤35),表示字符串的长度。
     
     
  输出格式 Output Format  
  输出文件仅包含一个整数,表示符合上述条件的字符串的总数。
     
     
  样例输入 Sample Input  
  2
     
     
  样例输出 Sample Output  
  4
     
     
  时间限制 Time Limitation  
  各个测试点1s
     
     
  注释 Hint  
  各个测试点1s
我写的裸的搜索1S过去不,所以原来n<=40改成了n<=35,如果你有更好的算法,请发站内消息联系我!
 

 

 

 

#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;
}

 

你可能感兴趣的:(tyvj 1170 0/1字符串问题 字符串中仅包含0和1两个字符;②字符串的长度为n;③字符串中不含有三个连续的相同子串 搜索)