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:

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

Here, ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing 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.

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

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

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

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 ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashingbe the result of XOR operation of x and y. Then r is defined as rk... r2r1r0 where:

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

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

分析:经典dp。

显然有一个dp

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

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

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

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

 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = , M = << ;
int n, arr[N];
LL dp[N][M]; inline void Input()
{
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%x", &arr[i]);
} inline void Solve()
{
for(int i = ; i < M; i++) dp[][i] = -;
dp[][] = ;
for(int i = ; i <= n; i++)
for(int j = ; j < M; j++)
{
dp[i][j] = dp[i - ][j];
int p = j ? j - : ;
if(dp[i - ][p] < ) continue;
int c = i >> ;
if(((c << ) | j) >= i) c--;
dp[i][j] = max(dp[i][j], dp[i - ][p] + (arr[i] ^ ((c << ) | j)));
} LL ans = ;
for(int i = ; i < M; i++) ans = max(ans, dp[n][i]);
cout << ans << endl;
} int main()
{
Input();
Solve();
return ;
}
上一篇:WebGPU学习(二): 学习“绘制一个三角形”示例


下一篇:ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 A. Anagrams