http://codeforces.com/contest/427/problem/D

题目是找出两个串的最短公共子串,并且在两个串中出现的次数只能是1次。

正解好像是dp啥的,但是用sam可以方便很多,复杂度n^2

首先对两个串建立sam,拓扑dp出endpos集合的大小,然后枚举第二个串的所有子串,在两个sam中跑就行了。

很无脑。从[i, j] 递推到[i, j + 1]这个子串,是可以O(1)转移的。

#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 = * + , N = ;
struct Node {
int mxCnt; //mxCnt表示后缀自动机中当前节点识别子串的最大长度
int miCnt; //miCnt表示后缀自动机中当前节点识别子串的最小长度
int id; //表示它是第几个后缀自动机节点,指向了它,但是不知道是第几个,用id判断
int pos; //pos表示它在原串中的位置。
bool flag; //表示当前节点是否能识别前缀
struct Node *pNext[N], *fa;
} suffixAutomaton[maxn * ], sam[maxn * ], *root, *last; //大小需要开2倍,因为有一些虚拟节点
int t; //用到第几个节点
struct Node *create(int mxCnt = -, struct Node *node = NULL) { //新的节点
if (mxCnt != -) {
suffixAutomaton[t].mxCnt = mxCnt, suffixAutomaton[t].fa = NULL;
for (int i = ; i < N; ++i) suffixAutomaton[t].pNext[i] = NULL;
} else {
suffixAutomaton[t] = *node; //保留了node节点所有的指向信息。★全部等于node
//可能需要注意下pos,在原串中的位置。现在pos等于原来node的pos
}
suffixAutomaton[t].id = t; //必须要有的,不然id错误
suffixAutomaton[t].flag = false; //默认不是前缀节点
return &suffixAutomaton[t++];
}
void addChar(int x, int pos) { //pos表示在原串的位置
struct Node *p = last, *np = create(p->mxCnt + , NULL);
np->flag = true;
np->pos = pos, last = np; //last是最尾那个可接收后缀字符的点。
for (; p != NULL && p->pNext[x] == NULL; p = p->fa) p->pNext[x] = np;
if (p == NULL) {
np->fa = root;
np->miCnt = ; // 从根节点引一条边过来
return;
}
struct Node *q = p->pNext[x];
if (q->mxCnt == p->mxCnt + ) { //中间没有任何字符
np->fa = q;
np->miCnt = q->mxCnt + ; // q是7-->8的那些"ab",np是"bab"长度是2+1
return;
}
// p: 当前往上爬到的可以接受后缀的节点
// np:当前插入字符x的新节点
// q: q = p->pNext[x],q就是p中指向的x字符的节点
// nq:因为q->cnt != p->cnt + 1而新建出来的模拟q的节点
struct Node *nq = create(-, q); // 新的q节点,用来代替q,帮助np接收后缀字符
nq->mxCnt = p->mxCnt + ; //就是需要这样,这样中间不包含任何字符
q->miCnt = nq->mxCnt + , np->miCnt = nq->mxCnt + ;
q->fa = nq, np->fa = nq; //现在nq是包含了本来q的所有指向信息
for (; p && p->pNext[x] == q; p = p->fa) {
p->pNext[x] = nq;
}
}
void init() {
t = ;
root = last = create(, NULL);
}
void build(char str[], int lenstr) {
init();
for (int i = ; i <= lenstr; ++i) addChar(str[i] - 'a', i);
}
char str[maxn], sub[maxn];
int in[maxn], que[maxn], dp[][maxn];
unsigned long long int sum[maxn], po[maxn];
bool ok(int en, int len, int lensub) {
unsigned long long int val = sum[en] - sum[en - len] * po[len];
int tim = ;
for (int i = len; i <= lensub; ++i) {
if (val == sum[i] - sum[i - len] * po[len]) tim++;
}
return tim == ;
}
void init(int t, struct Node * suffixAutomaton, int dp[]) {
memset(in, false, sizeof in);
for (int i = ; i < t; ++i) {
if (suffixAutomaton[i].flag) dp[i] = ;
in[suffixAutomaton[i].fa->id]++;
}
int head = , tail = ;
for (int i = ; i < t; ++i) {
if (in[i] == ) que[tail++] = i;
}
while (head < tail) {
int cur = que[head++];
if (cur == ) break;
dp[suffixAutomaton[cur].fa->id] += dp[cur];
in[suffixAutomaton[cur].fa->id]--;
if (in[suffixAutomaton[cur].fa->id] == )
que[tail++] = suffixAutomaton[cur].fa->id;
}
}
void work() {
scanf("%s%s", str + , sub + );
int lenstr = strlen(str + ), lensub = strlen(sub + );
build(str, lenstr);
int sam_t = t;
memcpy(sam, suffixAutomaton, sizeof suffixAutomaton);
build(sub, lensub);
init(sam_t, sam, dp[]);
init(t, suffixAutomaton, dp[]);
// printf("%d\n", dp[1][5]);
int mi = inf;
for (int i = ; i <= lensub; ++i) {
int strnow = , subnow = ;
for (int j = i; j <= lensub; ++j) {
int id = sub[j] - 'a';
if (sam[strnow].pNext[id] == NULL) break;
strnow = sam[strnow].pNext[id]->id;
subnow = suffixAutomaton[subnow].pNext[id]->id;
if (dp[][strnow] == && dp[][subnow] == ) {
mi = min(mi, j - i + );
}
}
}
if (mi == inf) mi = -;
printf("%d\n", mi);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

这题有一个O(n)的算法,那就是,把两个串合并。中间用一个字符分割,这是为了不产生多余的子串。

主要思想就是,要找到一个串,出现的次数为2,并且是在两个不同的串中分别出现的。

出现次数是2,那么就是拓扑dp的时候,endpos集合的大小是2即可。那么怎么限制在两个不同的串中出现过?

记在第一个串出现的时候,id是1 << 0,第二个串出现的时候,id是1 << 1

然后在dp出endpos集合大小的时候,顺便也维护一下在那里出现过即可。

每一个状态,都可能包含了若干个子串,那么需要取最短的子串。

#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 = * + , N = ;
struct Node {
int mxCnt; //mxCnt表示后缀自动机中当前节点识别子串的最大长度
int miCnt; //miCnt表示后缀自动机中当前节点识别子串的最小长度
int id; //表示它是第几个后缀自动机节点,指向了它,但是不知道是第几个,用id判断
int pos; //pos表示它在原串中的位置。
bool flag; //表示当前节点是否能识别前缀
struct Node *pNext[N], *fa;
}suffixAutomaton[maxn * ], *root, *last; //大小需要开2倍,因为有一些虚拟节点
int t; //用到第几个节点
struct Node *create(int mxCnt = -, struct Node *node = NULL) { //新的节点
if (mxCnt != -) {
suffixAutomaton[t].mxCnt = mxCnt, suffixAutomaton[t].fa = NULL;
for (int i = ; i < N; ++i) suffixAutomaton[t].pNext[i] = NULL;
} else {
suffixAutomaton[t] = *node; //保留了node节点所有的指向信息。★全部等于node
//可能需要注意下pos,在原串中的位置。现在pos等于原来node的pos
}
suffixAutomaton[t].id = t; //必须要有的,不然id错误
suffixAutomaton[t].flag = false; //默认不是前缀节点
return &suffixAutomaton[t++];
}
void addChar(int x, int pos) { //pos表示在原串的位置
struct Node *p = last, *np = create(p->mxCnt + , NULL);
np->flag = true;
np->pos = << pos, last = np; //last是最尾那个可接收后缀字符的点。
for (; p != NULL && p->pNext[x] == NULL; p = p->fa) p->pNext[x] = np;
if (p == NULL) {
np->fa = root;
np->miCnt = ; // 从根节点引一条边过来
return;
}
struct Node *q = p->pNext[x];
if (q->mxCnt == p->mxCnt + ) { //中间没有任何字符
np->fa = q;
np->miCnt = q->mxCnt + ; // q是7-->8的那些"ab",np是"bab"长度是2+1
return;
}
// p: 当前往上爬到的可以接受后缀的节点
// np:当前插入字符x的新节点
// q: q = p->pNext[x],q就是p中指向的x字符的节点
// nq:因为q->cnt != p->cnt + 1而新建出来的模拟q的节点
struct Node *nq = create(-, q); // 新的q节点,用来代替q,帮助np接收后缀字符
nq->mxCnt = p->mxCnt + ; //就是需要这样,这样中间不包含任何字符
q->miCnt = nq->mxCnt + , np->miCnt = nq->mxCnt + ;
q->fa = nq, np->fa = nq; //现在nq是包含了本来q的所有指向信息
for (; p && p->pNext[x] == q; p = p->fa) {
p->pNext[x] = nq;
}
}
void init() {
t = ;
root = last = create(, NULL);
}
void build(char str[], int lenstr) {
init();
for (int i = ; i <= lenstr; ++i) addChar(str[i] - 'a', i);
}
char str[maxn], sub[maxn];
int que[maxn * ], in[maxn], dp[maxn], is[maxn];
void work() {
scanf("%s%s", str + , sub + );
init();
for (int i = ; str[i]; ++i) addChar(str[i] - 'a', );
addChar(, );
for (int i = ; sub[i]; ++i) addChar(sub[i] - 'a', );
for (int i = ; i < t; ++i) {
is[i] = suffixAutomaton[i].pos;
if (suffixAutomaton[i].flag) dp[i] = ;
in[suffixAutomaton[i].fa->id]++;
}
int head = , tail = ;
for (int i = ; i < t; ++i) {
if (in[i] == ) que[tail++] = i;
}
while (head < tail) {
int cur = que[head++];
if (!cur) break;
is[suffixAutomaton[cur].fa->id] |= is[cur];
dp[suffixAutomaton[cur].fa->id] += dp[cur];
in[suffixAutomaton[cur].fa->id]--;
if (in[suffixAutomaton[cur].fa->id] == ) que[tail++] = suffixAutomaton[cur].fa->id;
}
int mi = inf;
for (int i = ; i < t; ++i) {
if (is[i] == && dp[i] == ) {
mi = min(mi, suffixAutomaton[i].miCnt); //最短
}
}
if (mi == inf) mi = -;
printf("%d\n", mi);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

也可以直接用广义后缀自动机。

广义后缀自动机能识别多个主串的所有子串,并且在拓扑dp的时候能识别到是在那个串出现的。

广义后缀自动机就是把多个主串统一弄起来,每次都从root开始插入

这就带来一个问题就是已经存在了该节点。

那么就不需要np了。如果该节点能够代替新插入的节点接受后缀,也就是p->mxCnt + 1 == q->mxCnt,中间不含有任何字符。那么last直接去到q就好了,否则就要新建节点nq来弄个节点代替q接受后缀。和后缀自动机一个意思。

ps: 这个节点就是当前id的前缀节点。是属于id的。

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int MOD = 1e9 + ;
const int maxn = 1e5 + , N = ;
struct Node {
int mxCnt; //mxCnt表示后缀自动机中当前节点识别子串的最大长度
int miCnt; //miCnt表示后缀自动机中当前节点识别子串的最小长度
int id; //表示它是第几个后缀自动机节点,指向了它,但是不知道是第几个,用id判断
int pos; //pos表示它在原串中的位置。
bool flag; //表示当前节点是否能识别前缀
bool R[]; // 广义后缀自动机识别此状态是否在第R[i]个主串中出现过
struct Node *pNext[N], *fa;
}suffixAutomaton[maxn * ], *root, *last; //大小需要开2倍,因为有一些虚拟节点
int t; //用到第几个节点
struct Node *create(int mxCnt = -, struct Node *node = NULL) { //新的节点
if (mxCnt != -) {
suffixAutomaton[t].mxCnt = mxCnt, suffixAutomaton[t].fa = NULL;
for (int i = ; i < N; ++i) suffixAutomaton[t].pNext[i] = NULL;
} else {
suffixAutomaton[t] = *node; //保留了node节点所有的指向信息。★全部等于node
//可能需要注意下pos,在原串中的位置。现在pos等于原来node的pos
}
suffixAutomaton[t].id = t; //必须要有的,不然id错误
suffixAutomaton[t].flag = false; //默认不是前缀节点
return &suffixAutomaton[t++];
}
void addChar(int x, int pos, int id) { //pos表示在原串的位置
struct Node *p = last;
if (p->pNext[x] != NULL) { // 有了,就不需要np
struct Node *q = p->pNext[x];
if (p->mxCnt + == q->mxCnt) {
last = q; //用来接收后缀字符
q->R[id] = true;
q->flag = true;
return;
}
//现在的q没办法成为接受后缀的点
//那么就开一个节点模拟它,所以这个节点是id的前缀节点
struct Node * nq = create(-, q);
for (int i = ; i < ; ++i) nq->R[i] = false;
nq->mxCnt = p->mxCnt + ;
nq->R[id] = true;
nq->flag = true; //这个点是属于id的。是id的前缀节点
q->fa = nq; //这里是没有np的
q->miCnt = nq->mxCnt + ;
for (; p && p->pNext[x] == q; p = p->fa) p->pNext[x] = nq;
last = nq; //成为接受后缀的节点。
return;
}
struct Node *np = create(p->mxCnt + , NULL);
for (int i = ; i < ; ++i) np->R[i] = false; //每次都要清空
np->R[id] = true;
np->flag = true; //前缀节点
np->pos = pos, last = np; //last是最尾那个可接收后缀字符的点。
for (; p != NULL && p->pNext[x] == NULL; p = p->fa) p->pNext[x] = np;
if (p == NULL) {
np->fa = root;
np->miCnt = ; // 从根节点引一条边过来
return;
}
struct Node *q = p->pNext[x];
if (q->mxCnt == p->mxCnt + ) { //中间没有任何字符,可以用来代替接受后缀、
np->fa = q;
np->miCnt = q->mxCnt + ; // q是状态8的"ab",np是状态7的"bab"长度是2+1
return;
}
struct Node *nq = create(-, q); // 新的q节点,用来代替q,帮助np接收后缀字符
for (int i = ; i < ; ++i) nq->R[i] = false;
nq->mxCnt = p->mxCnt + ; //就是需要这样,这样中间不包含任何字符
q->miCnt = nq->mxCnt + , np->miCnt = nq->mxCnt + ;
q->fa = nq, np->fa = nq; //现在nq是包含了本来q的所有指向信息
for (; p && p->pNext[x] == q; p = p->fa) {
p->pNext[x] = nq;
}
}
void init() {
t = ;
root = last = create(, NULL);
}
char str[maxn];
int dp[maxn * ][];
int d[maxn * ];
queue<int> que;
int in[maxn];
void work() {
init();
scanf("%s", str + );
for (int i = ; str[i]; ++i) addChar(str[i] - 'a', i, );
last = root;
scanf("%s", str + );
for (int i = ; str[i]; ++i) addChar(str[i] - 'a', i, );
for (int i = ; i < t; ++i) {
in[suffixAutomaton[i].fa->id]++;
if (suffixAutomaton[i].flag) {
dp[i][] = suffixAutomaton[i].R[];
dp[i][] = suffixAutomaton[i].R[];
d[i] = suffixAutomaton[i].R[] + suffixAutomaton[i].R[];
}
}
for (int i = ; i < t; ++i) {
if (in[i] == ) que.push(i);
}
while (!que.empty()) {
int cur = que.front();
que.pop();
if (!cur) break;
dp[suffixAutomaton[cur].fa->id][] += dp[cur][];
dp[suffixAutomaton[cur].fa->id][] += dp[cur][];
d[suffixAutomaton[cur].fa->id] += d[cur];
in[suffixAutomaton[cur].fa->id]--;
if (in[suffixAutomaton[cur].fa->id] == ) que.push(suffixAutomaton[cur].fa->id);
}
int ans = inf;
for (int i = ; i < t; ++i) {
assert(d[i] == dp[i][] + dp[i][]);
if (dp[i][] == && dp[i][] == ) {
ans = min(ans, suffixAutomaton[i].miCnt);
}
}
if (ans == inf) ans = -;
printf("%d\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

D. Match & Catch 后缀自动机 || 广义后缀自动机的更多相关文章

  1. 字典树(trie树) 后缀树 广义后缀树

    转自:http://www.cnblogs.com/dong008259/archive/2011/11/11/2244900.html (1)字典树(Trie树) Trie是个简单但实用的数据结构, ...

  2. POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 对[广义后缀自动机]的一些理解

    先说一下对后缀自动机的理解,主要是对构造过程的理解. 构造中,我们已经得到了前L个字符的后缀自动机,现在我们要得到L+1个字符的后缀自动机,什么需要改变呢? 首先,子串$[0,L+1)$对应的状态不存 ...

  4. BZOJ 3926 && ZJOI 2015 诸神眷顾的幻想乡 (广义后缀自动机)

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MB Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽 ...

  5. BZOJ 3277 串 (广义后缀自动机)

    3277: 串 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 309 Solved: 118 [Submit][Status][Discuss] De ...

  6. BZOJ 3473: 字符串 [广义后缀自动机]

    3473: 字符串 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 354  Solved: 160[Submit][Status][Discuss] ...

  7. BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)

    题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...

  8. BZOJ.3926.[ZJOI2015]诸神眷顾的幻想乡(广义后缀自动机)

    题目链接 要对多个串同时建立SAM,有两种方法: 1.将所有串拼起来,中间用分隔符隔开,插入字符正常插入即可. 2.在这些串的Trie上建SAM.实际上并不需要建Trie,还是只需要正常插入(因为本来 ...

  9. 【CF666E】Forensic Examination 广义后缀自动机+倍增+线段树合并

    [CF666E]Forensic Examination 题意:给你一个字符串s和一个字符串集合$\{t_i\}$.有q个询问,每次给出$l,r,p_l,p_r$,问$s[p_l,p_r]$在$t_l ...

随机推荐

  1. window7 下配置python2.7+tornado3.3开发环境

    玩python的人大都在linux下进行开发,由于长期习惯在windows下开发代码,今天蛋疼尝试在window7下配置python2.7+tornado3.3开发环境,必然的中间遇到各种报错,但是最 ...

  2. Time - Time-interval Measurements

    public class TimeHelper { private long _start, _stop, _elapsed; /// <summary> /// 获取初始时间戳 /// ...

  3. C#Task学习

    简介: Task 对象是一种的中心思想基于任务的异步模式首次引入.NET Framework 4 中. 因为由执行工作Task对象通常以异步方式执行线程池线程上而不是以同步方式在主应用程序线程中,可以 ...

  4. C#ADO.NET基础二

    DataAdapter的使用,批量增删改 1.使用DataAdapter查询 private void Select2() { try { using (SQLiteConnection conn = ...

  5. 死磕Java之聊聊ArrayList源码(基于JDK1.8)

    工作快一年了,近期打算研究一下JDK的源码,也就因此有了死磕java系列 ArrayList 是一个数组队列,相当于动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractLis ...

  6. RDD与DataFrame

    RDD:分布式对象集合: 解决了:减少中间计算结果处理所需的开销   数据存在内存,提供一个通用的抽象的数据结构 惰性求值 DataFrame:分布式Row对象集合 服务于:SparkSQL

  7. yum及RPM安装

    yum及RPM安装 基本说明: 1.yum相当于windows上面的360软件中心 2.yum是redhat系列发行版的软件安装命令 debian系统用的是apt-get 3.yum安装软件的来源得存 ...

  8. linux下 zip解压 tar解压 gz解压 bz2等各种解压文件命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...

  9. 表单校验--js部分

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  10. 2、Tensorflow中的变量

    2.Tensorflow中的变量注意:tf中使用 变量必须先初始化下面是一个使用变量的TF代码(含注释): # __author__ = "WSX" import tensorfl ...