您的位置:首页 > 大数据 > 人工智能

2015 Multi-University Training Contest 7 hdu 5375 Gray code

2015-08-11 19:29 393 查看

Gray code

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


[align=left]Problem Description[/align]
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.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 210010;
int dp[maxn][2],w[maxn],len;
char str[maxn];
int main() {
int kase,cs = 1;
scanf("%d",&kase);
while(kase--) {
scanf("%s",str);
len = strlen(str);
for(int i = 0; i < len; ++i)
scanf("%d",w + i);
memset(dp,-1,sizeof dp);
if(str[0] == '0' || str[0] == '?') dp[0][0] = 0;
if(str[0] == '1' || str[0] == '?') dp[0][1] = w[0];
for(int i = 1; i < len; ++i){
if(str[i] == '1' || str[i] == '?'){
if(dp[i-1][1] != -1) dp[i][1] = max(dp[i][1],dp[i-1][1]);
if(dp[i-1][0] != -1) dp[i][1] = max(dp[i][1],dp[i-1][0] + w[i]);
}
if(str[i] == '0' || str[i] == '?'){
if(dp[i-1][0] != -1) dp[i][0] = max(dp[i][0],dp[i-1][0]);
if(dp[i-1][1] != -1) dp[i][0] = max(dp[i][0],dp[i-1][1] + w[i]);
}
}
printf("Case #%d: %d\n",cs++,max(dp[len-1][0],dp[len-1][1]));
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: