• 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
  • 相关阅读:
    netty系列之:netty中的核心MessageToMessage编码器
    【无标题】
    effective c++ 41 隐式接口和编译器多态
    对数几率回归的损失函数,线性回归损失函数公式
    战队集结 蓄势而发 | 网安朝阳·西门子白帽黑客大赛
    深入协议栈了解TCP的三次握手、四次挥手、CLOSE-WAIT、TIME-WAIT。
    uniapp: 实现pdf预览功能
    Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
    Day14--商品详情-渲染商品详情的数据并优化详情页的显示
    当两界交汇:前端开发、后端开发与全栈开发的对比与选择
  • 原文地址:https://blog.csdn.net/qq_36018057/article/details/126760483