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 ...
随机推荐
- 仿QQ大战—服务器的搭建(ServerSocket)
ServerSocket(服务器): ServerSocket是JAVA中提供的用于建立服务器的类: 在客户/服务器通信模式中, 服务器端需要创建监听端口的 ServerSocket, ServerS ...
- css-margin与百分数的关系
可以对元素的margin设置百分数,百分数是相对于父元素的width计算,不管是margin-top/margin-bottom还是margin-left/margin-right.(padding同 ...
- MapReduce实现手机上网日志分析(排序)
一.背景 1.1 流程 实现排序,分组拍上一篇通过Partitioner实现了. 实现接口,自动产生接口方法,写属性,产生getter和setter,序列化和反序列化属性,写比较方法,重写toStri ...
- VsFtpd配置文件详解
1.默认配置:1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES2>匿名用户使用的登陆名为ftp或anonymou ...
- 11月13日上午ajax返回数据类型为JSON数据的处理
ajax返回数据类型为JSON数据的处理 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- thinkphp3.2与phpexcel基础生成
public function ff(){ import("Org.Util.PHPExcel"); import("Org.Util.PHPExcel.Workshee ...
- List拆分成多个集合
如果对一组大的集合进行操作,想分组进行,比如批量新增10000条数据,想100条分成一个集合分成100个集合,对集合进行操作100次,用C#如何编写,这里记录下代码如下 //构造被分隔的集合 List ...
- hibernate4学习
1. 安装hibernatetools插件 2. 这个是篇测试文档 来自为知笔记(Wiz)
- div内填内容
<div contenteditable="true">可以编辑里面的内容</div> 这样就可以使div想textarea一样 可以加入自动换行与over ...
- MSSQL-to-MySQL v5.3, 从MSSQL迁移到mySQL的最佳工具
将现有的MSSQL数据库迁移到MySQL数据库,尝试了很多种工具 MySQL Workbench / MSSQL to MySQL Export / DB Converter / openDBcopy ...