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. Python基础(5) - 文件

    Python Python提供的函数和方法方便地对文件进行读.写.删除等的操作. open()函数返回一个文件对象. open(name[, mode[, buffering]]) -> fil ...

  2. bzoj 3512: DZY Loves Math IV

    Description 给定n,m,求 模10^9+7的值. Solution 设 \(S(n,m)\) 表示 \(\sum_{i=1}^{m}\phi(n*i)\) \(Ans=\sum_{i=1} ...

  3. 【response-下载文件步骤】

    Java中都通用文件下载(ContentType.文件头.response.out四步骤) 1.设置文件ContentType类型 2.设置文件头 3.通过response获取ServletOutpu ...

  4. 修改django 后台admin用户的密码

    python manage.py shellfrom django.contrib.auth.models import User from django.contrib.auth.models im ...

  5. python模块之contexlib

    一.上下文管理器 with是python实现上下文管理器的核心关键词.它能够在代码执行前和执行后做一些额外的事情. 最常见的代码恐怕就是文件操作了. with open("file" ...

  6. 前端小结(4)---- 页面加载loding....

    /*正在加载*/ function showLoading(elem) { var html = '<div class="loading"><div id=&q ...

  7. 为My97DatePicker日期插件设置默认日期

    datepicker.zip 为My97DatePicker日期插件设置默认日期,开始日期为系统日期的前一个月,结束日期为系统日期: 开始日期不能大于结束日期,且都不能大于今天: 开始日期-maxDa ...

  8. Cocos2d-js 开发记录:骨骼动画载入

    不得不说cocos2d-js的文档实在是少,骨骼动画的载入和C++版本的好像还有些不同不能直接依样画葫芦. 一个由cocos studio编辑生成的骨骼动画一般会包含如下几个文件: .ExportJs ...

  9. IE 8兼容:<meta http-equiv="X-UA-Compatible" content="IE=edge" /> X-UA-Compatible的解释

    前言:9月份开始了,大四也真正的到来了.深知自己网页布局还有很大的缺陷,接下来打算从工作中抽时间出来模仿着一些互联网公司的网站.顺便把基础理论知识打好. 第一个目标:小米官网:后续会把练习的项目放到g ...

  10. 排序算法lowb三人组-插入排序

    def insert_sort(li): for i in range(1, len(li)): # i表示摸到的牌的下标 tmp = li[i] # 摸到的牌 j = i - 1 while j & ...