P2408 不同子串个数

计算一个字符串的不同子串个数

两种方法,一种是\(dp\)出来\(SAM\)从起点开始的路径数量

另一种方法就是计算每个点的\(len[i]-len[link[i]]\)这个计算的就是这个等价类的不同串的数量

//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 2e5+7;
using LL = int_fast64_t;
struct SAM{
int len[MAXN],link[MAXN],tot,ch[MAXN][26],last;
LL f[MAXN];
void init(){ tot = last = 0; link[tot] = -1; memset(ch,0,sizeof(ch)); memset(f,0,sizeof(f)); }
void extend(int c){
int np = ++tot, p = last;
len[np] = len[p] + 1;
while(p!=-1 and !ch[p][c]){
ch[p][c] = np;
p = link[p];
}
if(p==-1) link[np] = 0;
else{
int q = ch[p][c];
if(len[p]+1==len[q]) link[np] = q;
else{
int clone = ++tot;
len[clone] = len[p] + 1;
link[clone] = link[q];
memcpy(ch[clone],ch[q],sizeof(ch[q]));
link[np] = link[q] = clone;
while(p!=-1 and ch[p][c]==q){
ch[p][c] = clone;
p = link[p];
}
}
}
last = np;
}
void dfs(int u = 0){
f[u] = 1;
for(int i = 0; i < 26; i++){
if(!ch[u][i]) continue;
if(!f[ch[u][i]]) dfs(ch[u][i]);
f[u] += f[ch[u][i]];
}
}
LL query(){
dfs(); return f[0] - 1;
LL ret = 0; for(int i = 1; i <= tot; i++) ret += len[i] - len[link[i]]; return ret;
//return f[0] - 1;
}
}sam;
char s[MAXN];
void solve(){
int n; cin >> n;
cin >> s;
sam.init();
for(int i = 0, l = strlen(s); i < l; i++) sam.extend(s[i]-'a');
cout << sam.query() << endl;
}
int main(){
solve();
return 0;
}

最新文章

  1. 关于css清除浮动,解决内容溢出的问题
  2. System V进程间通信
  3. css 设计总结
  4. IOS 100 - 1 开工闲聊
  5. [Python爬虫]cnblogs博客备份工具(可扩展成并行)
  6. dbus
  7. LoadRunner ---检查点
  8. Lua metatable &amp; metamethod
  9. MRI中T1和T2的含义与区分[转]
  10. macos port总结
  11. Mac下的截屏功能
  12. GD库使用小结---2
  13. UVA 10943 How do you add?
  14. linux之SQL语句简明教程---GROUP BY
  15. ubuntu命令行下java工程编辑与算法(第四版)环境配置
  16. MySQL架构备份之M-S-S级联备份
  17. 【转】Android 增,删,改,查 通讯录中的联系人
  18. 十个有意思的Github Page
  19. idea常用的插件
  20. Fedora 全局代理上网设置

热门文章

  1. Eclipse-Che 安装(Centos)
  2. 集成spring框架的web.xml
  3. ajax跨域访问http服务--jsonp
  4. innodb是怎么刷新日志缓冲的
  5. SDUST数据结构 - chap9 排序
  6. 记忆中的像素块褪色了吗?用开源的体素编辑器重新做个 3D 的吧!
  7. SAP 修改表和表中数据
  8. python3.8.1安装cx_Freeze
  9. 关于springboot项目通过jar包启动之后无法读取项目根路径静态资源
  10. mysql的安装使用及其用户管理