Codeforces 427D Match & Catch(后缀自动机)
来源:互联网 发布:炮王 知乎 编辑:程序博客网 时间:2024/06/10 05:40
题目链接:Codeforces 427D Match & Catch
代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1e4 + 5;const int SIGMA_SIZE = 26;const int inf = 0x3f3f3f3f;struct SAM { int sz, last; int g[maxn<<1][SIGMA_SIZE], pre[maxn<<1], step[maxn<<1]; int pos[maxn<<1], right[maxn<<1], cnt[maxn<<1]; void newNode(int s) { step[++sz] = s; pre[sz] = 0; memset(g[sz], 0, sizeof(g[sz])); } int idx(char ch) { return ch -'a'; } void init() { sz = 0, last = 1; newNode(0); } void insert(char ch); void get_tuopu(); void get_right(char* str); int solve (char* str) { memset(cnt, 0, sizeof(cnt)); int p = 1, n = strlen(str); for (int i = 0; i < n; i++) { int v = idx(str[i]); if (g[p][v]) p = g[p][v]; else { while (p && g[p][v] == 0) p = pre[p]; if (p == 0) p = 1; else p = g[p][v]; } cnt[p]++; } for (int i = sz; i; i--) { int u = pos[i]; cnt[pre[u]] += cnt[u]; } int ret = inf; for (int i = 2; i <= sz; i++) { if (right[i] == 1 && cnt[i] == 1) ret = min(ret, step[pre[i]] + 1); } if (ret == inf) ret = -1; return ret; }}SA;char a[maxn], b[maxn];int main () { scanf("%s%s", a, b); int n = strlen(a); SA.init(); for (int i = 0; i < n; i++) SA.insert(a[i]); SA.get_tuopu(); SA.get_right(a); printf("%d\n", SA.solve(b)); return 0;}void SAM::insert(char ch) { newNode(step[last] + 1); int v = idx(ch), p = last, np = sz; while (p && !g[p][v]) { g[p][v] = np; p = pre[p]; } if (p) { int q = g[p][v]; if (step[q] == step[p] + 1) pre[np] = q; else { newNode(step[p] + 1); int nq = sz; for (int j = 0; j < SIGMA_SIZE; j++) g[nq][j] = g[q][j]; pre[nq] = pre[q]; pre[np] = pre[q] = nq; while (p && g[p][v] == q) { g[p][v] = nq; p = pre[p]; } } } else pre[np] = 1; last = np;}void SAM::get_tuopu() { for (int i = 0; i <= sz; i++) cnt[i] = 0; for (int i = 1; i <= sz; i++) cnt[step[i]]++; for (int i = 1; i <= sz; i++) cnt[i] += cnt[i-1]; for (int i = 1; i <= sz; i++) pos[cnt[step[i]]--] = i; }void SAM::get_right(char* str) { int p = 1, n = strlen(str); for (int i = 0; i <= sz; i++) right[i] = 0; for (int i = 0; i < n; i++) { int v = idx(str[i]); p = g[p][v]; right[p]++; } for (int i = sz; i; i--) { int u = pos[i]; right[pre[u]] += right[u]; }}
0 0
- Codeforces 427D Match & Catch 后缀自动机
- Codeforces 427D Match & Catch(后缀自动机)
- Codeforces 427D Match & Catch 后缀自动机 或 后缀数组
- codeforces 427 D Match & Catch ( 后缀数组 )
- CodeForces 427D Match & Catch 后缀数组
- CodeForces 427 D.Match & Catch(后缀数组)
- Codeforces 427 D. Match & Catch
- CodeForces 427D-Match & Catch
- CodeForces 427D-Match & Catch
- CodeForces 427D Match & Catch
- cf 427D Match & Catch 后缀数组
- CF(427D-Match & Catch)后缀数组应用
- Codeforces Round #244 (Div. 2)D (后缀自动机)
- codeforces 123 D String(后缀自动机 SAM)
- codeforces 127D Password (后缀自动机 SAM)
- Codeforces Round #244 (Div. 2) D. Match & Catch
- Codeforces round 244(div.2) D match&catch【字符串hash】
- codeforces Round #92 D——后缀自动机
- 软件开发 注释习惯重要性
- iOS入门编程1--iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- NOIP 数字反转
- 高人对libsvm的经典总结
- 非托管资源泄露
- Codeforces 427D Match & Catch(后缀自动机)
- Linux 给用户及用户组分配权限以及对文件目录的操作
- [hadoop2.7.1]I/O之SequenceFile最新API编程实例(写入、读取)
- Xcode快捷键
- hdu--4455+ Substrings+2012杭州区域赛C题+DP
- Swift-- 方法
- [TwistedFate]属性property
- zoj 3891 K-hash(后缀自动机)
- hdoj2053(switch game