HDU5968 异或密码 —— 二分 + 边界的细节处理

本文介绍了一种基于异或运算的密码算法问题及其解决方案。针对一系列整数,通过计算连续子序列的异或值来响应特定询问,寻找与目标值最接近的异或结果,并返回该子序列的最大长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5968


异或密码

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1214    Accepted Submission(s): 404


Problem Description
晨晨在纸上写了一个长度为N的非负整数序列{ ai }。对于这个序列的一个连续子序列{ al,al+1,ar }晨晨可以求出其中所有数异或的结果  alxoral+1xor...xorar 其 中xor表示位异或运算,对应C、C++、 Java等语言中的^运算。
小璐提出了M个询问,每个询问用一个整数  xi 描述。
对于每个询问,晨晨需要找到序列{ ai }的所有连续子序列,求出每个子序列异或的结果,找到所有的结果中与  xi 之差的绝对值最小的一个,并告诉小璐相应子序列的长度。
若有多个满足条件的连续子序列,则告诉小璐这些子序列中最长的长度。
 

Input
包含多组测试数据,第一行一个正整数T,表示数据组数。
每组数据共两行。
第一行包含N+1个非负整数。其中第一个数为N,表示序列的长度;接下来N 个数,依次描述序列{  ai }中的每个数。
第二行包含M+1个整数。其中第一个数为M,表示询问的个数;接下来M个数  xi ,每个数对应题目描述中的一个询问。
保证 1 <= N <= 100,1 <= M <= 100, ai  <= 1024,| xi | <= 1024,数据组数 <= 100。
 

Output
对于每组数据输出M + 1行。前M行对应晨晨M个询问的回答,第M + 1行为空行
 

Sample Input
  
2 2 1 1 2 0 2 3 1 2 4 3 10 5 1
 

Sample Output
  
2 1 3 2 1



题解:

1.先预处理出连续子序列的异或值和长度。

2.由于异或值最大只能为2048,所以可以用优先队列保存每个异或值下的长度,用一个数组s[]保存有哪些长度,然后再对s[]数组去重(sort()+unique())。当然不推荐这种做法,因为每个异或值下,只有最大长度是有用的,所以再保存其他值就浪费了空间。推荐的做法是:将每个连续子序列的异或值和长度放到一个结构体里面,然后再对结构体进行排序,最后手动去重。

3.对于每次操作,使用二分试图去找到第一个异或值大于等于x的结构体,然后就是一些有关二分以及其他的细节处理,不细讲。



数组 + 优先队列(不推荐):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
//#define LOCAL
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 2048+10;

int a[110];
int n, m, cnt;
priority_queue<int>q[maxn];

int s[110*110];

int sch(int x)
{
    int l = 1, r = cnt;
    while(l<r)
    {
        int mid = (l+r)>>1;
        if(s[mid]>=x)
            r = mid;
        else
            l = mid + 1;
    }
    return r;
}

void init()
{
    cnt = 0;
    scanf("%d",&n);
    for(int i = 1; i<=n; i++)
        scanf("%d",&a[i]), a[i] = a[i]^a[i-1];

    for(int  i = 0; i<maxn; i++)
        while(!q[i].empty()) q[i].pop();

    for(int i = 1; i<=n; i++)
    for(int j = i; j<=n; j++)
    {
        s[++cnt] = a[j]^a[i-1];
        q[a[j]^a[i-1]].push(j-i+1);
    }

    sort(s+1,s+1+cnt);
    cnt = unique(s+1,s+1+cnt) - (s+1);
}

void solve()
{
    scanf("%d",&m);
    for(int i = 0; i<m; i++)
    {
        int x, ans;
        scanf("%d",&x);
        int t = sch(x);

        if(s[t]==x)
            ans = q[x].top();
        else if(t==1)
            ans = q[s[t]].top();
        else
        {
            if(abs(x-s[t])==abs(x-s[t-1]))
                ans = max(q[s[t]].top(), q[s[t-1]].top());
            else if(abs(x-s[t])<abs(x-s[t-1]))
                ans = q[s[t]].top();
            else
                ans = q[s[t-1]].top();
        }
        cout<<ans<<endl;
    }
    cout<<endl;
}

int main()
{
#ifdef LOCAL
    freopen("1", "r", stdin);
//      freopen("output.txt", "w", stdout);
#endif
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        solve();
    }
}



结构体:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
//#define LOCAL
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 2048+10;

int a[110];
int n, m, cnt;

struct node
{
    int val, len;
    bool operator<(node &xx)const{
        if(val==xx.val) return len>xx.len;
        return val<xx.val;
    }
}s[110*110];

void init()
{
    scanf("%d",&n);
    for(int i = 1; i<=n; i++)
        scanf("%d",&a[i]), a[i] = a[i]^a[i-1];

    cnt = 0;
    for(int i = 1; i<=n; i++)
    for(int j = i; j<=n; j++)
        s[++cnt].val = a[i-1]^a[j], s[cnt].len = j-i+1;

    n = 0;
    sort(s+1,s+1+cnt);
    for(int i = 1; i<=cnt; i++)
        if(i==1 || s[i].val!=s[i-1].val)
            s[++n] = s[i];
}

int sch(int x)
{
    int l = 1, r = n;
    while(l<r)
    {
        int mid = (l+r)>>1;
        if(s[mid].val>=x)
            r = mid;
        else
            l = mid + 1;
    }
    return r;
}

void solve()
{
    scanf("%d",&m);
    for(int i = 0; i<m; i++)
    {
        int x, ans;
        scanf("%d",&x);
        int t = sch(x);

        if(s[t].val==x)
            ans = s[t].len;
        else if(t==1)
            ans = s[1].len;
        else
        {
            if( abs(x-s[t].val)==abs(x-s[t-1].val) )
                ans = max( s[t].len, s[t-1].len );
            else if( abs(x-s[t].val)<abs(x-s[t-1].val) )
                ans = s[t].len;
            else
                ans = s[t-1].len;
        }
        cout<<ans<<endl;
    }
    cout<<endl;
}

int main()
{
#ifdef LOCAL
    freopen("1", "r", stdin);
//      freopen("output.txt", "w", stdout);
#endif
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        solve();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值