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 ...
随机推荐
- 一些js 插件的作用
前言: 从一些开源网站上下载下来的 后台管理系统模板一般会有很多的js ,其js 的功能是什么呢?这里随手查询了一下,记录下来 正文: 1.zDialog.js 各种弹窗插件详细案例:http://w ...
- C#-WebForm-WebForm开发基础、如何给控件注册事件?——事件委托写法、http无状态性、三层结构
(小知识 - xml:可扩展的标记语言 html:超文本标记语言) 一.创建WebForm:新建→网站 此时文件夹中只有一个 config 文件,打开后 二.在项目下右键添加新项 在设计页面中打开 从 ...
- AOJ DSL_2_C Range Search (kD Tree)
Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...
- JS实现网页批量下载文件,支持PC/手机
//把下载链接放入集合里 var downloadData = new Array{"http://www.empli.com/data1.apk","http://ww ...
- WinForm------TextEdit只能输入数字
代码: this.textEdit1.Properties.Mask.EditMask = @"\d+"; this.textEdit1.Properties.Mask.MaskT ...
- Liunx下查看服务器硬件信息
一.如何查看服务器的CPU 今天安装了9台Linux服务器,型号完全不一样(有DELL. HP和IBM服务器),又懒得去对清单,如何在Linux下cpu的个数和核数呢?另外,nginx的cpu工作模式 ...
- yii2 session的使用方法
yii2打开session use yii\web\Session; $session = Yii::$app->session; // check if a session is alread ...
- mate标签
<meta charset='utf-8'> <!-- 优先使用 IE 最新版本和 Chrome --> <meta http-equiv="X-UA-C ...
- [Centos]升级安装GCC
摘要 在尝试运行asp.net core站点的时候,发现了gcc包版本太低,造成一些错误.没办法只能升级gcc了. 升级 最新包:http://gcc.parentingamerica.com/rel ...
- ASP.NET WEB API路由机制
(一)路由原理 (二)路由设计架构分析 RouteBase