pku 1173 Bar Codes 經典DP,逐位確定rank
題意:給出條形碼系統
BC(n,k,m) is the set of all symbols with k bars that together extend over exactly n units, each bar being at most m units wide.
1、給出n,k,m,條形碼第一條是黑色。問有多少種不同的劃分方式。
2、給出條形碼,求它的rank(字典序)

第一問應該很好解決,BC(n,k,m)=sum(BC(n-i,k-1,m)),i=1,2..m
第二問也是用經典的逐位確定的方法,唯一要注意的是,白條要從大到小累加(因為0代表白色,顯然,當前條0的位數越多,字典序越?。?,而黑條要從小到大累加
代碼:
1
# include <cstdio>
2
# include <cstring>
3
# include <algorithm>
4
using namespace std;
5
int c[35][35],n,k,m;
6
int main()
7

{
8
scanf("%d%d%d",&n,&k,&m);
9
memset(c,0,sizeof(c));
10
for(int num=1;num<=min(n,m);num++)
11
c[num][1]=1;
12
for(int num=2;num<=n;num++)
13
for(int i=2;i<=k;i++)
14
for(int j=max(1,num-m);j<num;j++)
15
c[num][i]+=c[j][i-1];
16
printf("%d\n",c[n][k]);
17
int num;
18
scanf("%d",&num);
19
while(num--)
20
{
21
char str[50];
22
char color='1';
23
int last=0,rank=0,co=1;
24
scanf("%s",str);
25
for(int i=0;i<strlen(str);i++)
26
{
27
if(str[i]!=color)
28
{
29
int len=i-last;
30
switch(color)
31
{
32
case '1':
33
for(int j=last+1;j<i;j++)
34
rank+=c[n-j][k-co];
35
color='0';
36
break;
37
case '0':
38
for(int j=min(n-1,last+m);j>i;j--)
39
rank+=c[n-j][k-co];
40
color='1';
41
break;
42
};
43
co++;
44
last=i;
45
}
46
}
47
printf("%d\n",rank);
48
}
49
return 0;
50
}
51
# include <cstdio>2
# include <cstring>3
# include <algorithm>4
using namespace std;5
int c[35][35],n,k,m;6
int main()7


{8
scanf("%d%d%d",&n,&k,&m);9
memset(c,0,sizeof(c));10
for(int num=1;num<=min(n,m);num++)11
c[num][1]=1;12
for(int num=2;num<=n;num++)13
for(int i=2;i<=k;i++)14
for(int j=max(1,num-m);j<num;j++)15
c[num][i]+=c[j][i-1];16
printf("%d\n",c[n][k]);17
int num;18
scanf("%d",&num);19
while(num--)20

{21
char str[50];22
char color='1';23
int last=0,rank=0,co=1;24
scanf("%s",str);25
for(int i=0;i<strlen(str);i++)26

{27
if(str[i]!=color)28

{29
int len=i-last;30
switch(color)31

{32
case '1':33
for(int j=last+1;j<i;j++)34
rank+=c[n-j][k-co];35
color='0';36
break;37
case '0':38
for(int j=min(n-1,last+m);j>i;j--)39
rank+=c[n-j][k-co];40
color='1';41
break;42
};43
co++;44
last=i;45
}46
}47
printf("%d\n",rank);48
}49
return 0;50
}51

posted on 2011-01-02 23:17 yzhw 閱讀(386) 評論(0) 編輯 收藏 引用 所屬分類: DP
