码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5375(Gray code-格雷码dp)

时间:2015-08-26 22:31:21      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

Gray code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 860    Accepted Submission(s): 490


Problem Description
The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed to prevent spurious output from electromechanical switches. Today, Gray codes are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems.

技术分享


Now , you are given a binary number of length n including ‘0’ , ’1’ and ‘?’(? means that you can use either 0 or 1 to fill this position) and n integers(a1,a2,….,an) . A certain binary number corresponds to a gray code only. If the ith bit of this gray code is 1,you can get the point ai.
Can you tell me how many points you can get at most?

For instance, the binary number “00?0” may be “0000” or “0010”,and the corresponding gray code are “0000” or “0011”.You can choose “0000” getting nothing or “0011” getting the point a3 and a4.
 

Input
The first line of the input contains the number of test cases T.

Each test case begins with string with ‘0’,’1’ and ‘?’.

The next line contains n (1<=n<=200000) integers (n is the length of the string).

a1 a2 a3 … an (1<=ai<=1000)
 

Output
For each test case, output “Case #x: ans”, in which x is the case number counted from one,’ans’ is the points you can get at most
 

Sample Input
2 00?0 1 2 4 8 ???? 1 2 4 8
 

Sample Output
Case #1: 12 Case #2: 15
Hint
https://en.wikipedia.org/wiki/Gray_code http://baike.baidu.com/view/358724.htm
 

Author
UESTC
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5421 5420 5419 5418 5417 
 



格雷码

二进制转格雷码的话

二进制码→格雷码(编码):
此方法从对应的n位二进制码字中直接得到n位格雷码码字,步骤如下:
对n位二进制的码字,从右到左,以0到n-1编号
如果二进制码字的第i位和i+1位相同,则对应的格雷码的第i位为0,否则为1(当i+1=n时,二进制码字的第n位被认为是0,即第n-1位不变)[3]
公式表示: (G:格雷码,B:二进制码)

技术分享

,直接dp就行



#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (200000+10)
#define MAXai (1000)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
char s[MAXN];
int f[MAXN][2];
int p[MAXN];
int a[MAXN];
int main()
{
//	freopen("G.in","r",stdin);
	
	int T; cin>>T;
	For(kcase,T) {
		MEMi(f) MEM(a) MEM(p) 
		
		scanf("%s",s);
		int n=strlen(s);
		Rep(i,n) p[i]=s[n-1-i]=='?' ? -1 : s[n-1-i]-'0';
		p[n]=0; 
		
		RepD(i,n-1) scanf("%d",&a[i]);
		
		if (p[0]==-1) f[0][0]=0,f[0][1]=0;
		if (p[0]==0) f[0][0]=0;
		if (p[0]==1) f[0][1]=0;
		
		For(i,n) Rep(j,2) {
			if (p[i]!=-1&&p[i]!=j) continue;
			
			f[i][j]=max(f[i-1][j],f[i][j]);
			f[i][j]=max(f[i][j],f[i-1][j^1]+a[i-1]);
			
		}
		
//		For(i,n) {
//			Rep(j,2) cout<<f[i][j]<<' ';cout<<endl; 
//		}
		
		printf("Case #%d: %d\n",kcase,f[n][0]);
		
	}
	
	return 0;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5375(Gray code-格雷码dp)

标签:

原文地址:http://blog.csdn.net/nike0good/article/details/48009703

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!