[BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)

传送门

看到前缀就要想到字典树!

看到前缀就要想到字典树!

看到前缀就要想到字典树!

#include <cstdio>
#include <iostream>
#define N 500001

int n, m, k, cnt;
int a[N], val[N], num[N], next[N][2];

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
	return x * f;
}

inline void insert()
{
	int i, now = 0;
	for(i = 1; i <= k; i++)
	{
		if(!next[now][a[i]])
			next[now][a[i]] = ++cnt;
		now = next[now][a[i]];
		num[now]++;
	}
	val[now]++;
}

inline int query()
{
	int i, now = 0, ans = 0;
	for(i = 1; i <= k; i++)
	{
		if(!next[now][a[i]])
			return ans;
		now = next[now][a[i]];
		ans += val[now];
	}
	return ans + num[now] - val[now];
}

int main()
{
	int i, j;
	n = read();
	m = read();
	for(i = 1; i <= n; i++)
	{
		k = read();
		for(j = 1; j <= k; j++) a[j] = read();
		insert();
	}
	for(i = 1; i <= m; i++)
	{
		k = read();
		for(j = 1; j <= k; j++) a[j] = read();
		printf("%d\n", query());
	}
	return 0;
}

  

时间: 2024-08-28 01:50:09

[BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)的相关文章

BZOJ1590: [Usaco2008 Dec]Secret Message 秘密信息

给n<=50000条01串,m<=50000个询问,每次给出一个01串求有多少个n条中有多少是它的前缀以及它是多少条的前缀. 前缀?Trie!匹配时记一路上单词节点的总量加上最后一个节点子树中单词节点总量即可. 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<math.h> 5 //#include<iostream> 6 using

1590: [Usaco2008 Dec]Secret Message 秘密信息

1590: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 5 Sec  Memory Limit: 32 MBSubmit: 209  Solved: 143[Submit][Status][Discuss] Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l<bi≤10000)位.他同时知

bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息

1590: [Usaco2008 Dec]Secret Message 秘密信息 Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l<bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位. 对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多

【BZOJ】1590: [Usaco2008 Dec]Secret Message 秘密信息

Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l<bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位. 对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小

【bzoj1590】【Usaco2008 Dec】秘密消息Secret Message

题目描述 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l<bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位. 对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小者. 在输入文

P2922 [USACO08DEC]秘密消息Secret Message

P2922 [USACO08DEC]秘密消息Secret Message 题目描述 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 <= 1

洛谷 P2922 [USACO08DEC]秘密消息Secret Message

题目描述 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 &

[Trie] USACO08DEC 秘密消息Secret Message

题目描述 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 &

[BZOJ1607][Usaco2008 Dec]Patting Heads 轻拍牛头

1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 2590  Solved: 1361[Submit][Status][Discuss] Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号奶牛与1号奶牛相邻.农夫约翰用很多纸条装