[USACO08DEC]秘密消息Secret Message——[trie]

【原题】
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 c_j (1 <= c_j <= 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 c_j) will not exceed 500,000.

【题目描述】
贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.

信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位.

对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小者.

在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.

【输入格式】
第一行m,n
接下来m行表示奶牛的全部信息
每行首先一个整数k,表示这条信息有几位,接下来k个二进制数表示信息(全部以空格隔开)
接下来n行表示约翰截得的信息,格式同上

【输出格式】
对于约翰截得的每条信息,输出与此条信息有相同前缀的奶牛信息条数。

Sample  InputSample~~InputSample  Input

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

Sample  OutputSample~~OutputSample  Output

1
3
1
1
2

【题意分析】

很明显的trie,而且更加简化了

需要注意:这里的满足条件的前缀可以是比待搜索信息长的,也可以是比带搜索信息短的。

我们定义end[pos]表示正好落在pos的字符串个数
pass[pos]表示路过pos并且往下延伸的字符串个数
若该查询信息最后一位落在pos,那么答案就是从根到father[pos]所有的end[i]pass[pos]之和

直接建树查询即可

Code :

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#define read(a) a = SlowRead <int> ()
#define rep(x,y,z) for (register int x = y; x <= z; x++)
#define MAXN 600000
using namespace std;

int s[MAXN], n, q, k;

template <typename T> inline T SlowRead () {
	register int s = 0, w = 1;
	register char ch = getchar ();
	while (! isdigit (ch)) {if (ch == '-') w = -1; ch = getchar ();}
	while (isdigit (ch)) {s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar ();}
	return s * w;
}

struct trie {
	
	int size, son[MAXN][3], end[MAXN], pass[MAXN];
	
	void clear () {
		size = 0;
		memset (pass, 0, sizeof (pass));
		memset (end, 0, sizeof (end));
		memset (son, -1, sizeof (son));
	}
	
	inline void insert (int s[], int len) {
		int pos = 0;
		rep (i, 1, len) {
			if (son[pos][s[i]] == -1) son[pos][s[i]] = ++size;//new node
			pos = son[pos][s[i]];
			pass[pos]++; //路过++
		}
		end[pos]++; //结尾++
	}
	
	inline int search (int s[], int len) {
		int pos = 0, ans = 0;
		rep (i, 1, len) {
			if (son[pos][s[i]] == -1) return ans;
			pos = son[pos][s[i]]; 
			ans += end[pos]; //加上路径上的end[]之和
		}
		return ans - end[pos] + pass[pos];
		//减去多算的end[pos],加上后面的所有字符串个数pass[pos]
	}
	
}tree;

int main () {
	read (n), read (q), tree.clear ();
	rep (i, 1, n) {
		read (k); rep (j, 1, k) read (s[j]);
		tree.insert (s, k);
	}
	rep (i, 1, q) {
		read (k); rep (j, 1, k) read (s[j]);
		printf ("%d\n", tree.search (s, k));
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值