F - New Distinct Substrings (后缀数组)
题目链接:https://cn.vjudge.net/contest/283743#problem/F
题目大意:给你一个字符串,然后让你求出不同的子串的个数。
具体思路:首先,一个字符串中总的子串个数是len*(len+1)/2,然后就开始去重了,通过height数组,求出所有重复的子串的个数,然后就用总的子串的个数-重复的子串的个数就可以了。
AC代码:
#include<iostream>
#include<stack>
#include<cstring>
#include<iomanip>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
# define ll long long
const int maxn = 5e5+;
int cntA[maxn], cntB[maxn], sa[maxn], tsa[maxn], A[maxn], B[maxn], height[maxn];
int Rank[maxn];
char ch[maxn];
ll n;
//sa[i]代表第i小的后缀位置,Rank[i]代表第i位置开始的后缀在所有的后缀串中排名第几
// height[i]代表排名第i个字符串和第i-1个字符串的相同前缀的长度
void cal()
{
for(int i = ; i < ; i++) cntA[i] = ;
for(int i = ; i <= n; i++) cntA[ch[i-]]++;
for(int i = ; i < ; i++) cntA[i] += cntA[i-];
for(int i = n; i; i--) sa[cntA[ch[i-]]--] = i;
Rank[sa[]] = ;
for(int i = ; i <= n; i++)
{
Rank[sa[i]] = Rank[sa[i-]];
if(ch[sa[i]-] != ch[sa[i-]-]) Rank[sa[i]]++;
}
for(int l = ; Rank[sa[n]] < n; l <<= )
{
memset(cntA, , sizeof(cntA));
memset(cntB, , sizeof(cntB));
for(int i = ; i <= n; i++)
{
cntA[A[i] = Rank[i]]++;
cntB[B[i] = (i+l <= n)?Rank[i+l]:]++;
}
for(int i = ; i <= n; i++) cntB[i] += cntB[i-];
for(int i = n; i; i--) tsa[cntB[B[i]]--] = i;
for(int i = ; i <= n; i++) cntA[i] += cntA[i-];
for(int i = n; i; i--) sa[cntA[A[tsa[i]]]--] = tsa[i];
Rank[sa[]]=;
for(int i = ; i <= n; i++)
{
Rank[sa[i]] = Rank[sa[i-]];
if(A[sa[i]] != A[sa[i-]] || B[sa[i]] != B[sa[i-]]) Rank[sa[i]]++;
}
}
for(int i = , j = ; i <= n; i++)
{
if(j) j--;
while(ch[i+j-] == ch[sa[Rank[i]-] + j - ]) j++;
height[Rank[i]] = j;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",ch);
n=strlen(ch);
if(n==)
{
printf("1\n");
continue;
}
// cout<<1<<endl;
cal();
ll ans=n*(n+)/;
//cout<<ans<<endl;
for(int i=; i<=n; i++)
{
ans-=height[i];
// cout<<i<<" "<<height[i]<<endl;
}
printf("%lld\n",ans);
}
return ;
}
F - New Distinct Substrings (后缀数组)的更多相关文章
- SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数
题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- 【SPOJ – SUBST1】New Distinct Substrings 后缀数组
New Distinct Substrings 题意 给出T个字符串,问每个字符串有多少个不同的子串. 思路 字符串所有子串,可以看做由所有后缀的前缀组成. 按照后缀排序,遍历后缀,每次新增的前缀就是 ...
- SPOJ DISUBSTR Distinct Substrings 后缀数组
题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...
- SPOJ 694 || 705 Distinct Substrings ( 后缀数组 && 不同子串的个数 )
题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀 ...
- spoj Distinct Substrings 后缀数组
给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...
- [spoj694&spoj705]New Distinct Substrings(后缀数组)
题意:求字符串中不同子串的个数. 解题关键:每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数. 1.总数减去height数组的和即可. 注意这里height中为什么不需 ...
- spoj 694. Distinct Substrings 后缀数组求不同子串的个数
题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...
- SPOJ_705_New Distinct Substrings_后缀数组
SPOJ_705_New Distinct Substrings_后缀数组 题意: 给定一个字符串,求该字符串含有的本质不同的子串数量. 后缀数组的一个小应用. 考虑每个后缀的贡献,如果不要求本质不同 ...
- SPOJ- Distinct Substrings(后缀数组&后缀自动机)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- java List 根据属性排序
Collections.sort(fileItems, new Comparator<FileItem>() { public int compare(FileItem arg0, Fil ...
- 13函数式编程&Stream流
13.1常用的函数式接口总结 接口名称 方法名称 抽象/默认 延迟/终结 方法描述 Supplier get 抽象 终结 供给型接口,无参有返回值,主要用于 Consumer accept 抽象 ...
- Uva101-STL模拟
一道有点复杂的STL模拟题,对STL迭代器不太熟悉改了好久,最后总算A了出来. 感觉用数组更方便...但是为了练习STL嘛 对比白书上的代码,我写的还是傻了点.一开始没有理解四个操作的意思,单纯的模拟 ...
- 自学Python6.2-类、模块、包
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- 【BZOJ3668】【NOI2014】起床困难综合症(贪心)
[NOI2014]起床困难综合症(贪心) 题面 Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚 ...
- [hdu3966]Aragorn's Story
传送门 题目描述 Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One ...
- 安装 Minio服务
#MINIO SERVER Minio是在Apache License v2.0下发布的对象存储服务器.它与Amazon S3云存储服务兼容. 它最适合存储非结构化数据,如照片,视频,日志文件,备份和 ...
- luogu3305/bzoj3130 费用流 (二分答案+dinic)
Bob肯定想挑一个流量最大的边,然后把所有的费用都加给它呗 那Alice就让流量最大的边尽量小呗 那就二分一下答案再dinic呗 #include<bits/stdc++.h> #defi ...
- Java -- JDBC 学习--通过Statement进行数据库更新操作
通过 JDBC 向指定的数据表中插入一条记录. 1. Statement: 用于执行 SQL 语句的对象 1). 通过 Connection 的 createStatement() 方法来获取 2). ...
- SqlServer存储过程详解
SqlServer存储过程详解 1.创建存储过程的基本语法模板: if (exists (select * from sys.objects where name = 'pro_name')) dro ...