• 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
    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
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
  • 相关阅读:
    Spring之AOP原理及总结
    基于SpringBoot+Vue的疫苗接种管理系统
    黑马旅游网-配置项目(一)
    18-Spring AOP源码分析-AOP与BeanPostProcessor处理器
    【环境搭建】Apache ZooKeeper 3.8.4 Stable
    淘宝商品销量接口/淘宝商品销量监控接口/商品累计销量接口代码对接分享
    第五章 图像处理
    【重识云原生】第四章云网络4.7.9节——NFV
    虚拟机Ubuntu18.04安装对应ROS版本详细教程!(含错误提示解决)
    直接插入排序(C++实现)
  • 原文地址:https://blog.csdn.net/qq_36018057/article/details/126760483