HDU 5375 (gray code-Grey code DP)

Source: Internet
Author: User

Gray CodeTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 860 Accepted Submission (s): 490


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



Now, is given a binary number of length n including ' 0 ', ' 1 ' and '? ' (? means that can use the either 0 or 1 to the 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 was 1,you can get the point AI.
Can you tell me how many points can get to most?

For instance, the binary number "00?0" could be "0000" or "0010", and the corresponding gray code is "0000" or "0011". You can choose ' 0000 ' getting nothing or ' 0011 ' getting the point A3 and A4.

Inputthe first line of the input contains the number of test cases T.

Each test case is begins with a 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)

Outputfor each test case, output "case #x: ans", in which X are the case number counted from one, ' ans ' are the points you ca n Get at most
Sample Input
200?01 2 4 8???? 1 2 4 8

Sample Output
Case #1:12Case #2:Hinthttps://en.wikipedia.org/wiki/Gray_codehttp://baike.baidu.com/view/358724.htm

Authoruestc
Source2015 multi-university Training Contest 7
Recommendwange2014 | We have carefully selected several similar problems for you:5421 5420 5419 5418 5417



Gray Code

Binary transcoding of gray code words

binary code → Gray Code (Code):
This method obtains the N-bit gray code word directly from the corresponding n-bit binary code word, the steps are as follows:
Code word for n-bit binary, from right to left, numbered 0 to N-1
If the first and i+1 bits of the binary code word are the same, the corresponding gray code of the first bit is 0, otherwise 1 (when i+1=n, the nth bit of the binary code word is considered 0, that is, the n-1 bit is unchanged) [3]
The formula means: (G: Gray code, B: binary code)


, direct DP on the line



#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+ #define MAXAI (+) 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)/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;}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5375 (gray code-Grey code DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.