传送门

思路:

  还是比较水的(不看题解不看书),用 vis 存字典树上的每个点是多少个单词的前缀,bo 来存每个点是多少个单词的结尾(坑点:会有很多相同的单词,不能只有 bool 来存)。统计时:① 如果匹配串的长度小于单词的长度,ans+= vis;② 如果匹配串的长度≥单词长度,ans+=bo;③ 如果遇到字典树上的节点为空,直接返回。

标程:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stack>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define maxn 500002
typedef long long LL;
LL n,m,len,cnt=,ans=;
LL a[maxn],ch[maxn][],vis[maxn],bo[maxn];
inline LL read()
{
LL kr=,xs=;
char ls;
ls=getchar();
while(!isdigit(ls))
{
if(!(ls^))
kr=-;
ls=getchar();
}
while(isdigit(ls))
{
xs=(xs<<)+(xs<<)+(ls^);
ls=getchar();
}
return xs*kr;
}
inline void insert()
{
LL u=;
for(LL i=;i<=len;i++)
{
if(!ch[u][a[i]]) ch[u][a[i]]=++cnt;
u=ch[u][a[i]];
vis[u]++;
}
bo[u]++;
}
inline void find()
{
LL u=;
for(LL i=;i<=len;i++)
{
if(!ch[u][a[i]]) return;
u=ch[u][a[i]];
if(bo[u]&&i<len) ans+=bo[u];
}
ans+=vis[u];
}
int main()
{
freopen("S.in","r",stdin);
freopen("S.out","w",stdout);
n=read();m=read();
for(LL i=;i<=n;i++)
{
len=read();
for(LL j=;j<=len;j++)
a[j]=read();
insert();
}
for(LL i=;i<=m;i++)
{
ans=;
len=read();
for(LL j=;j<=len;j++)
a[j]=read();
find();
printf("%lld\n",ans);
}
return ;
}

P2922 [USACO08DEC]秘密消息Secret Message的更多相关文章

  1. 洛谷p2922[USACO08DEC]秘密消息Secret Message

    题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...

  2. 洛谷 P2922 [USACO08DEC]秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  3. 【题解】P2922 [USACO08DEC]秘密消息Secret Message

    \(\text{Tags}\) 字典树,统计 题意: 给出两组\(\text{0/1}\)串\(\text{A,B}\),求解\(\text{A}\)中某串是\(\text{B}\)中某串的前缀,和\ ...

  4. Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树

    本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...

  5. 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]

    洛谷传送门,BZOJ传送门 秘密消息Secret Message Description     贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.     信息是二进制的,共有M(1≤M≤5 ...

  6. [USACO08DEC] 秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  7. [USACO08DEC] 秘密消息Secret Message (Trie树)

    题目链接 Solution Trie 树水题. 直接将前面所有字符串压入Trie 中. 在查询统计路上所有有单词的地方和最后一个地方以下的单词数即可. Code #include<bits/st ...

  8. 洛谷 2922 BZOJ 1590 [USACO08DEC]秘密消息Secret Message

    [题意概述] 给出n个01串组成的字典和m个询问,每次询问某个01串和多少个字典中的串有相同的前缀.(前缀长度是两串中较小的部分) [题解] 直接上Trie树即可.树上每个节点记录两个信息:这个节点有 ...

  9. Luogu2922 [USACO08DEC]秘密消息Secret Message (Trie树)

    统计以节点\(i\)结尾的数量与经过的数量 #include <iostream> #include <cstdio> #include <cstring> #in ...

随机推荐

  1. 日志系统的 ELK 的搭建

    https://www.cnblogs.com/yuhuLin/p/7018858.html 快速搭建ELK日志分析系统 一.ELK搭建篇 官网地址:https://www.elastic.co/cn ...

  2. 程序员调 Bug 的样子,非常真实

    程序员调 Bug 的样子,非常真实

  3. RoR - Introduction to Active Record

    Active Record: ORM ( Object-relational Mapping)Bridges the gap between relational databases , which ...

  4. 洛谷P1021邮票面值设计 [noip1999] dp+搜索

    正解:dfs+dp 解题报告: 传送门! 第一眼以为小凯的疑惑 ummm说实话没看标签我还真没想到正解:D 本来以为这么多年前的noip应该不会很难:D 看来还是太菜了鸭QAQ 然后听说题解都可以被6 ...

  5. PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别

    $_SERVER['SCRIPT_FILENAME']          -------> 当前执行程序的绝对路径及文件名__FILE__                             ...

  6. docker基本知识

  7. sql server创建windows账户

    --不要干坏事 sql server中使用xp_cmdshell --1.允许配置高级选项 GO RECONFIGURE GO --2.开启xp_cmdshell服务 RECONFIGURE GO - ...

  8. jenkins和svn搭建自动代码构建发布

    jenkins安装和配置 .安装jenkins .yum install java wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins. ...

  9. element-ui table表格展开行每次只能展开一行

    https://www.jianshu.com/p/a59c22202f2c <template> <el-table @expand-change="expandSele ...

  10. 【UML】-NO.45.EBook.5.UML.1.005-【UML 大战需求分析】- 通讯图(Communication Diagram)

    1.0.0 Summary Tittle:[UML]-NO.45.EBook.1.UML.1.005-[UML 大战需求分析]- 通讯图(Conmunication Diagram) Style:De ...