Codechef REBXOR[dp+字典树]


Codechef REBXOR[dp+字典树]_第1张图片


解题思路:1.区间异或和可以搞前缀[or后缀]异或, x o r i = l r a = p r e [ l ] ⊕ p r e [ r ] xor_{i=l}^{r}a=pre[l]\oplus pre[r] xori=lra=pre[l]pre[r]
2.那么题目就变成了 p r e [ l ] ⊕ p r e [ r ] + s u f [ l 1 ] ⊕ s u f [ r 1 ] , p r e 是 前 缀 异 或 和 , s u f 是 后 缀 异 或 和 pre[l]\oplus pre[r] +suf[l_1]\oplus suf[r_1],pre是前缀异或和,suf是后缀异或和 pre[l]pre[r]+suf[l1]suf[r1],presuf
3.对于一边相当于求任意两个 p r e [ i ] pre[i] pre[i]异或值最大,对于这种考虑两端的我们先预处理出一端再求另一个,我们就用一个 d p dp dp数组存一下 [ 1 , i ] [1,i] [1,i]每个位置选两个数的异或最大值然后再跑一遍逆序的相加就可以了


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define hash Hash
#define next Next
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 4e5 + 10, mod = 1e9 + 9;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{
    read(first);
    read(args...);
}
int n, idx;
int a[N];
int tr[31 * N][2];
int dp[N];
int pre[N], suf[N];
void insert(int x)
{
    int rt = 0;
    for(int i = 31; i >= 0; -- i)
    {
        int ch = (x >> i) & 1;
        if(!tr[rt][ch]) 
        {
            tr[rt][ch] = ++ idx;
            tr[idx][0] = tr[idx][1] = 0;
        }
        rt = tr[rt][ch];
    }
}

int ask(int x)
{
    int rt = 0;
    int tmp = 0;
    for(int i = 31; i >= 0; -- i)
    {
        int c = (x >> i) & 1;
        if(tr[rt][c ^ 1]) tmp += 1 << i, rt = tr[rt][c ^ 1];
        else rt = tr[rt][c];
    }
    return  tmp;
}

int main()
{
   read(n);
   pre[0] = 0;
   for(int i = 1; i <= n; ++ i)
   {
       read(a[i]);
       pre[i] = pre[i - 1] ^ a[i];
   }
   suf[n + 1] = 0;
   for(int i = n; i >= 1; -- i)
      suf[i] = suf[i + 1] ^ a[i];
   insert(0); 
   for(int i = 1; i <= n; ++ i)
   {
       dp[i] = max(dp[i - 1],ask(pre[i]));
       insert(pre[i]);
   }
   insert(0);
   tr[0][0] = tr[0][1] = idx = 0;
   int ans = 0;
   for(int i = n; i >= 2; i --)
   {
       ans = max(ans,ask(suf[i]) + dp[i - 1]);
       insert(suf[i]);
   }
   cout << ans << endl;
   return 0;
}

你可能感兴趣的:(字符串算法)