字典树求字符串前缀+思维 [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 <= 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.

Memory Limit: 32MB

POINTS: 270

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

信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(1≤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 b_i followed by b_i space-separated 0’s and 1’s

  • Lines M+2…M+N+1: Line M+j+1 describes codeword j with an integer c_j followed by c_j 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

字典树+思维;

刚开始想维护val数组,记录每个字符串出现的次数;然后当一个要查找的字符串结束时,再以这个字符串的结点做为根进行dfs,找到其子树的val值;
思路是对的,但是超时;

超时代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=500100;
const LL mod=1e9+7;
int tr[M][2],val[M];
int rt;
inline void add(string s){
	int k=0;
	int len=s.length();
	for(int i=0;i<len;i++){
		int d=s[i]-'0';
		if(!tr[k][d]) tr[k][d]=++rt;
		k=tr[k][d];
	}
	val[k]++;
}
inline int dfs(int p){
	//if(tr[p][0]==0&&tr[p][1]==0) return val[p];
	int s=0;
	if(tr[p][0]) s+=dfs(tr[p][0])+val[tr[p][0]];
	if(tr[p][1]) s+=dfs(tr[p][1])+val[tr[p][1]];
	return s;
}
inline int ask(string s){
	int sum=0;
	int k=0;
	int len=s.length();
	for(int i=0;i<len;i++){
		int d=s[i]-'0';
		if(!tr[k][d]) return sum;
		k=tr[k][d];
		sum+=val[k];
	}
	sum+=dfs(k);
	return sum;
}
int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m>>n;
	for(int i=1;i<=m;i++){
		int a;
		string s="";
		cin>>a;
		for(int j=1;j<=a;j++){
			string c;
			cin>>c;
			s+=c;
		}
		add(s);
	}
	for(int i=1;i<=n;i++){
		int a;
		string s="";
		cin>>a;
		for(int j=1;j<=a;j++){
			string c;
			cin>>c;
			s+=c;
		}
		cout<<ask(s)<<endl;
	} 
	return 0;
} 

再看了题解后,顿时傻逼了,原来可以这么简单,充分体现了思维的重要性;

val1为记录以某个结点为尾结点的字符串数,val2记录经过某个结点的字符串数;
那么val2[k]-val1[k]就等于k结点之后的字符串数,而不要dfs;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=500100;
const LL mod=1e9+7;
int tr[M][2],val1[M],val2[M];//val1为记录以某个结点为尾结点的字符串数,val2记录经过某个结点的字符串数; 
int rt;
inline void add(string s){
	int k=0;
	int len=s.length();
	for(int i=0;i<len;i++){
		int d=s[i]-'0';
		if(!tr[k][d]) tr[k][d]=++rt;
		k=tr[k][d];
		val2[k]++; 
	}
	val1[k]++;
}
inline int ask(string s){
	int sum=0;
	int k=0;
	int len=s.length();
	for(int i=0;i<len;i++){
		int d=s[i]-'0';
		if(!tr[k][d]) return sum;
		k=tr[k][d];
		sum+=val1[k];
	}
	return sum+val2[k]-val1[k];
}
int main(){
	ios::sync_with_stdio(false);
	int m,n;
	cin>>m>>n;
	for(int i=1;i<=m;i++){
		int a;
		string s="";
		cin>>a;
		for(int j=1;j<=a;j++){
			string c;
			cin>>c;
			s+=c;
		}
		add(s);
	}
	for(int i=1;i<=n;i++){
		int a;
		string s="";
		cin>>a;
		for(int j=1;j<=a;j++){
			string c;
			cin>>c;
			s+=c;
		}
		cout<<ask(s)<<endl;
	} 
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是P4087 [USACO17DEC]Milk Measurement的c++代码: ```c++ #include<bits/stdc++.h> using namespace std; int n,d,i,x,minn=1e9,maxn=-1e9,sum=7;//注意sum要初始化为7,因为一开始有三个人挤奶! map<int,int> mp; struct node{ int day,milk,id;//day表示某一天,milk表示这一天的产奶量,id表示这头牛的编号 }a[100010]; bool cmp(node x,node y){ return x.day<y.day; } int main(){ scanf("%d%d",&n,&d); for(i=1;i<=n;i++){ scanf("%d%d%d",&a[i].day,&a[i].id,&a[i].milk); minn=min(minn,a[i].id);//记录最小的牛的编号 maxn=max(maxn,a[i].id);//记录最大的牛的编号 } sort(a+1,a+n+1,cmp);//排序 for(i=1;i<=n;i++){ int p=a[i].id; mp[p]+=a[i].milk;//记录每头牛产奶总量 if(mp[p]-a[i].milk>=mp[minn]&&mp[p]>=mp[minn]){//如果这头牛的产奶总量减去这一天的产奶量后等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 sum--; } if(mp[p]>=mp[maxn]&&mp[p]-a[i].milk<mp[maxn]){//如果这头牛的产奶总量大于等于最大产奶量且这头牛的产奶总量减去这一天的产奶量小于最大产奶量 sum++; } if(mp[p]-a[i].milk<mp[maxn]&&mp[p]>=mp[maxn]){//如果这头牛的产奶总量减去这一天的产奶量小于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]-mp[p]+a[i].milk>0)sum++; } mp[p]-=a[i].milk;//减去这一天的产奶量 if(i==n||a[i].day!=a[i+1].day){//如果到了新的一天或者到了最后一天 if(mp[maxn]!=mp[a[i].id]&&mp[a[i].id]>=mp[maxn])sum++;//如果这头牛的产奶总量不等于最大产奶量且这头牛的产奶总量大于等于最大产奶量 if(mp[maxn]==mp[a[i].id]){//如果这头牛的产奶总量等于最大产奶量 if(a[i].id==maxn)sum+=0;//如果这头牛就是最大产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } if(mp[minn]!=mp[a[i].id]&&mp[a[i].id]>=mp[minn])sum++;//如果这头牛的产奶总量不等于最小产奶量且这头牛的产奶总量大于等于最小产奶量 if(mp[minn]==mp[a[i].id]){ if(a[i].id==minn)sum+=0;//如果这头牛就是最小产奶量的牛,那么不需要增加计数器 else sum++;//否则需要增加计数器 } } } printf("%d\n",sum); return 0; } ``` 该题的解题思路是模拟,需要注意细节问题。我们可以首先将输入的数据按天数排序,然后模拟每一天挤奶的情况,并根据题目要进行计数即可。具体细节请见代码注释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值