异或密码---hdu5968(CCPC合肥,二分)

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

思路:先把所有的连续异或值保存起来,排序,然后用二分找到距离x最近的那个点,判断即可;

 

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <iostream>
#include <time.h> typedef long long LL; using namespace std; const int N = ;
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
const int mod = ; struct node
{
int len, num;
}c[N*N]; int a[N], b[N], k, c1[N*N], c2[N*N]; bool cmp(node p, node q)
{
if(p.num!=q.num)
return p.num<q.num;
return p.len < q.len;
} int Find(int num)
{
int pos = upper_bound(c1, c1+k, num)-c1;
return c2[pos-];
} int solve(int x)///Find x in the c[];
{
int pos = lower_bound(c1, c1+k, x)-c1; if(pos == || c1[pos] == x)///如果数组中含有x,那就找到异或值为x的最大长度;
return Find(c1[pos]); int p = abs(c1[pos]-x);
int q = abs(c1[pos-]-x);
int ans1 = Find(c1[pos]);
int ans2 = Find(c1[pos-]); if(p < q || (p == q && ans1>ans2))
return ans1;
return ans2;
} int main()
{
int T, n, m; scanf("%d", &T); while(T --)
{
scanf("%d", &n); memset(a, , sizeof(a));
memset(b, , sizeof(b));
memset(c, , sizeof(c)); for(int i=; i<=n; i++)
{
scanf("%d", &a[i]);
b[i] = b[i-]^a[i];
} k = ; for(int i=; i<=n; i++)/// the length is i;
{
for(int j=; i+j-<=n; j++)/// from j to j+i;
{
int num = b[j+i-]^b[j-];
c[k].len = i;
c[k++].num = num;
}
} sort(c, c+k, cmp); for(int i=; i<k; i++)
{
c1[i] = c[i].num;
c2[i] = c[i].len;
} scanf("%d", &m); for(int i=; i<=m; i++)
{
int x; scanf("%d", &x); int ans = solve(x); printf("%d\n", ans);
}
printf("\n");
}
return ;
}
上一篇:JSON 的标准:双引号而非单引号!


下一篇:C#设计模式(10)——桥接模式