Spoj SUBST1 New Distinct Substrings
2024-08-29 20:06:42
Given a string, we need to find the total number of its distinct substrings.
Input
T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 50000
Output
For each test case output one number saying the number of distinct substrings.
Example
Input:
2
CCCCC
ABABA Output:
5
9 就是让你求一下一个串的本质不同的子串的个数。
这个就等价于求一下后缀自动机每个节点的权值和,每个节点的权值等于(max{}-min{}+1)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
#define maxn 200005
int f[maxn],ch[maxn][26],cnt=1;
int n,siz[maxn],l[maxn],T,las=1;
int a[maxn],c[maxn];
char s[maxn];
ll ans=0; inline void init(){
cnt=las=1;
memset(f,0,sizeof(f));
memset(ch,0,sizeof(ch));
memset(c,0,sizeof(c));
siz[1]=l[1]=ans=0;
} inline void ins(int x){
int p=las,np=++cnt;
las=np,l[np]=l[p]+1;
siz[np]=1; for(;p&&!ch[p][x];p=f[p]) ch[p][x]=np;
if(!p) f[np]=1;
else{
int q=ch[p][x];
if(l[q]==l[p]+1) f[np]=q;
else{
int nq=++cnt;
l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
f[nq]=f[q];
f[q]=f[np]=nq;
for(;ch[p][x]==q;p=f[p]) ch[p][x]=nq;
}
}
} inline void build(){
for(int i=0;i<n;i++) ins(s[i]-'a');
} inline void solve(){
for(int i=1;i<=cnt;i++) ans+=(ll)(l[i]-l[f[i]]);
} int main(){
scanf("%d",&T);
while(T--){
init();
scanf("%s",s);
n=strlen(s);
build();
solve();
printf("%lld\n",ans);
} return 0;
}
最新文章
- Visual Studio (VS IDE) 你必须知道的功能和技巧 - 【.Net必知系列】
- C#窗体 LISTVIEW
- P1391 走廊泼水节
- C++ struct 定义及初始化
- 20145120黄玄曦 《java程序设计》 寒假学习总结
- 问题-安装XP时,提示不识别SATA硬盘
- ACPI电源管理中的S0 S1 S2 S3 S4 S5
- jquery ajax局部加载方法介绍
- 关于Java中Eclipse运行卡顿、未响应,Cpu100%的快速处理办法
- 翻译:CREATE DATABASE语句
- Ionic2 cordova angular2 打包到Android apk环境搭建
- HDU1348 Wall 凸包
- springboot2.x里面访问静态资源的坑
- tcc-transaction 分析
- 【20190228】JavaScript-数组的操作
- VScode编辑器使用
- 还原MongoDB dump备份出来的Bson数据
- 【转】每天一个linux命令(42):kill命令
- 【Python】模块
- MyBatis 返回(批量)新增数据的自增id
热门文章
- JMeter学习笔记(六) 文件下载接口测试
- python3 虚拟环境配置
- wxPython 界面编程的有关事件
- C#向上转型与向下转型(转)
- 孤荷凌寒自学python第二十三天python类的封装
- HDU 4763 Theme Section ( KMP next函数应用 )
- spring 配置问题记录1-@ResponseBody和favorPathExtension
- [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]
- CORS跨域cookie传递
- 【HDU 5961 传递】