HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机
https://vjudge.net/problem/HDU-6208
首先可以知道最长那个串肯定是答案
然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配。
普通kmp的话,每次都要O(mxLen)的复杂度肯定不行。考虑AC自动机,不说这个算法了都懂。
大概就是,询问主串的时候用Fail指针快速转移到LCP,然后就可以用字典树快速判断其是否一个模式串
可以知道判断过的可以标记下,不需要再判断了(听说很多人TLE在这里了,比赛的时候写歪了也TLE)
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 6e5 + , N = ;
struct node {
int flag;
struct node *Fail; //失败指针,匹配失败,跳去最大前后缀
struct node *pNext[N];
} tree[maxn];
int t; //字典树的节点
struct node *create() { //其实也只是清空数据而已,多case有用,根是0号顶点、
struct node *p = &tree[t++];
p->flag = ;
p->Fail = NULL;
for (int i = ; i < N; i++) {
p->pNext[i] = NULL;
}
return p;
}
void toinsert(struct node **T, char str[], int be, int en) {
struct node *p = *T;
if (p == NULL) {
p = *T = create();
}
for (int i = be; i <= en; i++) {
int id = str[i] - 'a';
if (p->pNext[id] == NULL) {
p->pNext[id] = create();
}
p = p->pNext[id];
}
p->flag++; //相同的单词算两次
}
struct node *que[maxn + ]; //这里的t是节点总数,字典树那里统计的,要用G++编译
void BuiltFail(struct node **T) {
//根节点没有失败指针,所以都是需要特判的
//思路就是去到爸爸的失败指针那里,找东西匹配,这样是最优的
struct node *p = *T; //用个p去代替修改
struct node *root = *T;
if (p == NULL) return ;
//树上bfs,要更改的是p->pNext[i]->Fail
int head = , tail = ;
que[tail++] = root;
while (head < tail) {
p = que[head]; //p取出第一个元素 ★
for (int i = ; i < N; i++) { //看看存不存在这个节点
if (p->pNext[i] != NULL) { //存在的才需要管失败指针。
if (p == root) { //如果爸爸是根节点的话,根节点没有失败指针
p->pNext[i]->Fail = root; //指向根节点
} else {
struct node *FailNode = p->Fail; //首先找到爸爸的失败指针
while (FailNode != NULL) {
if (FailNode->pNext[i] != NULL) { //存在
p->pNext[i]->Fail = FailNode->pNext[i];
break;
}
FailNode = FailNode->Fail; //回溯,根节点的fail是NULL
}
if (FailNode == NULL) { //如果还是空,那么就指向根算了
p->pNext[i]->Fail = root;
}
}
que[tail++] = p->pNext[i]; //这个id是存在的,入队bfs
}
}
head++;
}
}
int searchAC(struct node *T, char str[], int be, int en) {
int ans = ;
struct node *p = T;
struct node *root = T;
if (p == NULL) return ;
for (int i = be; i <= en; i++) { //遍历主串中的每一个字符
int id = str[i] - 'a';
p = p->pNext[id]; //去到这个节点,虚拟边也建立起来了,所以一定存在。
struct node *temp = p; //p不用动,下次for就是指向这里就OK,temp去找后缀串
//什么叫找后缀串?就是,有单词 she,he 串***she,那么匹配到e的时候,she统计成功
//这个时候,就要转移去到he那里,也把he统计进去。也就是找等价态
while (temp != root && temp->flag != -) { //root没失败指针
if (temp->flag > ) {
ans += temp->flag;
}
temp->flag = -; //标记,,他们卡在这里吗
temp = temp->Fail;
}
}
return ans;
}
char str[maxn];
void work() {
t = ;
int n;
scanf("%d", &n);
struct node * T = NULL;
int ansbe = , ansen = , anslen = ;
int pre = ;
for (int i = ; i <= n; ++i) {
scanf("%s", str + pre);
int tlen = strlen(str + pre);
pre += tlen;
if (tlen > anslen) {
anslen = tlen;
ansbe = pre - tlen;
ansen = pre - ;
}
toinsert(&T, str, pre - tlen, pre - );
}
// printf("%s\n", now + 1);
BuiltFail(&T);
if (searchAC(T, str, ansbe, ansen) == n) {
for (int i = ansbe; i <= ansen; ++i) {
printf("%c", str[i]);
}
printf("\n");
} else printf("No\n");
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
然后生气了还是sam吧
对最长的串建立sam
对于每一个串是否其子串,可以O(lensub)判断。
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e5 + , N = ;
struct SAM {
int mxCnt[maxn << ], son[maxn << ][N], fa[maxn << ];
int root, last, DFN, t;
int create() {
++t;
mxCnt[t] = fa[t] = NULL;
for (int i = ; i < N; ++i) son[t][i] = NULL;
return t;
}
void init() {
++DFN;
t = , root = ;
last = create();
}
void addChar(int x, int _pos, int id) {
int p = last;
int np = create();
last = np;
mxCnt[np] = mxCnt[p] + ;
for (; p && son[p][x] == NULL; p = fa[p]) son[p][x] = np;
if (p == NULL) {
fa[np] = root;
return;
}
int q = son[p][x];
if (mxCnt[q] == mxCnt[p] + ) {
fa[np] = q;
return;
}
int nq = create();
for (int i = ; i < N; ++i) son[nq][i] = son[q][i];
fa[nq] = fa[q], mxCnt[nq] = mxCnt[p] + ;
fa[q] = nq, fa[np] = nq;
for (; p && son[p][x] == q; p = fa[p]) son[p][x] = nq;
}
bool is(string &str, int tt) {
int p = root;
for (int i = ; i < tt; ++i) {
if (son[p][str[i] - 'a']) {
p = son[p][str[i] - 'a'];
} else return false;
}
return true;
}
} sam;
string str[maxn];
int tt[maxn];
char liu[maxn];
void work() {
sam.init();
int n;
scanf("%d", &n);
int id = , len = ;
for (int i = ; i <= n; ++i) {
scanf("%s", liu);
str[i] = string(liu);
tt[i] = strlen(str[i].c_str());
if (len < tt[i]) {
len = tt[i];
id = i;
}
}
for (int i = ; i < len; ++i) {
sam.addChar(str[id][i] - 'a', i, );
}
for (int i = ; i <= n; ++i) {
if (i == id) continue;
if (!sam.is(str[i], tt[i])) {
printf("No\n");
return;
}
}
printf("%s\n", str[id].c_str());
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机的更多相关文章
- hdu 6208 The Dominator of Strings【AC自动机】
hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...
- HDU 6208 The Dominator of Strings(AC自动机)
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- 2017 ACM/ICPC Asia Regional Qingdao Online 1003 The Dominator of Strings hdu 6208
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- 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 ...
- The Dominator of Strings HDU - 6208(ac自动机板题)
题意: 就是求是否有一个串 是其它所有串的母串 解析: 把所有的串都加入到trie数中 然后用最长的串去匹配就好了 emm..开始理解错题意了...看成了只要存在一个串是另一个的母串就好.. 然后输 ...
- HDU 6208 The Dominator of Strings ——(青岛网络赛,AC自动机)
最长的才可能成为答案,那么除了最长的以外全部insert到自动机里,再拿最长的去match,如果match完以后cnt全被清空了,那么这个最长串就是答案.事实上方便起见这个最长串一起丢进去也无妨,而且 ...
- HDU 4622 Reincarnation(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...
- hdu6208 The Dominator of Strings
地址: 题目: The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 ...
随机推荐
- HUST高级软件工程--测试管理工具实践--Day3
测试管理工具实践--Day3 今天完成任务情况: 小靳 今天,大家参加考试,时间比较紧促.庆幸,自己的队伍比较给力,大家都没有拖后腿,深夜还在为自己的任务拼搏,很是激励人心 我今天的工作就是 学会了注 ...
- 基于jQuery的Tooltip
近来,要开发一个关务管理系统,为了增加系统美观度,自己开发了一个基于jQuery框的的小插件,与大家分享一下,希望大家能给我提出宝贵修改意见. 下面开发说明使用方法和内容: 一.引用jQuery框架, ...
- HTTP 协议 -- 状态码
HTTP 协议状态码(Http Status Code) 使用ASP.NET/PHP/JSP 或者javascript都会用到http的不同状态,一些常见的状态码为: 200 – 服务器成功返回网页 ...
- MongoDB 分片2
mongodb 在windows下面进行分片 mongodb 更新很快,在做分片的时候,查找了不少文章,但是很多已经过时了.现在把我搭建的过程及命令分享给大家.我用的是最新版本windows版3.4. ...
- nginx 部署 .net core 获取的客户端ip为127.0.0.1
采用nginx和.net core 部署一套api接口到服务器上,发现获取到的ip地址为127.0.0.1 经过检查发现,需要在nginx配置上以下参数 proxy_set_header Host $ ...
- jmeter:dubbo接口测试
最近工作中接到一个需求,需要对一个MQ消息队列进行性能测试,测试其消费能力,开发提供了一个dubbo服务来供我调用发送消息. 这篇博客,介绍下如何利用jmeter来测试dubbo接口,并进行性能测试. ...
- 正经学C#_变量与其数据类型:《c#入门经典》
这一篇总结以下变量与其数据类型. 变量:在c#中指 某一个值或者数据存储在变量中,并且可以取出或者查看.变量不仅仅是一种,也有很多种,细分而言就是类型.泛指就是变量.如果是要是使用变量就要 声明变量, ...
- 关于CRTP(Curiously Recurring Template Prattern)的使用
在阅读frameworks/rs/cpp/util/RefBase.h之LightRefBase时,我记得<C++设计新思维>里对这种用法是有过介绍的,可是今天翻箱倒柜,怎么都找不到那本奇 ...
- MVC(Java , C# ,php)
- OkHttp 3.x 源码解析之Interceptor 拦截器
拦截器 Java里的拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提 ...