hdu 5384 Danganronpa(字典树)
题意:
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(字典树)的更多相关文章
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
- hdu 2846(字典树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- *hdu 5536(字典树的运用)
Input The first line of input contains an integer T indicating the total number of test cases. The f ...
- HDU 5384 Danganronpa (2015年多校比赛第8场)
1.题目描写叙述:点击打开链接 2.解题思路:本题利用字典树解决.本题要求查找全部的B[j]在A[i]中出现的总次数.那么我们能够建立一颗字典树,将全部的B[j]插入字典树,因为一个串的全部字串相当于 ...
- three arrays HDU - 6625 (字典树)
three arrays \[ Time Limit: 2500 ms \quad Memory Limit: 262144 kB \] 题意 给出 \(a\),\(b\) 数组,定义数组 \(c[i ...
随机推荐
- 高性能WEB开发:重排与重绘
DOM编程可能最耗时的地方,重排和重绘. 1.什么是重排和重绘 浏览器下载完页面中的所有组件——HTML标记.JavaScript.CSS.图片之后会解析生成两个内部数据结构——DOM树和渲染树. D ...
- zend studio 13.0.0 安装破解汉化
zend studio 13安装破解汉化步骤 官网原版下载 http://downloads.zend.com/studio-eclipse/13.0.0/ZendStudio-13.0.0-win3 ...
- android中可以使用bitmap的平铺,镜像平铺等减小图片带来的apk过大的问题
bitmap的平铺.镜像drawable文件夹中新建bitmap,其中的tileMode属性 tileMode 属性就是用于定义背景的显示模式: disabled 默认值,表示不使用平铺 cla ...
- python发送邮件方法总结
python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点. 一.相关模块介绍 发送邮件主要用到了smtplib和ema ...
- oracle 两表关联查询
oracle 两表关联查询 CreationTime--2018年7月4日17点27分 Author:Marydon 情景描述 查询学生表student,sname,sex,age信息及所在班级c ...
- Kubuntu 初始配置
1.中文配置 系统设置中地区选择中国.语系使用简体中文(一開始可能仅仅有英文选项,似乎是更新后能够选择其它语言了) 安装中文字体: sudo apt-get install ttf-wqy-micro ...
- MySQL学习记录一
1.MySQL join操作 left join以左表为基础,其记录会全部表示出来,而右表只显示满足搜索条件的记录.right join以右表为基础,其记录会全部显示出来,而左表只显示满足搜索条件的记 ...
- Linux防火墙的关闭和开启(转)
1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...
- Java程序(非web)slf4j整合Log4j2
一.依赖包准备 //slf4j项目提供 compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' //log4j2项目提供 co ...
- django.db.utils.OperationalError: no such table: auth_user
关于使用django 首次创建超级管理员时,出现 django.db.utils.OperationalError: no such table: auth_user 错误 1.首先使用命 ...