Cod sursa(job #2260864)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 15 octombrie 2018 18:12:59
Problema Xor Max Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in ("xormax.in");
ofstream out ("xormax.out");

struct Trie{
  int val;
  Trie* son[2];
};

void add(Trie* &root , int key , int pos , int val){
  if(root == nullptr)
    root = new Trie();

  root->val = val;


  if(pos == -1)
    return ;
  else
    add(root->son[0 < (key & (1 << pos)) ] , key , pos - 1 , val);
}

int query(Trie* &root , int key , int pos , int &result){
  if(root == NULL) {
    return 0;
  }

  if(pos == -1)
    return root->val;
  else {
    int bit = (0 < (key & (1 << pos)) );

    if(root->son[!bit] != NULL) {
      result = result * 2 + !bit;
      return query(root->son[!bit] , key , pos - 1, result);
    } else if(root->son[bit] != NULL) {
      result = result * 2 + bit;
      return query(root->son[bit] , key , pos - 1, result);
    } else {
      cout << "Error\n";
      return 0;
    }
  }
}

int main()
{
  Trie* root;
  root = new Trie();

  int n;
  in >> n;
  int result = 0;

  //*
  add(root ,0 , 20 , 1);
  //cout << ":";
  int smax = 0, x = 1, y = 1;

  for(int i = 1 ; i <= n ; i++){
    int a;
    in >> a;
    result ^= a;

    int smax2 = 0 , val;
    val = query(root , result , 20 , smax2);
    smax2 ^= result;

    if(smax < smax2){
      smax = smax2;
      x = val;
      y = i;
    }

    cout << '\n';

    add(root , result , 20 , i + 1);
  }
  out << smax << " " << x << " " << y << '\n';
  //*/

  return 0;
}