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 ...
随机推荐
- 【转】Java中try catch finally语句中含有return语句的执行情况(总结版)
Java中try catch finally语句中含有return语句的执行情况(总结版) 有一点可以肯定,finally块中的内容会先于try中的return语句执行,如果finall语句块中也有r ...
- 【USACO 2.3】Zero Sum(dfs)
按字典序输出所有在123..n之间插入'+','-',' '结果为0的表达式.. http://train.usaco.org/usacoprob2?a=jUh88pMwCSQ&S=zeros ...
- Map工具系列-03-代码生成BySQl工具使用说明
所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...
- elastichq auto connect
$(document).ready(function () { $('#connectionURL').focus(); ajaxloading.hide(); scrollToTop.activat ...
- Bootstrap学习笔记
Bootstrap提供了一套响应式.移动设备优先的流式栅格系统. Bootstrap把一个容器或整个网页平均分成了12列. 栅格系统必须放在.container或container-fluid中 样式 ...
- [Head First设计模式]生活中学设计模式——组合模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- postgresql利用pg_upgrade升级数据库(从8.4升级到9.5)
其他见:http://my.oschina.net/ensn/blog/636766 本文利用pg_upgrade实现将8.4.18版本升级到9.5.0版本,8.4.18版本为RedHat系统自带pg ...
- 代码质量管理工具 sonar 配置
代码检查工具有很多findBugs等等 sonar配置: 1.下载sonar 5.5, 解压,运行 sonarqube-5.5\bin\windows-x86-64\StartSonar.bat , ...
- javascript数据结构-队列
gihub博客地址 队列(Queue)是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插 ...
- PHPExcel yii2 加载使用
除了用composer 包管理组件的方式外 我们还可以使用 直接最原始的加载方式---超级简单 1.PHPExcel上下载最新的PHPExcel http://phpexcel.codeplex.co ...