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. UEditor编辑器 字符数统计和字符数限制 问题

    1.百度UEditor修改右下角统计字数默认只统计前台所见的文字个数,为了便于展示实际保存的时候是保存的包含html标签的,所以右下角的统计字数功能需要修改 getContentLength: fun ...

  2. awk常用命令总结

    awk工具,主要将一行分成“字段”来处理. awk '条件类型1{动作1} 条件类型2{动作2}...‘ filename awk主要是处理每一行的字段内的数据,而默认的字段的分隔符为空格键或[tab ...

  3. Golang教程:循环语句

    循环语句用于重复执行一段代码. for 语句是 Go 中唯一的循环语句.Go 没有提供其他语言(如 C)中的 while 和 do while 语句. for 语句语法 for 语句的语法如下: fo ...

  4. .netCore2.0 程序集DI依赖注入

    传统的依赖注入确实简单,但是随着项目的扩展随之而来的问题又来了,因为传统的注入是单个类和接口注入的,加入项目的接口和类增加到了上百个的话,就需要在Startup.cs中复制注入上百次,虽然能解决问题, ...

  5. bootstrap-select在angular上的应用

    1.bootstrap-select 依赖bootstrap.js ,又依赖jQuery,这些都可以用requirejs来处理. 2.一般bootstrap-select 都放在具体的模块上,而是动态 ...

  6. JQuery extend()与工具方法、实例方法

    使用jQuery的时候会发现,jQuery中有的函数是这样使用的: $.get(); $.post(); $.getJSON(); 有些函数是这样使用的: $('div').css(); $('ul' ...

  7. JqueryEasyUI EasyLoader

    EasyLoader(简单加载) 对象的属性和方法: 使用: <script src="~/jquery-easyui-1.5.2/jquery.min.js">< ...

  8. mycat核心概念

    一.逻辑库(schema) 业务人员一般是不需要知道数据库中间件的,他们只需要连接到数据库并使用数据库,一切复杂的细节都被中间件给隐藏了,对于业务人员来说中间件即是一个数据库.这里逻辑库的概念就是一个 ...

  9. JAVA_SE_Day02 String 的正则表达式

    字符串支持正则表达式的方法一: boolean matches(String regex) 注意: 给定的正则表达式就算不指定边界符(^,$),也会全匹配验证 空字符串和null 空字符串是看不见,而 ...

  10. Spring_Spring与IoC_基于注解的DI

    一.基本注解的使用 (1)导入AOP的Jar包 (2) 与set()无关 二.组件扫描器的base-package 三.@Component相关注解 四.@Scope 五.域属性的注入 (1)byTy ...