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. ASID 与 MIPS 中 TLB 相关

    ASID 为了提高TLB的性能,将TLB分成Global和process-specific.global 是指常驻在tlb中不会被刷出的,例如内核空间的翻译,process-specific 是指每个 ...

  2. Java finally关键字

    关于finally语句块,有如下特点: 1.finally语句块可以直接和try语句块联用.try...finally... 2.try...catch...finally也可以 3.通常在final ...

  3. 获取指定包名下继承或者实现某接口的所有类(扫描文件目录和所有jar)

    import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.net.JarURLCo ...

  4. sharepint 2013 添加subsite

    在用服务器端对象模型往里面添加subsite的时候,照着书上的代码,结果,失败.报错 not suported language. bing了半天,说是语言未支持,又是修改系统区域,显示语言等,还是失 ...

  5. java设计模式之抽象工厂模式学习

    工厂模式有个问题就是,类的创建依赖工厂.要想增加一个工厂类,就要修改原来的代码,这违背了闭包原则.所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的 ...

  6. 怎么让div显示一行,其余的隐藏。

    <style> div{ white-space: nowrap; text-overflow:ellipsis; text-overflow: ellipsis; overflow:hi ...

  7. 三大图表库:ECharts 、 BizCharts 和 G2,该如何选择?

    最近阿里正式开源的BizCharts图表库基于React技术栈,各个图表项皆采用了组件的形式,贴近React的使用特点.同时BizCharts基于G2进行封装,Bizcharts也继承了G2相关特性. ...

  8. 使用QQ第三方登录 并在父页面跳转刷新

    <html> <head> <title>QQ登录跳转</title> <script src="http://lib.sinaapp. ...

  9. 盒模型的属性丶display显示丶浮动

    一丶盒模型的属性(重要) 1.padding padding是标准文档流,父子之间调整位置 <!DOCTYPE html> <html> <head> <me ...

  10. CSS3,3D效果轮播图

    ---恢复内容开始--- 大家还记得我昨天的3D拖拽立方体吗??我昨天还说过css还可以做轮播图,所以咱们今天就写一下,css的轮播图吧! ....这个轮播图主要是用CSS3里的transform的旋 ...