zoj 2745 01-K Code

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1745


题目大意:

对于一个01串,如果任意连续子串的0的个数和1的个数之差的绝对值不超过k(2~5),那么这个01串合法。

问有多少个长度为n(1~62)的合法串。


题目思路:

如果知道某长度为i合法串的后缀里最大的0和1的差,以及最大的1和0的差,那么我们就可以为其添0或1,构成长度为i+1的新串。

添加0:

new_max{0-1}=max{1,max{0,1}+1}

new_max{1-0}=max{-1,max{1-0}-1}

添加1同理。


代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<string>
using namespace std;
#define ll long long
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n))
#define clr_all(x,c) memset(x,c,sizeof(x))
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle l+r>>1
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define eps (1e-8)
#define PI 3.1415926535897932384626433832795
#define E 2.7182818284590452353602874713527
template <class T> T _min(T a,T b){return a<b? a:b;}
template <class T> T _max(T a,T b){return a>b? a:b;}
template <class T> T _abs(T a){return a>0? a:-a;}
template <class T> T _mod(T a,T m){return a<m? (a<0? (a%m+m)%m:a):a%m;}
template <class T> T _gcd(T a,T b){while(b){T t=b;b=a%b;a=t;}return a;}
template <class T> void _swap(T &a,T &b){T t=b;b=a;a=t;}
template <class T> void getmax(T &a,T b){a= a>b? a:b;}
template <class T> void getmin(T &a,T b){a= (a!=-1 && a<b)? a:b;}
int TS,cas=1;
const int M=66+5;
int n,m;
ll dp[M][11][11];

void run(){
	int i,j,k;
	clr_all(dp,0);
	dp[1][-1+5][1+5]=1;
	dp[1][1+5][-1+5]=1;
	for(i=2;i<=n;i++){
		for(j=-m;j<=m;j++){
			for(k=-m;k<=m;k++){
				if(_abs(j+1)<=m && _abs(k-1)<=m)
					dp[i][_max(j+1,1)+5][_max(k-1,-1)+5]+=dp[i-1][j+5][k+5];
				if(_abs(j-1)<=m && _abs(k+1)<=m)
					dp[i][_max(j-1,-1)+5][_max(k+1,1)+5]+=dp[i-1][j+5][k+5];
			}
		}
	}
	ll res=0;
	for(j=-m+5;j<=m+5;j++)
		for(k=-m+5;k<=m+5;k++)
			res+=dp[n][j][k];
	printf("%lld\n",res);
}

void presof(){
}

int main(){
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	presof();
	//run();
	while(~scanf("%d%d",&n,&m)) run();
	//for(scanf("%d",&TS),cas=1;cas<=TS;cas++) run();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值