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

题目描述

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

信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位.

对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小者.

在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.

输入

第1行输入N和M,之后N行描述秘密信息,之后M行描述密码.每行先输入一个整数表示信息或密码的长度,之后输入这个信息或密码.所有数字之间都用空格隔开.

输出

共M行,输出每条密码的匹配信息数.

样例输入

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
3
1
1
2 


题解

先把信息建成字典树。每个信息的结尾打上标记,并记录以该节点结尾的有多少个。查询的时候,如果是前缀等于信息的情况,那么就返回在字典树上经过的标记的数目,如果是前缀等于密码的情况,那么返回密码的最后一个字符的子树中的标记数。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long

const int maxn=50000*10;

int t[maxn][2],n,m,len,root,tot,cnt[maxn],num[maxn];
char c[500000+50];
bool word[maxn];

void insert(char *s,int r){
    root=0;int id;
    for(int i=0;i<r;i++){
        id=s[i]-‘0‘;
        if(!t[root][id]) t[root][id]=++tot;
        cnt[root]++;root=t[root][id];
    }
    word[root]=true;num[root]++;
}

int find(char *s,int r){
    root=0;int id,ans=0;
    for(int i=0;i<r;i++){
        id=s[i]-‘0‘;
        if(!t[root][id]){
            return ans;
        }
        root=t[root][id];if(word[root]) ans+=num[root];
    }
    return ans+cnt[root];
}

template<typename T>void read(T& aa){
    char cc; ll ff;aa=0;cc=getchar();ff=1;
    while((cc<‘0‘||cc>‘9‘)&&cc!=‘-‘) cc=getchar();
    if(cc==‘-‘) ff=-1,cc=getchar();
    while(cc>=‘0‘&&cc<=‘9‘) aa=aa*10+cc-‘0‘,cc=getchar();
    aa*=ff;
}

int main(){
    read(m),read(n);
    for(int i=1;i<=m;i++){
        int x,w;
        read(w);for(int i=1;i<=w;i++) read(x),c[i-1]=x+‘0‘;
        insert(c,w);
    }
    for(int i=1;i<=n;i++){
        int x,w;
        read(w);for(int i=1;i<=w;i++) read(x),c[i-1]=x+‘0‘;
        printf("%d\n",find(c,w));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/rlddd/p/9785450.html

时间: 2024-08-30 02:57:35

【bzoj1590】【Usaco2008 Dec】秘密消息Secret Message的相关文章

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 &

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

[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 = getch

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,他想知道有多少截得的信息能够和它匹配.也就是说,有多

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)位.他同时知

P3102 [USACO14FEB]秘密代码Secret Code

题目描述 Farmer John has secret message that he wants to hide from his cows; the message is a string of length at least 2 containing only the characters A..Z. To encrypt his message, FJ applies a sequence of "operations" to it, where an operation ap

[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号奶牛相邻.农夫约翰用很多纸条装