HDU 6208 The Dominator of Strings(AC自动机)
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
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.
题目链接: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自动机)的更多相关文章
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- hdu 6208 The Dominator of Strings【AC自动机】
hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...
- 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 ...
- HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机
https://vjudge.net/problem/HDU-6208 首先可以知道最长那个串肯定是答案 然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配. 普通kmp的话,每次都要 ...
- HDU 6208 The Dominator of Strings ——(青岛网络赛,AC自动机)
最长的才可能成为答案,那么除了最长的以外全部insert到自动机里,再拿最长的去match,如果match完以后cnt全被清空了,那么这个最长串就是答案.事实上方便起见这个最长串一起丢进去也无妨,而且 ...
- HDU 2222:Keywords Search(AC自动机模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...
- SPOJ 7758. Growing Strings AC自动机DP
Growing Strings 题目:给出n个字符串,问最多能够选出多少个串组成序列,并满足前一个字符串是后一个字符串的子串. 分析: AC自动机经典水题... 考虑每个节点结尾时,他能够选出最多的串 ...
- hdu_5507_GT and strings(AC自动机)
题目链接:hdu_5507_GT and strings 题意:给n个字符串和q个询问,每个询问给两个数字x,y,问1.x是否为y的子序列,2.x是否为y的子串,是输出1,否则输出0,每个询问输出2个 ...
- HDU 2457/POJ 3691 DNA repair AC自动机+DP
DNA repair Problem Description Biologists finally invent techniques of repairing DNA that contains ...
随机推荐
- POCO TCPServer 解析
POCO C++ Libraries提供一套 C++ 的类库用以开发基于网络的可移植的应用程序,功能涉及线程.文件.流,网络协议包括:HTTP.FTP.SMTP 等,还提供 XML 的解析和 SQL ...
- vue服务端渲染按需引入mint
vue服务器渲染按需引入mint-ui 1.修改.babelrc文件,在.babelrc文件中plugins数组中添加 { "presets": [["es2015&qu ...
- jquery easyui alert闪一下的问题
最近做项目使用了 jQuery EasyUI,版本是 1.4.3.x,在使用alert方法的时候如果alert后面执行页面跳转的话alert的消息只会闪一下,就跳到其他页面了 $.messager.a ...
- IdeaVim插件使用技巧--JetBrains Plugin Vim
在 IDEA Intellij小技巧和插件 一文中简单介绍了一下IdeaVim插件.在这里详细总结一下这个插件在日常编程中的一些常用小技巧.供有兴趣使用这个插件,但对Vim还不十分熟悉的朋友参考.当然 ...
- Python栈的学习资料
持续更新... 1. 基础 Python for Everybody的视频课程,称得上深入浅出 https://www.py4e.com/ 2. 进阶 偏重实践应用,快速上手,稀饭~ https:// ...
- pycharm中每次创建py文件时就自动生成代码头,以及出现SyntaxError:Non-ASCII 。。。问题
我们在pycharm中执行py文件的时候,可能会出现以下错误 这是因为你没有制定编码格式,这时候你需要在文件最开始制定编码格式,代码如下 #!/user/bin/env python #-*- cod ...
- 嵌入式框架Zorb Framework搭建七:任务的实现
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- 03---Nginx配置文件
#启动子进程程序默认用户#user nobody;#一个主进程和多个工作进程.工作进程是单进程的,且不需要特殊授权即可运行:这里定义的是工作进程数量worker_processes 1; #全局错误日 ...
- flask与javascript及ajax
flask与javascript及ajax 1. flask+js 1.1. 最简单的 最简单的元素信息改变. {% block content %} <h1>我的第一张网 ...
- HBase Scan,Get用法
Scan,get用法 1. get help帮助信息 从下列get用法信息可以看出 get 后面可以跟table表名,rowkey,以及column,value.但是如果想通过get直接获取一个表中的 ...