题目描述
Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret binary messages to each other.
Ever the clever counterspy, Farmer John has intercepted the first b_i (1 <= b_i <= 10,000) bits of each of M (1 <= M <= 50,000) of these secret binary messages.
He has compiled a list of N (1 <= N <= 50,000) partial codewords that he thinks the cows are using. Sadly, he only knows the first cj(1<=cj<=10,000)c_j (1 <= c_j <= 10,000)cj(1<=cj<=10,000) bits of codeword j.
For each codeword j, he wants to know how many of the intercepted messages match that codeword (i.e., for codeword j, how many times does a message and the codeword have the same initial bits). Your job is to compute this number.
The total number of bits in the input (i.e., the sum of the b_i and the cjc_jcj) will not exceed 500,000.
Memory Limit: 32MB
POINTS: 270
贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.
信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条暗号.但是,他仅仅知道第J条暗号的前cj(1≤cj≤10000)位.
对于每条暗号J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条暗号有着相同的前缀.当然,这个前缀长度必须等于暗号和那条信息长度的较小者.
在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.
输入格式
Line 1: Two integers: M and N
Lines 2…M+1: Line i+1 describes intercepted code i with an integer bib_ibi followed by bib_ibi space-separated 0’s and 1’s
Lines M+2…M+N+1: Line M+j+1 describes codeword j with an integer cjc_jcj followed by cjc_jcj space-separated 0’s and 1’s
输出格式
Lines 1…M: Line j: The number of messages that the jth codeword could match.
输入输出样例
输入 #1
4 5
3 0 1 0
1 1
3 1 0 0
3 1 1 0
1 0
1 1
2 0 1
5 0 1 0 0 1
2 1 1
输出 #1
1
3
1
1
2
解释:字段树,我们在树上维护经过这条路径的个数,以及以它为终点的个数,最终答案就是以经过路径中为终止的节点,以及经过暗号路径的数量求和,同时特殊处理一下,刚好完全匹配的情况,减去重复计算.
#include<iostream>
#define N 200005
using namespace std;
struct node{
node *tree[2];
int v,num;
};
void free(node *rt){
if(!rt) return;
free(rt->tree[0]);free(rt->tree[1]);
delete(rt);
}
void init(node *rt){
rt->v=0;
rt->tree[0]=rt->tree[1]=NULL;
rt->num=0;
}
void insert(node *rt,int *a,int num){
node *temp=rt;
for(int i=1;i<=num;i++){
if(temp->tree[a[i]]==NULL){
temp->tree[a[i]]=new node;
init(temp->tree[a[i]]);
}
temp=temp->tree[a[i]];temp->num++;
}
temp->v++;
}
int query(node *rt,int *a,int num){
int ret=0;
int temp_ret=0,i=0;
node *temp=rt;
for(i=1;i<=num;i++){
if(temp->tree[a[i]]==NULL) break;
temp=temp->tree[a[i]];ret+=temp->v;
}
if(i>num) ret+=temp->num-temp->v;
return ret;
}
node *root=new node;
int n=0,m=0;
int a[10005]={0};
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1,l=0;i<=n;i++){
cin>>l;for(int j=1;j<=l;j++) cin>>a[j];
insert(root,a,l);
}
for(int i=1,l=0;i<=m;i++){
cin>>l;for(int j=1;j<=l;j++) cin>>a[j];
cout<<query(root,a,l)<<endl;
}
free(root);
return 0;
}
本文介绍了一种用于匹配二进制信息的算法,旨在帮助分析员Farmer John确定其截获的部分信息与奶牛使用的暗号之间的匹配度。算法通过构建树结构来存储和查询信息,以高效地计算出每个暗号可能匹配的信息数量。
322

被折叠的 条评论
为什么被折叠?



