ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing

H. Hashing
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

In this problem you are given a byte array a. What you are going to do is to hash its subsequences. Fortunately you don't have to make a painful choice among infinitely large number of ways of hashing, as we have made this decision for you.

If we consider a subsequence as a strictly increasing sequence s of indices of array a, the hash function of the subsequence is calculated by the formula:

Here,  means the bitwise XOR operation. See Note section if you need a clarification.

As you need to store the values in an array after all, you want to know the maximum possible value of the hash function among all subsequences of array a.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000), denoting the number of bytes in array a. The second line contains n bytes written in hexadecimal numeral system and separated by spaces. Each byte is represented by exactly two hexadecimal digits (0...F).

Output

Output a single integer which is the maximum possible value of the hash function a subsequence of array a can have.

Sample test(s)
input
3
03 00 1B
output
29
input
3
01 00 02
output
4
Note

In the first sample one of the best ways is to choose the subsequence 03 00 1B.

In the second sample the only best way is to choose the subsequence 01 02.

Here we are to tell you what a bitwise XOR operation is. If you have two integers x and y, consider their binary representations (possibly with leading zeroes): xk... x2x1x0 and yk... y2y1y0. Here, xi is the i-th bit of number x and yi is the i-th bit of number y. Let be the result of XOR operation of x and y. Then r is defined as rk... r2r1r0 where:

 

题意:N个16进制的数,问从中选出一个序列,Σ i^(p[i]) 最大是多少,p[i]是选中的数的序列。(从0开始计数)

分析:经典dp。

显然有一个dp

dp[i][j]表示前i个,一共选择了j个数的最大值。

显然下一个数的贡献仅与j的大小有关,且仅与j%256有关,

那么dp就变味dp[i][0...255]表示前i为,一共选择了这么多个数的最大值。(题目不限制选择的个数)

然后转移就可以每次枚举转移。

 1 /**
 2 Create By yzx - stupidboy
 3 */
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cmath>
 8 #include <deque>
 9 #include <vector>
10 #include <queue>
11 #include <iostream>
12 #include <algorithm>
13 #include <map>
14 #include <set>
15 #include <ctime>
16 #include <iomanip>
17 using namespace std;
18 typedef long long LL;
19 typedef double DB;
20 #define MIT (2147483647)
21 #define INF (1000000001)
22 #define MLL (1000000000000000001LL)
23 #define sz(x) ((int) (x).size())
24 #define clr(x, y) memset(x, y, sizeof(x))
25 #define puf push_front
26 #define pub push_back
27 #define pof pop_front
28 #define pob pop_back
29 #define ft first
30 #define sd second
31 #define mk make_pair
32 
33 inline int Getint()
34 {
35     int Ret = 0;
36     char Ch = ' ';
37     bool Flag = 0;
38     while(!(Ch >= '0' && Ch <= '9'))
39     {
40         if(Ch == '-') Flag ^= 1;
41         Ch = getchar();
42     }
43     while(Ch >= '0' && Ch <= '9')
44     {
45         Ret = Ret * 10 + Ch - '0';
46         Ch = getchar();
47     }
48     return Flag ? -Ret : Ret;
49 }
50 
51 const int N = 100010, M = 1 << 8;
52 int n, arr[N];
53 LL dp[N][M];
54 
55 inline void Input()
56 {
57     scanf("%d", &n);
58     for(int i = 1; i <= n; i++) scanf("%x", &arr[i]);
59 }
60 
61 inline void Solve()
62 {
63     for(int i = 0; i < M; i++) dp[0][i] = -1;
64     dp[0][255] = 0;
65     for(int i = 1; i <= n; i++)
66         for(int j = 0; j < M; j++)
67         {
68             dp[i][j] = dp[i - 1][j];
69             int p = j ? j - 1 : 255;
70             if(dp[i - 1][p] < 0) continue;
71             int c = i >> 8;
72             if(((c << 8) | j) >= i) c--;
73             dp[i][j] = max(dp[i][j], dp[i - 1][p] + (arr[i] ^ ((c << 8) | j)));
74         }
75 
76     LL ans = 0;
77     for(int i = 0; i < M; i++) ans = max(ans, dp[n][i]);
78     cout << ans << endl;
79 }
80 
81 int main()
82 {
83     Input();
84     Solve();
85     return 0;
86 }
View Code

 

posted @ 2015-12-25 10:10  yanzx6  阅读(356)  评论(0编辑  收藏  举报