The Dominator of Strings

Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2830    Accepted Submission(s): 1010

Problem Description
Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string S is dominated by T if S is a substring of T.
 
Input
The input contains several test cases and the first line provides the total number of cases.
For each test case, the first line contains an integer N indicating the size of the set.
Each of the following N lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of 100000.
The limit is 30MB for the input file.
 
Output
For each test case, output a dominator if exist, or No if not.
 
Sample Input
3
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
 
5
abc
cde
abcde
abcde
bcde
 
 
3
aaaaa
aaaab
aaaac
 
Sample Output
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
abcde
No
 

题目链接:HDU 6208

检查你用的是不是假模板的好题……,经此一题,许多人发现自己学的是假AC自动机

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct Trie
{
int nxt[26];
int cnt, fail;
void init()
{
for (int i = 0; i < 26; ++i)
nxt[i] = -1;
cnt = fail = 0;
}
} L[N];
char s[N];
int st[N], Len[N];
int tot; namespace AC
{
void init()
{
tot = 0;
L[tot++].init();
}
void insert(char s[], int len)
{
int u = 0;
for (int i = 0; i < len; ++i)
{
int v = s[i] - 'a';
if (L[u].nxt[v] == -1)
{
L[tot].init();
L[u].nxt[v] = tot++;
}
u = L[u].nxt[v];
}
++L[u].cnt;
}
void build()
{
L[0].fail = 0;
queue<int>Q;
for (int i = 0; i < 26; ++i)
{
if (L[0].nxt[i] == -1)
L[0].nxt[i] = 0;
else
{
L[L[0].nxt[i]].fail = 0;
Q.push(L[0].nxt[i]);
}
}
while (!Q.empty())
{
int u = Q.front();
Q.pop();
int uf = L[u].fail;
for (int i = 0; i < 26; ++i)
{
int v = L[u].nxt[i];
if (v == -1)
L[u].nxt[i] = L[uf].nxt[i];
else
{
L[v].fail = L[uf].nxt[i];
Q.push(v);
}
}
}
}
int query(char s[], int len)
{
int ret = 0;
int u = 0;
for (int i = 0; i < len; ++i)
{
int v = s[i] - 'a';
u = L[u].nxt[v];
while (u && L[u].nxt[v] == -1)
u = L[u].fail;
int t = u;
while (t && L[t].cnt != -1)
{
ret += L[t].cnt;
L[t].cnt = -1;
t = L[t].fail;
}
}
return ret;
}
}
int main(void)
{
int T, n, i;
scanf("%d", &T);
while (T--)
{
AC::init();
scanf("%d", &n);
int sum = 0;
int Maxlen = 0;
int ID = 0;
for (i = 0; i < n; ++i)
{
scanf("%s", s + sum);
Len[i] = strlen(s + sum);
AC::insert(s + sum, Len[i]);
st[i] = sum;
sum += Len[i];
if (Len[i] > Maxlen)
{
ID = i;
Maxlen = Len[i];
}
}
AC::build();
if (AC::query(s + st[ID], Len[ID]) == n)
{
int ed = st[ID] + Len[ID];
for (i = st[ID]; i < ed; ++i)
printf("%c", s[i]);
puts("");
}
else
puts("No");
}
return 0;
}

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自动机】

    hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...

  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. hash和history的区别

    vue-router 中hash模式和history模式. 在vue的路由配置中有mode选项,最直观的区别就是在url中hash 带了一个很丑的 # ,而history是没有#的.vue默认使用ha ...

  2. Delphi方法重载

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  3. flask的模板

    flask用的是jinja2的模板 模板其实是一个包含响应文本的文件,其中用占位符(变量)表示动态部分,告诉模板引擎其具体的值需要从使用的数据中获取 使用真实值替换变量,再返回最终得到的字符串,这个过 ...

  4. Python的scrapy之爬取豆瓣影评和排名

    基于scrapy框架的爬影评 爬虫主程序: import scrapy from ..items import DoubanmovieItem class MoviespiderSpider(scra ...

  5. Kubernetes-深入分析集群安全机制(3.6)

    集群的安全性主要考虑以下几个方面: 容器与所在宿主机的隔离: 限制容器给基础设施及其他容器带来消极影响的能力: 最小权限原则--合理限制所有组件的权限,确保组件只执行它被授权的行为,通过限制单个组件的 ...

  6. js数组长度

    js数组长度,一般使用length 属性即可获取,但这个数组是个对象则只能使用以下方式 var t=typeof o; var length=0; if(t=='string'){ length=o. ...

  7. echarts的pie图中,各区块颜色的调整

    今天在学习使用echarts生成各种图表. 然后在使用pie图时出现我无论怎么更改代码中的颜色,pie图中各块的颜色都十分相近,几乎没法区别块与块之间的区别,如下图: 在下图中,除了其中一块的是红色的 ...

  8. elasticsearch-mathc和term的区分

    elasticsearch和mysql在思想上是有不同的,elasticsearch有分词一说,比如北京奥运分词成北京,奥运,北京奥运.分词要要考虑两点,一个是查询字符串要不要分词,还有就是原存储字段 ...

  9. golang模拟新浪微博登录

    1.基于幽灵蛛pholcus开源项目的规则 直接贴代码,代码可以更改后用于其他爬虫项目 package pholcus_lib // 基础包 import ( // "github.com/ ...

  10. PAT——甲级1012:The Best Rank(有坑)

    1012 The Best Rank (25 point(s)) To evaluate the performance of our first year CS majored students, ...