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. pyhon-爬虫实战抓取豆瓣top250到mysql

    采集地址https://movie.douban.com/top250 一.创建mysql数据库 CREATE TABLE `t_doubantop` ( `id` int(11) unsigned ...

  2. 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...

  3. jQuery 整体架构

    不同于 jQuery 代码各个模块细节实现的晦涩难懂,jQuery 整体框架的结构十分清晰,按代码行文大致分为如上图所示的模块. 初看 jQuery 源码可能很容易一头雾水,因为 9000 行的代码感 ...

  4. out参数

             out参数: 参数在方法的内部必须为其赋值:可以同时返回不同类型的值:                           在Main方法里定义,在方法里赋值:          输 ...

  5. Full postback triggered by LinkButton inside GridView inside UpdatePanel

    GridView inside of a UpdatePanel,get the button to trigger a partial postback <asp:ScriptManager ...

  6. Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)

    http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...

  7. Python基础学习总结(九)

    11测试代码 1.编写函数和类时,还可以编写测试函数,通过测试可以确定代码面对各种输入都能正常工作.在程序中添加新代码时,也可以对其进行测试,确定他们不会破坏程序的既有程序.要经常测试模块. 2.通过 ...

  8. 插入排序——Python实现

    一.排序思想 排序思想参见:https://www.cnblogs.com/luomeng/p/10583124.html 二.python实现 def InsertSort(arrs): " ...

  9. 三 Buffer

    使用Buffer一般遵循以下四个步骤 写入数据到Buffer 调用flip() 从Buffer中读取数据 调用clear()或者compact()方法 当向buffer写入数据时,buffer会记录下 ...

  10. python anaconda 安装 环境变量 升级 以及特殊库安装

    Anaconda 是一个旗舰版的python安装包, 因为普通的python没有库, 如果需要安装一些重要的库, 要经常一个一个下载,会非常麻烦. 所以这个一个集成的, 可以手动批量升级的软件. 而且 ...