Secret Message ---- (Trie树应用)
Secret Message
- 总时间限制:
- 2000ms
- 内存限制:
- 32768kB
- 描述
-
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 - 输入
- INPUT FORMAT:
* 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..M: 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 - 提示
- INPUT DETAILS:
Four messages; five codewords.
The intercepted messages start with 010, 1, 100, and 110.
The possible codewords start with 0, 1, 01, 01001, and 11.0 matches only 010: 1 match
1 matches 1, 100, and 110: 3 matches
01 matches only 010: 1 match
01001 matches 010: 1 match
11 matches 1 and 110: 2 matches - 思路: Trie树的应用,注意输入的如果不是有长度一样的,需要特殊处理一下,
- 我是先记录一下第一个点出现的个数,然后找孩子节点,找的过程中减去孩子节点的兄弟节点的个数,
- 就得出了答案。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct node{
int v;
node *next[2];
}Trie;
Trie root;
void createTrie(char *str)
{
int len = strlen(str);
Trie *p = &root, *q;
for(int i=0; i<len; ++i)
{
int id = str[i]-'0';
if(p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(root));
q->v = 1;
for(int j=0; j<2; ++j)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
p->next[id]->v++;
p = p->next[id];
}
}
} int findTrie(char *str)
{
int len = strlen(str);
Trie *p = &root;
Trie *other;
if(len==1){
return p->next[str[0]-'0']->v;
}
int total =p->next[str[0]-'0']->v;
p = p->next[str[0]-'0'];
for(int i=1;i<len;i++){
if(p->next[(str[i]-'0')^1]!=NULL){ // 下一个节点的兄弟节点不空,要减去
total=total-p->next[(str[i]-'0')^1]->v;
}
if(i==len-1){
break;
}
if(p->next[str[i]-'0']!=NULL){ //下一个节点不空,遍历
p=p->next[str[i]-'0'];
}
else if(p->next[str[i]-'0']==NULL){
break;
}
}
return total;
} int main(){
int n,m,i,j,k,ans,num;
char str[50001];
scanf("%d%d",&n,&m);
for(i=0;i<n;i++){
scanf("%d",&num);
memset(str,'0',sizeof(str));
for(j=0;j<num;j++){
scanf("%d",&k);
str[j]=k+'0';
}
str[j]='\0';
createTrie(str);
}
for(i=0;i<m;i++){
scanf("%d",&num);
memset(str,'0',sizeof(str));
for(j=0;j<num;j++){
scanf("%d",&k);
str[j]=k+'0';
}
str[j]='\0';
ans = findTrie(str);
printf("%d\n",ans);
}
return 0;
}
Secret Message ---- (Trie树应用)的更多相关文章
- 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
洛谷传送门,BZOJ传送门 秘密消息Secret Message Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤5 ...
- [USACO08DEC] 秘密消息Secret Message (Trie树)
题目链接 Solution Trie 树水题. 直接将前面所有字符串压入Trie 中. 在查询统计路上所有有单词的地方和最后一个地方以下的单词数即可. Code #include<bits/st ...
- Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树
本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...
- [Usaco2008 Dec]Secret Message 秘密信息
2794: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 3 ...
- 1590: [Usaco2008 Dec]Secret Message 秘密信息
1590: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 5 Sec Memory Limit: 32 MBSubmit: 209 Solved: ...
- [USACO 08DEC]Secret Message
Description Bessie is leading the cows in an attempt to escape! To do this, the cows are sending sec ...
- bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息
1590: [Usaco2008 Dec]Secret Message 秘密信息 Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共 ...
- 洛谷:P2922 [USACO08DEC]秘密消息(Trie树)
P2922 [USACO08DEC]秘密消息Secret Message 题目链接:https://www.luogu.org/problemnew/show/P2922 题目描述 贝茜正在领导奶牛们 ...
- 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
随机推荐
- webapi 知识点
在web api 中后台的方法必须 加入 [HttpGet] ,[HttpPost],[HttpPut],[HttpDelete] 来区分,这是一种习惯. ps: get 方式参数都存在http协议头 ...
- Java作业九(2017-11-6)
/*圆的类*/ public class R { private double radius; // 构造方法,有参构造 public R(double radius) { this.radius = ...
- 解决 spring-cloud-starter-zipkin 启动错误
应用场景:Spring Boot 服务添加 Zipkin 依赖,进行服务调用的数据采集,然后进行 Zipkin-Server 服务调用追踪显示. 示例pom.xml配置: <parent> ...
- kali linux 网络渗透测试学习笔记(二)OWASP ZAP工具扫描SQL injection漏洞失败
按照惯例,利用OWASP ZAP工具扫描SQL injection漏洞时,应该很快就可以扫描出来,但是在笔者进行扫描的时候,却遇到了以下状况: 这说明了该工具根本就没能够扫描出SQL注入的漏洞,不知道 ...
- [Swift]LeetCode466. 统计重复个数 | Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [Swift]LeetCode858. 镜面反射 | Mirror Reflection
There is a special square room with mirrors on each of the four walls. Except for the southwest cor ...
- 初步学习大数据——设置虚拟机固定ip地址
1.打开本机的网络连接 2.右键以太网,打开属性. 3.右键VMnet8,打开属性.最多不能超过255,最少不能小于0. 0~255之间. 4.找到你要设置固定IP地址的虚拟机 ,选择上方的编辑 ...
- Linux 遭入侵,挖矿进程被隐藏排查记录
今天来给大家分享下这两天遇到的一个问题,服务器被挖矿了,把我的排查记录分享下,希望能帮到有需要的同学. 问题原因 多台服务器持续告警CPU过高,服务器为K8s的应用节点,正常情况下CPU使用率都挺低的 ...
- 网络协议 9 - TCP协议(下):聪明反被聪明误
网络协议 1 - 概述 网络协议 2 - IP 是怎么来,又是怎么没的? 网络协议 3 - 从物理层到 MAC 层 网络协议 4 - 交换机与 VLAN:办公室太复杂,我要回学校 网络协议 5 - I ...
- Qt之QComboBox定制
说起下拉框,想必大家都比较熟悉,在我们注册一些网站的时候,会出现大量的地区数据供我们选择,这个时候出现的就是下拉框列表,再比如字体选择的时候也是使用的下拉框,如图1所示.下拉框到处可见,作为一个图形库 ...