本来想找\(01Trie\)的结果找到了一堆字典树水题。。。算了算了当水个提交量好了。

直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点处的子树大小之和。

#include <bits/stdc++.h>
using namespace std; int top, sta[10010];
int n, m, l, s[10010], max_size;
int ch[500010][2], sz[500010], sum[500010]; void push_up (int p) {
sz[p] = sum[p];
if (ch[p][0]) sz[p] += sz[ch[p][0]];
if (ch[p][1]) sz[p] += sz[ch[p][1]];
} void add_str () {
int now = 0;
sta[top = 1] = 0;
for (int i = 1; i <= l; ++i) {
if (!ch[now][s[i]]) {
ch[now][s[i]] = ++max_size;
}
// printf ("ch[%d][%d] = %d\n", now, s[i], ch[now][s[i]]);
now = ch[now][s[i]];
sta[++top] = now;
}
sz[now]++;
sum[now]++;
while (top > 0) push_up (sta[top--]);
} int get_str () {
int now = 0, ans = 0;
for (int i = 1; i <= l; ++i) {
if (!ch[now][s[i]]) {
return ans;
}
// printf ("now = %d, sum[now] = %d sz[now] = %d\n", now, sum[now], sz[now]);
now = ch[now][s[i]];
ans += sum[now];
}
// printf ("now = %d\n", now);
ans += sz[now] - sum[now];
return ans;
} int main () {
cin >> m >> n;
for (int i = 1; i <= m; ++i) {
scanf ("%d", &l);
for (int j = 1; j <= l; ++j) {
scanf ("%d", &s[j]);
}
add_str ();
}
for (int i = 1; i <= n; ++i) {
scanf ("%d", &l);
for (int j = 1; j <= l; ++j) {
scanf ("%d", &s[j]);
}
cout << get_str () << endl;
}
}

Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树的更多相关文章

  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. P2922 [USACO08DEC]秘密消息Secret Message

    传送门 思路: 还是比较水的(不看题解不看书),用 vis 存字典树上的每个点是多少个单词的前缀,bo 来存每个点是多少个单词的结尾(坑点:会有很多相同的单词,不能只有 bool 来存).统计时:① ...

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

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

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

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

  7. [USACO08DEC] 秘密消息Secret Message

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

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

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

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

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

随机推荐

  1. Vue之双向数据绑定

    demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...

  2. Web API 如何请求基于Basic/Bearer 头的方式 C#

    public void SetBasicAuthHeader(WebRequest request, String userName, String userPassword) { string au ...

  3. Android PowerManager电源管理(Android N )

    ./frameworks/base/core/java/android/os/PowerManager.java该类提供给Application访问电源相关接口. 它的内部类WakeLock是定义的唤 ...

  4. 第四十九天 mysql 索引 元类

    一 昨日回顾 视图 触发器 事务 什么是事务 逻辑上的一组操作 要么都成功 要么都失败 如何使用 start transaction 开启事务 mysql 默认一条sql就是一个事务 pymysql默 ...

  5. 用牛顿-拉弗森法定义平方根函数(Newton-Raphson method Square Root Python)

    牛顿法(Newton’s method)又称为牛顿-拉弗森法(Newton-Raphson method),是一种近似求解实数方程式的方法.(注:Joseph Raphson在1690年出版的< ...

  6. hiho1258 Osu! Master

    题目链接:http://hihocoder.com/problemset/problem/1258 题目大意:看能连击的次数 思路:水 看有多少个1和s就好了 #include <stdio.h ...

  7. Add Zabbix Agent

    添加第三方源进行安装CentOS/RHEL 7:# rpm -Uvh http://repo.zabbix.com/zabbix/2.2/rhel/7/x86_64/zabbix-release-2. ...

  8. 【题解】 bzoj2982: combination (Lucas定理)

    题面戳我 Solution 板子题 Code //It is coded by ning_mew on 7.25 #include<bits/stdc++.h> #define LL lo ...

  9. nginx.conf(centos6, 1.12)主配置文件修改

    #nginx1.12 centos6.xuser admin admin;worker_processes 4; error_log /data/services/logs/nginx_error.l ...

  10. bandwagon host

    104.20.6.63 bandwagonhost.com 104.20.6.63 bwh1.net