P2922 [USACO08DEC]Secret Message G

在这里插入图片描述
题意:给定两个字符串(01)集合 S , T S,T S,T.
对于 T T T中的每个字符串 T i T_i Ti,问 S S S中有多少个字符串和它匹配
思路:考虑下限制条件, ∑ b i + c i \sum b_i+c_i bi+ci比较小.
对S建字典树,对于每个 T i T_i Ti暴力匹配.
在匹配的时候 , 公共前缀会造成重复统计 , 如果直接统计匹配路径上的 c n t 在匹配的时候,公共前缀会造成重复统计,如果直接统计匹配路径上的cnt 在匹配的时候,公共前缀会造成重复统计,如果直接统计匹配路径上的cnt
比如说 S = 010 , T = 010111 比如说S=010,T=010111 比如说S=010,T=010111,这样路径上的 010 会被算作三次 , 但事实上只算 1 次 , 010会被算作三次,但事实上只算1次, 010会被算作三次,但事实上只算1,
所以还需要一个数组 e n d [ p ] end[p] end[p]表示在 p p p这个位置终结的字符串有多少个.
我们只统计两个部分的字符串
1. e n d [ p ] end[p] end[p]统计这个,必然避免了一个前缀统计多次的情况,而且也不会漏掉
2.对于S中前缀长度很长的,也就是反过来T变成了S的前缀的情况
那么这部分的数目是 c n t [ p ] − e n d [ p ] cnt[p]-end[p] cnt[p]end[p],在此时我们不能计算情况1的情况,因为它被纳入了 c n t [ p ] cnt[p] cnt[p]

/*
I don't wanna be left behind,Distance was a friend of mine.
Catching breath in a web of lies,I've spent most of my life.
Catching my breath letting it go turning my cheek for the sake of this show
Now that you know this is my life I won't be told what's supposed to be right
Catch my breath no one can hold me back I ain't got time for that.
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int INF = 1e9+7;
typedef long long ll;
typedef pair<int,int> pii;
#define all(a) (a).begin(), (a).end()
#define pb(a) push_back(a)
vector<int> G[maxn];
int tr[maxn][2];int cnt[maxn],idx = 0;
int ed[maxn];
void insert(vector<int> &s){
	int p = 0, n=s.size();
	for(int i=0;i<n;i++){
		int c = s[i];
		if(!tr[p][c]) tr[p][c] = ++idx;
		p = tr[p][c];
		cnt[p]++;
	}
	ed[p]++;
}
int find(vector<int> &s){
	int p = 0,n =s.size();
	int res = 0;
	for(int i=0;i<n;i++){
		int c = s[i];
		if(!tr[p][c]) return res;
		p = tr[p][c];
		res+=ed[p];
	}
	return res+cnt[p]-ed[p];
}
int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int M,N;cin>>M>>N;
	for(int i=0;i<M;i++){
		int sz;cin>>sz;vector<int> tp(sz);
		for(int j=0;j<sz;j++){
			cin>>tp[j];
		}
		insert(tp);
	}
	for(int i=0;i<N;i++){
		int sz;cin>>sz;vector<int> tp(sz);
		for(int j=0;j<sz;j++) cin>>tp[j];
		cout<<find(tp)<<"\n";
	}
}
/*
1 1
3 1 1 1
2 1 1
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

minato_yukina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值