HDU5880 Family View(2016青岛网络赛 AC自动机)
题意:将匹配的串用‘*’代替
tips:
1 注意内存的使用,据说g++中指针占8字节,c++4字节,所以用g++交会MLE
2 注意这种例子,
1
2
abcd
bc
abc
故失败指针要一直往下走,否则会丢弃一些串
3 当出现非英文字符时应先将指针指向根节点,否则出现
1
1
cy
c,,,,,,,y
时结果为c,,,,,,**(由不止一种Bug造成的),正确的应为c,,,,,,,y,然而很多题解这样也过了~
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
int Hash[128]; const int N = 1000008, CH = 26;
struct Trie{
Trie *next[CH];
Trie *fail;
int len;
}; int flag[N]; char str[N]; class ACauto{ Trie tree[N];
private:
int nxt;
Trie *root; public:
void init(){
root = &tree[0];
nxt=0;
memset(&tree[0], 0, sizeof(Trie));
memset(flag, 0, sizeof(flag));
} void insert(char *s){
Trie *p = root;
int i;
for(i = 0; s[i]; i++){
int c = Hash[s[i]];
if(!p -> next[c]){
memset(&tree[++nxt], 0, sizeof(Trie));
p -> next[c] = &tree[nxt];
}
p = p -> next[c];
}
//p -> cnt = 1;
//p -> cnt++;//用于统计
p -> len = i; } void build(){
queue<Trie *> q;
q.push(root);
root -> fail = NULL;
while(!q.empty()){
Trie *now = q.front();
q.pop();
for(int i = 0; i < CH; i++){
Trie *son = now -> next[i];
Trie *tp = (now == root)? root: now -> fail->next[i];
if(son == NULL){
now -> next[i] = tp;//Trie图
}else{
son -> fail = tp;
//状态合并(如一个串是另一个串的子串则值域可以合并)
//son -> cnt += son -> fail -> cnt;
q.push(son);
}
}
}
} //查询匹配次数
void query(char *s){
Trie *now = root;
for(int i = 0; s[i]; i++){
int c = Hash[s[i]];
if(c == -1){
//注意,要将now赋值为root
now = root;
continue;
}
now = now -> next[c];
Trie *p = now;
while(p != root){
if(p -> len){
flag[i - p -> len + 1]--;
flag[i + 1]++;
}
p = p -> fail;
} }
}
}ac; int main(){
fill(Hash, Hash + 128, -1);
for(int i = 'a'; i <= 'z'; i++){
Hash[i] = i - 'a';
} for(int i = 'A'; i <= 'Z'; i++){
Hash[i] = i - 'A';
} int t;
cin >>t;
while(t--){
ac.init();
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%s", str);
ac.insert(str);
}
ac.build();
getchar();
gets(str);
ac.query(str);
LL cnt = 0;
for(int i = 0; str[i]; i++){
cnt += flag[i];
if(cnt >= 0){
putchar(str[i]);
}else{
putchar('*');
} }
printf("\n");
} return 0;
}
HDU5880 Family View(2016青岛网络赛 AC自动机)的更多相关文章
- HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)
题目链接 2016 青岛网络赛 Problem C 题意 给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...
- HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)
题目链接 2016 Qingdao Online Problem I 题意 在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...
- HDU - 5878 2016青岛网络赛 I Count Two Three(打表+二分)
I Count Two Three 31.1% 1000ms 32768K I will show you the most popular board game in the Shanghai ...
- HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)
Herbs Gathering 10.76% 1000ms 32768K Collecting one's own plants for use as herbal medicines is pe ...
- HDU5887 Herbs Gathering(2016青岛网络赛 搜索 剪枝)
背包问题,由于数据大不容易dp,改为剪枝,先按性价比排序,若剩下的背包空间都以最高性价比选时不会比已找到的最优解更好时则剪枝,即 if(val + (LD)pk[d].val / (LD)pk[d]. ...
- 2016青岛网络赛 Barricade
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Proble ...
- 2016青岛网络赛 Sort
Sort Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Des ...
- 2016青岛网络赛 The Best Path
The Best Path Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Pr ...
- 2016 年青岛网络赛---Family View(AC自动机)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...
随机推荐
- 一次kibana小问题排查的过程记录
起因 中午业务组同事报告说kibana服务不能正常使用,登录kibana前端查看,网站能够访问,但是页面显示仅有title部分而无内容部分. 排查 首先确认kibana服务是否正常,登录kuberne ...
- js判断用户的浏览设备是移动设备还是PC
<script type="text/javascript"> function browserRedirect() { var sUserAgent = naviga ...
- 【原】用css让一个容器水平垂直
这种css布局平时用的比较多,也是面试题常出的一个题,网上一搜一大丢,不过还是想自己总结一下. 这种方法比较多,本文只总结其中的几种,以便加深印象. 效果图都为这个: 方法一:position加mar ...
- HTML5本地存储——IndexedDB(一:基本使用)
在HTML5本地存储——Web SQL Database提到过Web SQL Database实际上已经被废弃,而HTML5的支持的本地存储实际上变成了 Web Storage(Local Stora ...
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...
- 面试题目——《CC150》栈与队列
面试题3.1:描述如何只用一个数组来实现三个栈. 方法1:固定分割 方法2:弹性分割(较难) package cc150; public class ArrayStack { public stati ...
- 苹果手机IOS中div contenteditable=true 仿文本域无法输入编辑
问题: 在苹果手机IOS中 contenteditable="true" 做文本域输入,点击可以弹出键盘但是无法输入,安卓都正常. 经测试后,记得加一个样式 -webkit-use ...
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- 大熊君学习html5系列之------History API(SPA单页应用的必备------重构完结版)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...
- VBA笔记(一)
开启VBA编程环境--VBE 方法一:按<Alt+F11>组合建 方法二:查看代码 宏设置 当然启用宏的设置方式不同,宏的启动方式也不一样. 首先打开"office 按钮&quo ...