Hdu5375--dp--gray Code

Source: Internet
Author: User

/************************************************
/* Gray value and binary conversion formula, G[i] = b[i]^b[i-1], binary front complement 0
DP[I][J] is defined as the current I-gray value, the value is J and
*/* Author:p owatr* Created time:2015-8-11 13:19:46* File name:1007.cpp ************************************ /#include <cstdio> #include <algorithm> #include <iostream> #include <sstream># Include <cstring> #include <cmath> #include <string> #include <vector> #include <queue># Include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << # Define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int MAXN = 2e6 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;char s[maxn];int Ss[max N][2];int dp[maxn][2];int A[maxn];int Main () {int T; scanf ("%d", &t); for (int cas = 1; CAs <= T; cas++) {memset (A, 0, sizeof (a)); memset (s, 0, sizeof (s)); memset (DP, 0, sizeof (DP)); scanf ("%s", s+1); int len = sTrlen (s+1); for (int i = 1; I <= len; i++) scanf ("%d", &a[i]); if (s[1] = = ' 1 ') dp[1][1] = a[1]; else if (s[1] = = ' 0 ') dp[1][0] = 0; else if (s[1] = = '? ') {Dp[1][1] = a[1], dp[1][0] = 0; } for (int i = 2; I <= len; i++) {if (s[i] = = ' 1 ') {if (s[i-1] = = ' 0 ') DP[I][1] = dp[i-1][0] + a[i]; else if (s[i-1] = = ' 1 ') dp[i][1] = dp[i-1][1]; else if (s[i-1] = = '? ') {Dp[i][1] = max (dp[i-1][1], dp[i-1][0] + a[i]); }} else if (s[i] = = ' 0 ') {if (s[i-1] = = ' 1 ') dp[i][0] = dp[i-1][1] + A[i]; else if (s[i-1] = = ' 0 ') dp[i][0] = dp[i-1][0]; else if (s[i-1] = = '? ') {Dp[i][0] = max (dp[i-1][1] + a[i], dp[i-1][0]); }} else if (s[i] = = '? ') {if (s[i-1] = = ' 1 ') {dp[i][0] = dp[i-1][1] + a[i]; DP[I][1] = dp[i-1][1]; } else if (s[i-1] = = ' 0 ') {dp[i][0] = dp[i-1][0]; DP[I][1] = dp[i-1][0] + a[i]; } else if (s[i-1] = = '? ') {Dp[i][0] = max (dp[i-1][0], dp[i-1][1]+a[i]); DP[I][1] = max (dp[i-1][1], dp[i-1][0]+a[i]); }}} printf ("Case #%d:%d\n", CAS, Max (dp[len][1], dp[len][0])); } return 0;}

  

Hdu5375--dp--gray Code

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.