Codeforces 427D Match & Catch 后缀自动机 或 后缀数组
来源:互联网 发布:网络口碑营销定义 编辑:程序博客网 时间:2024/04/29 20:31
题目大意:
就是对于两个字符串S1和S2, 求出他们最短的一个公共字串P, 满足P在S1中只出现1次, P在S2中也只出现一次, 输出P最小的长度, 如果这样的P不存在, 输出-1
大致思路:
很明显的一个做法是将S1和S2连接起来中间用未出现的字符隔开, 然后建立后缀自动机, 记录每一个状态中表示的字符串的来源(可以状压记录), 然后所有状态中, 满足Right集合为2, 且状压表示出现在了2个字符串中即可, 在这样一个状态中取最短可行长度, 遍历所有状态点即可, 复杂度O(|S1| + |S2|)
当然还可以用后缀数组来做, 考虑到P要满足的条件如果这样的P存在, 必定是sa数组中相邻的存在, 所以扫描一遍height数组, 当sa[i - 1]与sa[i]不属于同一字符串时, 考虑P不能再其他位置出现那么只能height[i - 1] < height[i], height[i + 1] < height[i]是存在这样的P, 最小长度为 max(height[i - 1], height[i + 1]) + 1, 扫描一遍height数组找出满足条件的最小的那一个P即可
后缀自动机代码如下:
Result : Accepted Memory : 2648 KB Time : 31 ms
/* * Author: Gatevin * Created Time: 2015/4/16 19:29:08 * File Name: Rin_Tohsaka.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)#define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl#define maxm 10010#define maxn 20010struct Suffix_Automation{ struct State { State *par; State *go[27]; int right, val, mi, appear; void init(int _val = 0) { par = 0, val = _val, right = mi = appear = 0; memset(go, 0, sizeof(go)); } }; State *root, *last, *cur; State nodePool[maxn]; State* newState(int val = 0) { cur->init(val); return cur++; } void initSAM() { cur = nodePool; root = newState(); last = root; } void extend(int w, int belong) { State *p = last; State *np = newState(p->val + 1); np->right = 1; np->appear = (1 << belong); while(p && p->go[w] == 0) { p->go[w] = np; p = p->par; } if(p == 0) { np->par = root; } else { State *q = p->go[w]; if(q->val == p->val + 1) np->par = q; else { State *nq = newState(p->val + 1); memcpy(nq->go, q->go, sizeof(q->go)); nq->par = q->par; q->par = nq; np->par = nq; while(p && p->go[w] == q) { p->go[w] = nq; p = p->par; } } } last = np; } int d[maxm]; State *b[maxn]; void topo() { int cnt = cur - nodePool; int maxVal = 0; memset(d, 0, sizeof(d)); for(int i = 1; i < cnt; i++) maxVal = max(maxVal, nodePool[i].val), d[nodePool[i].val]++; for(int i = 1; i <= maxVal; i++) d[i] += d[i - 1]; for(int i = 1; i < cnt; i++) b[d[nodePool[i].val]--] = &nodePool[i]; b[0] = root; } void SAMInfo() { State *p; int cnt = cur - nodePool; for(int i = cnt - 1; i > 0; i--) { p = b[i]; p->par->right += p->right; p->par->appear |= p->appear; p->mi = p->par->val + 1; } }};Suffix_Automation sam;char s[maxm];int main(){ scanf("%s", s); int len = strlen(s); sam.initSAM(); for(int i = 0; i < len; i++) sam.extend(s[i] - 'a', 0); sam.extend(26, 2); scanf("%s", s); len = strlen(s); for(int i = 0; i < len; i++) sam.extend(s[i] - 'a', 1); sam.topo(); sam.SAMInfo(); int ans = 1e9; int cnt = sam.cur - sam.nodePool; for(int i = cnt - 1; i > 0; i--) if(sam.b[i]->appear == (1 << 2) - 1 && sam.b[i]->right == 2)//这个串只出现两次且属于不同串 ans = min(ans, sam.b[i]->mi);//取长度最小的即可 if(ans == 1e9) ans = -1; printf("%d\n", ans); return 0;}
后缀数组做法如下:
Result : Accepted Memory : 400 KB Time : 31 ms
/* * Author: Gatevin * Created Time: 2015/4/16 20:11:25 * File Name: Rin_Tohsaka.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;#define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)#define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl#define maxn 10010int wa[maxn], wb[maxn], wv[maxn], Ws[maxn];int cmp(int *r, int a, int b, int l){ return r[a] == r[b] && r[a + l] == r[b + l];}void da(int *r, int *sa, int n, int m){ int *x = wa, *y = wb, *t, i, j, p; for(i = 0; i < m; i++) Ws[i] = 0; for(i = 0; i < n; i++) Ws[x[i] = r[i]]++; for(i = 1; i < m; i++) Ws[i] += Ws[i - 1]; for(i = n - 1; i >= 0; i--) sa[--Ws[x[i]]] = i; for(j = 1, p = 1; p < n; j <<= 1, m = p) { for(p = 0, i = n - j; i < n; i++) y[p++] = i; for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j; for(i = 0; i < n; i++) wv[i] = x[y[i]]; for(i = 0; i < m; i++) Ws[i] = 0; for(i = 0; i < n; i++) Ws[wv[i]]++; for(i = 1; i < m; i++) Ws[i] += Ws[i - 1]; for(i = n - 1; i >= 0; i--) sa[--Ws[wv[i]]] = y[i]; for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } return;}int rank[maxn], height[maxn];void calheight(int *r, int *sa, int n){ int i, j, k = 0; for(i = 1; i <= n; i++) rank[sa[i]] = i; for(i = 0; i < n; height[rank[i++]] = k) for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++); return;}char in[maxn];int s[maxn], sa[maxn], belong[maxn];int N;int main(){ scanf("%s", in); int len = strlen(in); N = 0; for(int i = 0; i < len; i++) s[N] = in[i] - 'a' + 1, belong[N++] = 1; belong[N] = 0; s[N++] = 27;//刚开始逗比地写的26就WA了... scanf("%s", in); len = strlen(in); for(int i = 0; i < len; i++) s[N] = in[i] - 'a' + 1, belong[N++] = 2; s[N] = 0; da(s, sa, N + 1, 30); calheight(s, sa, N); int ans = 1e9; for(int i = 1; i <= N; i++) if(height[i] > 0)//s[sa[0]] = 0, s[sa[N]] = 26不考虑 if(belong[sa[i - 1]] != belong[sa[i]])//说明这两个不属于同一个串, 接下来保证P在各个串中只出现一次 { if(i == N && height[i - 1] < height[i]) ans = min(ans, height[i - 1] + 1); if(i == 1 && height[i + 1] < height[i]) ans = min(ans, height[i + 1] + 1); if(i > 1 && i < N && height[i - 1] < height[i] && height[i + 1] < height[i]) ans = min(ans, max(height[i - 1], height[i + 1]) + 1); } if(ans == 1e9) ans = -1; printf("%d\n", ans); return 0;}
0 0
- Codeforces 427D Match & Catch 后缀自动机 或 后缀数组
- Codeforces 427D Match & Catch 后缀自动机
- codeforces 427 D Match & Catch ( 后缀数组 )
- CodeForces 427D Match & Catch 后缀数组
- Codeforces 427D Match & Catch(后缀自动机)
- CodeForces 427 D.Match & Catch(后缀数组)
- cf 427D Match & Catch 后缀数组
- CF(427D-Match & Catch)后缀数组应用
- cf244D. Match & Catch 字符串hash (模板)或 后缀数组。。。
- codeforces 123 D String(后缀自动机 SAM)
- codeforces 127D Password (后缀自动机 SAM)
- Codeforces 427 D. Match & Catch
- CodeForces 427D-Match & Catch
- CodeForces 427D-Match & Catch
- CodeForces 427D Match & Catch
- 【后缀自动机】自动机<->后缀树<->后缀数组
- POJ 1509 Glass Beads 后缀自动机 或 后缀数组
- HDU 4622 Reincarnation 后缀数组 或 后缀自动机
- POJ2251 Dungeon Master(搜索BFS)
- C++之继承与多态
- CuckooDroid Book
- 线性代数导论9——线性相关性、基、维数
- c++中 . 和 -> 的区别
- Codeforces 427D Match & Catch 后缀自动机 或 后缀数组
- 设置MyEclipse编码、补全快捷键、字体大小
- 线性代数导论10——四个基本子空间
- android AndroidManifest.xml中android:configChanges的简介
- 关于前端中几个加载触发事件
- 再论Iterator
- 线性代数导论11——矩阵空间、秩1矩阵和小世界图
- C - 剪花布条 HDU2087( kmp找子串,子串不能有重叠 )
- POJ 1797Heavy Transportation Kruskal并查集求解