题意:

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. Android4.2.2 Gallery2源码分析(5)——GLCanvasImpl.java

    GLCanvasImpl.java是接口GLCanvas的唯一实现类,也就是说二者在功能上完全等同.代码中调用GLCanvas对象函数的地方,等效于调用GLCanvasImpl中的该函数,GLCanv ...

  2. [React] Preventing extra re-rendering with function component by using React.memo and useCallback

    Got the idea form this lesson. Not sure whether it is the ncessary, no othere better way to handle i ...

  3. [AngularJS] Angular 1.3 new $q constructor

    <!DOCTYPE html> <html ng-app="app"> <head lang="en"> <meta ...

  4. 虚拟机chrome os 没有可用网络错误

    从http://chromeos.hexxeh.net/ 下载了一个chrome os的VM版本的,在VM9上打开运行,一直提示没有可用网络 解决方案 查看虚拟机的网络设置设置为 NAT方式 查看主机 ...

  5. 谷歌Gmail 加速

    由于某些原因的限制,我们使用谷歌的Gmail服务时,网络加载总是很慢!如下修复 一:修改hosts文件 ping -c g.cn      得到ip地址 在hosts文件里面 添加  上面的 ip地址 ...

  6. OpenJudge百炼习题解答(C++)--题4010:2011

    题: 总时间限制:  1000ms  内存限制:  65536kB 描写叙述 已知长度最大为200位的正整数n.请求出2011^n的后四位. 输入 第一行为一个正整数k,代表有k组数据,k<=2 ...

  7. css实现九宫格

    原理:浮动+margin负边距 示例代码: <!DOCTYPE html> <html lang="zh"> <head> <meta c ...

  8. LDAP编辑器 LDAPAdmin

    LDAPAdmin 是一个在 Windows 用来编辑 LDAP 账户信息的管理工具,采用 Delphi 开发.

  9. 主从复制时报:ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in t

    centos 6.5 mysql5.7 在从库作stop slave时报: error:ERROR 1794 (HY000): Slave is not configured or failed to ...

  10. PHP-Yii执行流程分析(源码)

    转自:http://www.cnblogs.com/zhanghaoyong/articles/2659846.html   一 目录文件 |-framework     框架核心库 |--base  ...