hdu 6208 The Dominator of Strings【AC自动机】

求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了。。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
inline void read(int&a){char c;while(!(((c=getchar())>='')&&(c<='')));a=c-'';while(((c=getchar())>='')&&(c<=''))(a*=)+=c-'';}
const int N = ;
const int M = ;
struct Trie {
int next[N][M], fail[N], tag[N];
int root;
int vis[N];
int L;
int newnode() {
for(int i = ;i < M;i++)
next[L][i] = -;
vis[L] = ;
tag[L++] = ; return L-;
}
void init() {
L = ;
root = newnode();
}
void Insert(char buf[]) {
int len = strlen(buf);
int u = root;
for(int i = ; i < len; i++) {
if(next[u][buf[i]-'a'] == -)
next[u][buf[i]-'a'] = newnode();
u = next[u][buf[i]-'a'];
}
tag[u]++;
}
void build() {
queue<int> Q;
fail[root] = root;
for(int i = ; i < M; i++) {
if(next[root][i] == -)
next[root][i] = root;
else {
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while( !Q.empty() ) {
int u = Q.front();
Q.pop();
for(int i = ; i < M; i++) {
int &v = next[u][i];
if(v == -)
v = next[fail[u]][i];
else {
Q.push(v);
fail[v] = next[fail[u]][i];
}
}
}
}
int query(char buf[]) {
int len = strlen(buf);
int u = root;
int res = ;
for(int i = ; i < len; i++) {
u = next[u][buf[i]-'a'];
int t = u;
while( t != root && !vis[t] ) {
res += tag[t]; vis[t] = ;//要不然会t啊 tag[t] = ;
t = fail[t]; }
}
return res;
}
};
char s[N];
char ss[N];
Trie ac;
int main() {
int t, n;
read(t);
while(t--){
int ma = ;
ac.init();
read(n); for(int i=; i<n; i++){
scanf("%s",s);
int len = strlen(s);
ac.Insert(s);
if(ma <= len){
ma = len; strcpy(ss, s);
}
}
ac.build();
int ans = ac.query(ss);
if(ans == n){
printf("%s\n",ss);
}
else printf("No\n");
}
return ;
}

2168MS

hdu 6208 The Dominator of Strings【AC自动机】的更多相关文章

  1. HDU 6208 The Dominator of Strings 后缀自动机

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  2. HDU 6208 The Dominator of Strings(AC自动机)

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  3. HDU 6208 The Dominator of Strings【AC自动机/kmp/Sunday算法】

    Problem Description Here you have a set of strings. A dominator is a string of the set dominating al ...

  4. HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机

    https://vjudge.net/problem/HDU-6208 首先可以知道最长那个串肯定是答案 然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配. 普通kmp的话,每次都要 ...

  5. HDU 6208 The Dominator of Strings ——(青岛网络赛,AC自动机)

    最长的才可能成为答案,那么除了最长的以外全部insert到自动机里,再拿最长的去match,如果match完以后cnt全被清空了,那么这个最长串就是答案.事实上方便起见这个最长串一起丢进去也无妨,而且 ...

  6. HDU 2222:Keywords Search(AC自动机模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...

  7. SPOJ 7758. Growing Strings AC自动机DP

    Growing Strings 题目:给出n个字符串,问最多能够选出多少个串组成序列,并满足前一个字符串是后一个字符串的子串. 分析: AC自动机经典水题... 考虑每个节点结尾时,他能够选出最多的串 ...

  8. hdu_5507_GT and strings(AC自动机)

    题目链接:hdu_5507_GT and strings 题意:给n个字符串和q个询问,每个询问给两个数字x,y,问1.x是否为y的子序列,2.x是否为y的子串,是输出1,否则输出0,每个询问输出2个 ...

  9. HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair Problem Description   Biologists finally invent techniques of repairing DNA that contains ...

随机推荐

  1. 为 “超级大脑”构建支撑能力,腾讯云聚焦AI技术落地

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 5月24日,以"无界数据.无限智能"为主题的2018腾讯"云+未来"峰会AI大数据分论坛在广州拉开帷 ...

  2. sql 递归 STUFF

    select distinct fm_id, ,,'') AS SO_Nums from [dbo].[t_BADItems] its 表内一对多 的关系查询

  3. C++(笔)002

    #include <iostream> //预处理器编译指令 int main() //函数头:对函数和程序其它部份之间的接口作出总结 int:函数的返回类型 { using namesp ...

  4. Redis - 数据类型常用命令

    5种数据类型都离不开key,先列出key的相关命令. KEY相关操作 列出符合规则的KEYS KEYS pattern pattern支持glob风格的通配符格式,即: ? 一个字符 * 任意多个字符 ...

  5. java 并发(五)---AbstractQueuedSynchronizer(5)

    问题 : ArrayBlockQueue 和 LinkedBlockQueue 的区别 两者的实现又是怎么样的 应用场景 BlockingQueue 概述 blockingQueue 是个接口,从名字 ...

  6. git flow强制重新初始化

    Gitflow工作流定义了一个围绕项目发布的严格分支模型. git flow初始化命令: git flow init 关于各个分支的命名一路回车就可以了,如果不小心修改了默认的分支命名,后来又觉得不爽 ...

  7. Redis单机数据迁移至Sentinel集群

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  8. 6、springboot之根目录设置

    访问的时候用

  9. 四层协议和Socket编程

    <四层协议图> <Soclet编程模型图>

  10. mysql 查询近几天的结果

    //近两天的 不包括当天的数据 select * from order_info 今天的数据 select* fromorder_info where date(createtime)=curdate ...