zoukankan      html  css  js  c++  java
  • 2593: Secret Message(字典树)

    2593: Secret Message 分享至QQ空间

    时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte
    总提交: 10            测试通过:3

    描述

     

    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.

    输入

     

    * 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..N: Line j: The number of messages that the jth codeword could match.

    样例输入

     

    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

    题目来源

     

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <unordered_map>
     6 #define ull unsigned long long   //溢出部分自动取余
     7 using namespace std;
     8 
     9 int n,m,tot;   //tot记录下标的
    10 const int maxn=5e5+5;
    11 int trie[maxn<<2][5],sum[maxn<<2],num[maxn<<2];
    12 
    13 void Insert(int t){
    14     int root=0;
    15     for(int i=1,d;i<=t;i++){
    16         cin>>d;
    17         if(!trie[root][d]) trie[root][d]=++tot;
    18         sum[root]++;   //用来记录有几个
    19         root=trie[root][d];
    20     }
    21     num[root]++;  //以这个根结尾的字符串有多少个
    22 
    23 }
    24 
    25 int Search(int t){
    26     int root=0,res=0;
    27     for(int i=1,d;i<=t;i++){
    28         cin>>d;
    29         if(!trie[root][d]){
    30             for(int j=i+1,d;j<=t;j++) cin>>d;
    31             return res;
    32         }
    33         root=trie[root][d];
    34         res+=num[root];
    35     }
    36     return res+sum[root];
    37 }
    38 
    39 int main(){
    40     ios::sync_with_stdio(false);
    41     cin>>n>>m;
    42     for(int i=1,d;i<=n;i++){
    43         cin>>d;
    44         Insert(d);
    45     }
    46     for(int i=1,d;i<=m;i++){
    47         cin>>d;
    48         cout << Search(d) << endl;
    49     }
    50     return 0;
    51 }
    View Code
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <unordered_map>
     6 #define ull unsigned long long   //溢出部分自动取余
     7 using namespace std;
     8 
     9 int n,m,tot;   //tot记录下标的
    10 const int maxn=5e5+5;
    11 int trie[maxn<<2][5],sum[maxn<<2],num[maxn<<2];
    12 
    13 void Insert(int t){
    14     int root=0;
    15     for(int i=1,d;i<=t;i++){
    16         cin>>d;
    17         if(!trie[root][d]) trie[root][d]=++tot;
    18         sum[trie[root][d]]++;   //用来记录有几个
    19         root=trie[root][d];
    20     }
    21     num[root]++;  //以这个根结尾的字符串有多少个
    22 
    23 }
    24 
    25 int Search(int t){
    26     int root=0,res=0;
    27     for(int i=1,d;i<=t;i++){
    28         cin>>d;
    29         if(!trie[root][d]){
    30             for(int j=i+1,d;j<=t;j++) cin>>d;
    31             return res;
    32         }
    33         if(i!=t)   //不然的话会多加
    34         res+=num[trie[root][d]];
    35         root=trie[root][d];
    36     }
    37     return res+sum[root];   //当上面的字符串比下面的字符串长时
    38 }
    39 
    40 int main(){
    41     ios::sync_with_stdio(false);
    42     cin>>n>>m;
    43     for(int i=1,d;i<=n;i++){
    44         cin>>d;
    45         Insert(d);
    46     }
    47     for(int i=1,d;i<=m;i++){
    48         cin>>d;
    49         cout << Search(d) << endl;
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    ACMICPC 2009 China Northeast Area Contest :(
    2576 数论
    sql source control and sql prompt
    C语言中的单精度双精度数的有效位数
    [软件调试学习笔记]防止栈缓冲区溢出的基于Cookie的安全检查机制
    A tiny introduction to asynchronous IO
    Mysql扩展之replication概述
    C语言中的单精度双精度数的有效位数
    MySQL Cluster(MySQL 集群) 初试
    MySQL Cluster(MySQL 集群) 初试
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11703057.html
Copyright © 2011-2022 走看看