samedi 30 mai 2015

XMAX - XOR Maximization getting wrong answer

I am trying to solve XMAX-XOR Maximization problem on spoj.Here is my code-

#include <stdio.h>
struct node{
    int value;
    struct node *left;
    struct node *right;
};
void insert(int n, int pos, struct node *t);
int find(int a[], int n);
struct node *alloc();
int findmax(int n, int p, struct node *a);


int main()
{

    int n;
    scanf("%d", &n);
    int a[100000];
    int i;
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
    int max = find(a, n);
    printf("%d\n", max);

    return 0;
}
void insert(int n, int pos, struct node *t)
{
    if (pos >= 0)
    {
        struct node  *m;
        int bit = (1 << pos)&n;
        if (bit)
        {


            if (t->right == NULL)
            {
                m = alloc();
                m->value = 1;
                m->left = NULL;
                m->right = NULL;
                t->right = m;
            }


            if (pos == 0)
            {
                m = alloc();
                m->value = n;
                m->left = NULL;
                m->right = NULL;
                t->right->left = m;
            }


            insert(n, pos - 1, t->right);
        }
        else
        {


            if (t->left == NULL)
            {
                m = alloc();
                m->value = 0;
                m->left = NULL;
                m->right = NULL;
                t->left = m;
            }


            if (pos == 0)
            {
                m = alloc();
                m->value = n;
                m->left = NULL;
                m->right = NULL;
                t->left->left = m;
            }

            insert(n, pos - 1, t->left);
        }
    }
}

struct node *alloc()
{
    return (struct node *) malloc(sizeof(struct node));
}

int find(int a[], int n)
{
    int ans = 0;
    int z = 0;
    struct node root;
    root.value = 0;
    root.left = root.right = NULL;
    insert(0,31, &root);
    int i;
    for (i = 0; i < n; i++)
    {
        z = z^a[i];
        insert(z, 31, &root);
        int v = findmax(z,31, &root);
        ans = (ans>v) ? ans : v;

    }
    return ans;
}
int findmax(int n, int p, struct node *a)
{
    if (p >= 0)
    {

        int bit = (1 << p)&n;
        if (bit)
        {


            if (a->left != NULL)
            {
                return findmax(n, p - 1, a->left);
            }
            else
                return findmax(n, p - 1, a->right);

        }
        else
        {

            if (a->right != NULL)
            {
                return findmax(n, p - 1, a->right);
            }
            else
                return findmax(n, p - 1, a->left);
        }

    }
    else
        return (a->left->value) ^ n;
}

I checked a number of test cases and got the right output but I am getting a wrong answer on submission.Please help me find where this code is getting failed. I used a trie and the property that F(L,R)=F(1,R) XOR F(1,L-1) to solve this question where F(L,R) is XOR of subarray from L to R.

Aucun commentaire:

Enregistrer un commentaire