spoj 8222 Substrings(后缀自动机)
来源:互联网 发布:linux 查看代理服务器 编辑:程序博客网 时间:2024/05/21 23:17
题目链接:spoj 8222 Substrings
代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 250005;const int SIGMA_SIZE = 26;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) { if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 26; return ch -'a'; } void init() { sz = 0, last = 1; newNode(0); } void insert(char ch); void get_tuopu(); void get_right(char* str); int f[maxn]; void solve (int n) { memset(f, 0, sizeof(f)); for (int i = 1; i <= sz; i++) { int u = step[i]; f[u] = max(f[u], right[i]); } for (int i = n-1; i; i--) f[i] = max(f[i], f[i+1]); for (int i = 1; i <= n; i++) printf("%d\n", f[i]); }}SA;int K;char s[maxn];int main () { scanf("%s", s); SA.init(); int n = strlen(s); for (int i = 0; i < n; i++) SA.insert(s[i]); SA.get_tuopu(); SA.get_right(s); SA.solve(n); 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
- spoj 8222 Substrings(NSUBSTR),后缀自动机
- spoj 8222 Substrings(后缀自动机)
- SPOJ 8222 NSUBSTR - Substrings (后缀自动机)
- spoj 8222 Substrings (后缀自动机)
- spoj 8222 substrings 【后缀自动机】
- SPOJ Substrings --后缀自动机
- spoj Substrings【后缀自动机】
- spoj 8222 Substrings (后缀自动机+dp)
- [后缀自动机 模板题] SPOJ 8222 Substrings
- SPOJ 8222 Substrings 后缀自动机入门
- SPOJ 8222. Substrings(后缀自动机模板)
- SPOJ NSUBSTR Substrings 后缀自动机
- SPOJ - NSUBSTR Substrings 后缀自动机
- SPOJ 题目 8222 NSUBSTR - Substrings(后缀自动机+DP求子串出现最大次数)
- SPOJ 8222 [后缀自动机]
- 后缀自动机小结 (spoj 8222)
- SPOJ 8222 NSUBSTR 后缀自动机
- SPOJ LCS(后缀自动机)
- 同步异步堵塞非堵塞
- Deprecated: Function xxx() is deprecated
- 一个maven项目打多个可执行Jar文件
- UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法
- Android APP应用开发中异常处理
- spoj 8222 Substrings(后缀自动机)
- java.io.EOFException错误
- xcode git 忽略user interface state文件
- android 通知栏背景颜色跟随app导航栏背景颜色变化而变化
- Android Theme的设置
- [BZOJ1001][BeiJing2006]狼抓兔子
- linux下c/c++实例之九识别中文字符
- Android布局拉到尽头取消渐变颜色
- [服务器架构]微服务的深入思考