题意:

f(A,B)表示:B在A中作为子串出现的次数。

题目给出n个证据,m个子弹

Ai是证据。Bi是子弹。题目问:全部Bi对每一个Ai造成的伤害是多少,即每一个Bi在Ai中出现的次数总和。

解析:

不会AC自己主动机,所以就用字典树水了一发。没想到过了。

先把全部的Bi插入字典树中。然后枚举每一个Ai的后缀,查询后缀的每一个前缀在字典树中出现了几次。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
const int MAXN = (int)1e5 + 10;
const int maxnode = (int)6e5 + 10;
const int sigma_size = 26; struct Trie {
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
void clear() { sz = 1; memset(ch[0], 0, sizeof(ch[0]));}
Trie() {clear();}
int idx(char c) { return c - 'a';} void insert(char *s, int v = 1) {
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz], 0, sizeof(ch[u]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] += v;
} ll find(const char* s) {
int u = 0, n = strlen(s);
ll ret = 0;
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) return ret;
u = ch[u][c];
ret += val[u];
}
return ret;
}
} trie; int n, m;
string A[MAXN];
char B[MAXN]; int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
trie.clear();
for(int i = 0; i < n; i++) {
cin >> A[i];
}
for(int i = 0; i < m; i++) {
scanf("%s", B);
trie.insert(B);
}
ll ans = 0;
for(int i = 0; i < n; i++) {
ans = 0;
for(int j = 0; j < A[i].size(); j++) {
ans += trie.find(A[i].c_str()+j);
}
printf("%lld\n", ans);
}
}
return 0;
}

hdu 5384 Danganronpa(字典树)的更多相关文章

  1. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  2. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

  3. hdu 2846(字典树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  5. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

  6. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  7. *hdu 5536(字典树的运用)

    Input The first line of input contains an integer T indicating the total number of test cases. The f ...

  8. HDU 5384 Danganronpa (2015年多校比赛第8场)

    1.题目描写叙述:点击打开链接 2.解题思路:本题利用字典树解决.本题要求查找全部的B[j]在A[i]中出现的总次数.那么我们能够建立一颗字典树,将全部的B[j]插入字典树,因为一个串的全部字串相当于 ...

  9. three arrays HDU - 6625 (字典树)

    three arrays \[ Time Limit: 2500 ms \quad Memory Limit: 262144 kB \] 题意 给出 \(a\),\(b\) 数组,定义数组 \(c[i ...

随机推荐

  1. ps与top命令简单介绍

    Linux中ps与top命令 这两个命令都是查看系统进程信息的命令,但是用处有点儿不同 1.ps命令--提供系统过去信息的一次性快照 也就是说ps命令能够查看刚刚系统的进程信息  命令:ps aux或 ...

  2. IIS6.0 开启Gzip与PHP Gzip

    因为在做一个项目,项目里面服务器主要提供数据,但是数据多了文件就大了,比较浪费流量和时间,我们便用Gzip来处理.我在本机上是apache,服务器上是IIS6.0,用的是php,那么我就在这里分享一下 ...

  3. 彻底领悟javascript中的exec与match方法

    exec是正则表达式的方法,而不是字符串的方法,它的参数才是字符串,如下所示: var re=new RegExp(/\d/); re.exec( "abc4def" ); //或 ...

  4. UVA11402 - Ahoy, Pirates!(线段树)

    UVA11402 - Ahoy, Pirates!(线段树) option=com_onlinejudge&Itemid=8&category=24&page=show_pro ...

  5. HDU4300-Clairewd’s message(KMP前缀匹配后缀)

    Clairewd's message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. Colours

    A colour is an object representing a combination of Red, Green, and Blue (RGB) intensity values. Val ...

  7. element-ui table 前端渲染序号 index

    1.前端渲染table 序号 2.使用element ui http://element-cn.eleme.io/#/zh-CN/component/table#zi-ding-yi-suo-yin ...

  8. 【Oracle】查看正在运行的存储过程

    select name from v$db_object_cache where locks > 0 and pins > 0 and type='PROCEDURE';

  9. mysql last_insert_id() (转载)

    先来看看官方的说明 The ID that was generated is maintained in the server on a per-connection basis. This mean ...

  10. Python translate()方法

    描述 Python translate() 方法根据 maketrans() 方法给出的字符映射转换表转换字符串中的字符. 语法 translate() 方法语法: Python3中: S.trans ...