luogu 2408 不同子串个数 (后缀数组)

2024-03-20 12:32

本文主要是介绍luogu 2408 不同子串个数 (后缀数组),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目背景

因为NOI被虐傻了,蒟蒻的YJQ准备来学习一下字符串,于是它碰到了这样一道题:

题目描述

给你一个长为N的字符串,求不同的子串的个数

我们定义两个子串不同,当且仅当有这两个子串长度不一样 或者长度一样且有任意一位不一样。

子串的定义:原字符串中连续的一段字符组成的字符串

输入格式

第一行一个整数N

接下来一行N个字符表示给出的字符串

输出格式

一行一个整数,表示不一样的子串个数

输入输出样例

输入 #1

5
aabaa

输出 #1

11

输入 #2

3
aba

输出 #2

5

说明/提示

请使用64位整数来进行输出

(具体来说,C++和C选手请使用long long 类型,pascal选手请使用Int64)

由于输入文件过大,请使用 高效的读入方法(具体的,c++和c选手请不要使用cin,pascal选手不需要管)

对于30%的数据,N\le 1000N≤1000

对于100%的数据,N\le 10^5N≤105

题目链接:https://www.luogu.org/problem/P2408

题目分析:求出height,枚举排名,对排名为i的后缀,长度为n - sa[i] + 1,其中存在的不同的子串个数需要减去公共前缀,即h[i]

所以答案为∑(n - sa[i] + 1 - h[i])

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
char s[MAX];
int n, m, sa[MAX], height[MAX];
int rk[MAX], tp[MAX], tax[MAX];bool cmp(int* r, int a, int b, int k) {return r[a] == r[b] && r[a + k] == r[b + k];
}void radix_sort() {for (int i = 0; i <= m; i++) {tax[i] = 0;}for (int i = 1; i <= n; i++) {tax[rk[tp[i]]]++;}for (int i = 1; i <= m; i++) {tax[i] += tax[i - 1];}for (int i = n; i >= 1; i--) {sa[tax[rk[tp[i]]]--] = tp[i];}
}void get_sa() {for (int i = 1; i <= n; i++) {rk[i] = s[i] - 'a' + 1;tp[i] = i;m = max(m, s[i] - 'a' + 1);}radix_sort();for (int j = 1, p = 0; p < n; j <<= 1, m = p) {p = 0;for (int i = n - j + 1; i <= n; i++) {tp[++p] = i;}for (int i = 1; i <= n; i++) {if (sa[i] > j) {tp[++p] = sa[i] - j;}}radix_sort();swap(tp, rk);rk[sa[1]] = p = 1;for (int i = 2; i <= n; i++) {rk[sa[i]] = cmp(tp, sa[i], sa[i - 1], j) ? p : ++p;}}
}void get_height() {for (int i = 1, j = 0; i <= n; i++) {if (j) {j--;}int prevPos = sa[rk[i] - 1];while (i + j <= n && prevPos + j <= n && s[i + j] == s[prevPos + j]) {j++;}height[rk[i]] = j;}
}int main() {scanf("%d %s", &n, s + 1);get_sa();get_height();// for (int i = 1; i <= n; i++) {//     printf("sa[%d] = %d rk[%d] = %d height[%d] = %d\n", i, sa[i], i, rk[i], i, height[i]);// }ll ans = 0;for (int i = 1; i <= n; i++) {int suffixLen = n - sa[i] + 1;ans += (ll) (suffixLen - height[i]);}printf("%lld\n", ans);
}

 

这篇关于luogu 2408 不同子串个数 (后缀数组)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/829455

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

poj2406(连续重复子串)

题意:判断串s是不是str^n,求str的最大长度。 解题思路:kmp可解,后缀数组的倍增算法超时。next[i]表示在第i位匹配失败后,自动跳转到next[i],所以1到next[n]这个串 等于 n-next[n]+1到n这个串。 代码如下; #include<iostream>#include<algorithm>#include<stdio.h>#include<math.

poj3261(可重复k次的最长子串)

题意:可重复k次的最长子串 解题思路:求所有区间[x,x+k-1]中的最小值的最大值。求sa时间复杂度Nlog(N),求最值时间复杂度N*N,但实际复杂度很低。题目数据也比较水,不然估计过不了。 代码入下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring

spoj705( 求不相同的子串个数)

题意:求串s的不同子串的个数 解题思路:任何子串都是某个后缀的前缀,对n个后缀排序,求某个后缀的前缀的个数,减去height[i](第i个后缀与第i-1 个后缀有相同的height[i]个前缀)。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstrin

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

XTU 1233 n个硬币连续m个正面个数(dp)

题面: Coins Problem Description: Duoxida buys a bottle of MaiDong from a vending machine and the machine give her n coins back. She places them in a line randomly showing head face or tail face o

C语言:柔性数组

数组定义 柔性数组 err int arr[0] = {0}; // ERROR 柔性数组 // 常见struct Test{int len;char arr[1024];} // 柔性数组struct Test{int len;char arr[0];}struct Test *t;t = malloc(sizeof(Test) + 11);strcpy(t->arr,