【Trie】Secret Message 秘密信息
【题目链接】:
【题意】
我认为这个题目最难的是题意:
其实分了两种情况:
1、如果当前文本串匹配不完,那么答案的是:匹配过程中遇到的模式串结尾的个数 + 文本串结尾 的节点 ,模式串的经过该点的个数。
2、如果是文本串中途遇到问题,而不能继续匹配的情况是:匹配过程中遇到的模式串结尾的个数
只要分开这两种情况即可。
【代码】:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 5e4 ;
const int M = 5e4*;
int Son[M][];
int s[M],Sum[M],End[M];
int n,m,k,idx;
void Insert(){
int p = ;
for(int i=;i<k;i++){
if( !Son[p][s[i]] ) Son[p][s[i]] = ++idx;
p = Son[p][s[i]];
Sum[p] ++ ;
}
End[p]++;
}
void Query(){
int p = , res = ;
for(int i=;i<k;i++){
if( !Son[p][s[i]] ){
printf("%d\n",res);
return ;
}
p = Son[p][s[i]];
res += End[p];
}
res = res - End[p] + Sum[p];
printf("%d\n",res);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&k);
for(int j=;j<k;j++)
scanf("%d",&s[j]);
Insert();
}
for(int i=;i<=m;i++){
scanf("%d",&k);
for(int j=;j<k;j++)
scanf("%d",&s[j]);
Query();
}
return ;
}
【Trie】Secret Message 秘密信息的更多相关文章
- [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: ...
- bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息
1590: [Usaco2008 Dec]Secret Message 秘密信息 Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共 ...
- BZOJ1590:[Usaco2008 Dec]Secret Message秘密信息
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://lydsy.com/JudgeOnline/problem ...
- BZOJ1590 [Usaco2008 Dec]Secret Message 秘密信息
建立一颗trie树,记录下来每个点以它为结尾的字符串的个数cnt,和它的子树内有多少字符串size 于是查询的时候就只需要把沿途的cnt加起来,再加上最后的size就好了 /************* ...
- [BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)
传送门 看到前缀就要想到字典树! 看到前缀就要想到字典树! 看到前缀就要想到字典树! #include <cstdio> #include <iostream> #define ...
- 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
洛谷传送门,BZOJ传送门 秘密消息Secret Message Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤5 ...
- [USACO08DEC] 秘密消息Secret Message
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
- 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
随机推荐
- Python进行Redis数据迁移
Python进行Redis数据迁移 由于开发时的误操作,导致redis数据损坏,所以需要进行redis的数据迁移,网上大佬的教程基本都是需要下载附加工具,亦或是需要一些复杂的操作,个人觉得麻烦还不如写 ...
- PHP中部分宏应用
1.字符串复制 ZVAL_STRINGL(pzv, str, len, dup):str 和 len 分别为内存中保存的字符串地址和他的长度,dup之名该字符串是否需要被复制,值为1则将先申请一块新内 ...
- mongodb的安装及使用
1.MongoDB安装 安装包下载地址: https://www.mongodb.com/download-center/community 启动数据库:进入到mongd所在的bin目录,执行mong ...
- Vue中可用的判断对象是否为空的方法
vue有两个方法可用 1. JSON.stringify(evtValue)=='{}' 2. Object.keys(xxx).length==0 js判断对象是否为空对象的几种方法 1.将json ...
- springboot之JWT实现权限认证
1.在pom.xml添加依赖 <dependency> <groupId>com.auth0</groupId> <artifactId>java-jw ...
- Google Directions API 中路线编码解析
public List<Location> GetGeoPoints(string encoded) { List<Location> poly = new List<L ...
- 005-多线程-锁-JUC锁-LockSupport【使用、Unsafe、对比Object的wait、底层源码】
一.概述 在Java多线程中,当需要阻塞或者唤醒一个线程时,都会使用LockSupport工具类来完成相应的工作.LockSupport定义了一组公共静态方法,这些方法提供了最基本的线程阻塞和唤醒功能 ...
- rrt tree
package com.bim.rrt_20190529; import static java.lang.Math.pow;import static java.lang.Math.sqrt; im ...
- [Scikit-learn] 2.3 Clustering - kmeans
参考: 2.3. Clustering 2.4. Biclustering 2.1.2.3. The Dirichlet Process Clusering, GMM, Variational Inf ...
- @value取值
配置文件的书写 valm.DlUrl=http://14.168.55.203:5199/FOSSecMngTemplate?wsdl vals.DlUrl=http://14.168.55.203: ...