后缀自动机(SAM)
来源:互联网 发布:杭州瑞德设计 知乎 编辑:程序博客网 时间:2024/06/04 19:00
记录一个菜逼的成长。。
好文推荐
个人感觉通俗易懂。
这里已hihocoder第128周为例。
hihocoder第128周
上面也有讲解。
题意:一个字符串里有几种不同的子串
答案:SAM所有状态的字符串数的和就是。每个状态的字符串数 = (max(s) - min(s) + 1)
max(s):是状态s所能表示的最大字符串长度
min(s)同理。
这里记下简易模板。
#include <bits/stdc++.h>using namespace std; #define fi first#define se secondtypedef long long LL;const int maxn = 1000000 + 10;struct SAM{ int link[maxn<<1],go[maxn<<1][26],mxlen[maxn<<1],mnlen[maxn<<1]; int tot,last; void init(){ tot = last = 0; memset(go[0],-1,sizeof(go[0])); link[0] = -1; mxlen[0] = 0;mnlen[0] = 0; } void ins(int c){ int p = last,cur = ++tot; mxlen[cur] = mxlen[p] + 1; memset(go[cur],-1,sizeof(go[cur])); while(p != -1 && go[p][c] == -1)go[p][c] = cur,p = link[p]; if (p == -1) { mnlen[cur] = 1; link[cur] = 0; } else { int q = go[p][c]; if (mxlen[q] == mxlen[p] + 1) mnlen[cur] = mxlen[q] + 1,link[cur] = q; else { int nq = ++tot; memcpy(go[nq],go[q],sizeof(go[q])); mxlen[nq] = mxlen[p] + 1; link[nq] = link[q]; mnlen[q] = mxlen[nq] + 1; link[cur] = nq; mnlen[cur] = mxlen[nq] + 1; link[q] = nq; while(p != -1 && go[p][c] == q) { go[p][c] = nq,p = link[p]; } mnlen[nq] = mxlen[link[nq]] + 1; } } last = cur; }}sam;char str[maxn];int main(){ while(~scanf("%s",str)) { sam.init(); for (int i = 0; str[i]; i++)sam.ins(str[i] - 'a'); LL ans = 0; for (int i = 1; i <= sam.tot; i++) { ans += sam.mxlen[i] - sam.mnlen[i] + 1; } printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- [SAM]后缀自动机
- 后缀自动机SAM
- Sam后缀自动机模板
- 后缀自动机SAM
- 后缀自动机SAM
- 后缀自动机(SAM)
- spoj lcs2 后缀自动机SAM
- hdu4436 str2int 后缀自动机 SAM
- 后缀自动机(SAM)学习指南
- poj 3415 SAM后缀自动机
- HDU 4270 SAM 后缀自动机
- 【后缀自动机sam学习小记】
- 后缀自动机(SAM)模板
- 【后缀自动机】【SAM】【自动机】【数据结构】后缀自动机理解(入门)
- codeforces 123 D String(后缀自动机 SAM)
- spoj 1811 LCS (后缀自动机 SAM)
- codeforces 127D Password (后缀自动机 SAM)
- codeoforces 271 D (后缀自动机 SAM)
- redis命令详解
- [UnityUI]循环滑动列表
- 从字符串中取数
- hdu1856 More is better
- 使用freemarker导出word,wps可以打开,office打不开问题
- 后缀自动机(SAM)
- 30+关于如何成为更好Android开发者的简短而专业的建议
- Xcode 9 快速跳转到定义新姿势(Jump to Definition)
- Java API学习(一)java包
- Android 蓝牙遥控器的连接
- 游戏服务器架构演进(完整版)
- 手机/移动前端开发需要注意的20个要点
- codevs1520 回文字符串
- abstract 抽象类