C - Distinct Substrings (模板)
https://vjudge.net/problem/SPOJ-DISUBSTR
有两种方式来求去除重读的子串
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ; int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l) {
return r[a]==r[b] &&r[l+a] == r[l+b];
} void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m)
{
n++;
int p,*x=t1,*y=t2;
for(int i = ; i < m; i++) c[i] = ;
for(int i = ; i < n; i++) c[x[i] = str[i]]++;
for(int i = ; i < m; i++) c[i] += c[i-];
for(int i = n-; i>=; i--) sa[--c[x[i]]] = i;
for(int j = ; j <= n; j <<= ) {
p = ;
for(int i = n-j; i < n; i++) y[p++] = i;
for(int i = ; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
for(int i = ; i < m; i++) c[i] = ;
for(int i = ; i < n; i++) c[x[y[i]]]++ ;
for(int i = ; i < m; i++) c[i] += c[i-];
for(int i = n-; i >= ; i--) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = ;
x[sa[]] = ;
for(int i = ; i < n; i++)
x[sa[i]] = cmp(y,sa[i-],sa[i],j)? p-:p++;
if(p >= n) break;
m = p;
}
int k = ;
n--;
for(int i = ; i <= n; i++)
Rank[sa[i]] = i;
for(int i = ; i < n; i++) {
if(k) k--;
int j = sa[Rank[i]-];
while(str[i+k] == str[j+k]) k++;
height[Rank[i]] = k;
}
} int Rank[maxn],height[maxn];
int sa[maxn];
char str[maxn];
int a[maxn];
int n; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int tot = ;
scanf("%s",str);
int len = strlen(str);
for(int i = ;i < len; i++)
a[i] = str[i];
a[len] = ;
get_sa(a,sa,Rank,height,len,);
long long res = ;
long long ans = (len+)*len/;
for(int i=;i<=len;i++){
res+=len-sa[i]-height[i];
ans-=height[i];
}
//printf("%lld\n",res);
printf("%lld\n", ans);
}
return ;
}
C - Distinct Substrings (模板)的更多相关文章
- 后缀数组:SPOJ SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ694 New Distinct Substrings
New Distinct Substrings 题目大意 给定一个字符串,求本质不同的子串个数 题解 SA常见思想:每一个子串都是某个后缀的前缀 考虑每一个后缀的贡献,首先他拥有n - sa[i]个( ...
- 后缀数组---New Distinct Substrings
Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...
- SPOJ Distinct Substrings【后缀数组】
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- SpringBoot2.0之整合ActiveMQ(发布订阅模式)
发布订阅模式与前面的点对点模式很类似,简直一毛一样 注意:发布订阅模式 先启动消费者 公用pom: <project xmlns="http://maven.apache.org/PO ...
- deepin 安装微信与QQ
安装QQ sudo apt-get install deepin.com.qq.im 安装微信 sudo apt-get install deepin.com.wechat 附录 其他安装包 http ...
- 【转】PHP生成器 (generator)和协程的实现
原文地址:https://phphub.org/topics/1430 1.一切从 Iterator 和 Generator 开始 为便于新入门开发者理解,本文一半篇幅是讲述迭代器接口(Iterato ...
- legend2---开发日志14(游戏对用户友好的设计思路)
legend2---开发日志14(游戏对用户友好的设计思路) 一.总结 一句话总结: 不强制,但是激励:比如宗门灵力等级从强制提升到提升宗门和用户的修炼速度 1.丹药有必要做成随机数值么? 没有 1. ...
- java 基础 - 反转字符串
反转字符串 public class Main { public static void main(String[] args) { String newStr= strReverseWithArra ...
- String类中的equals是如何重写的
我们知道String中的equals方法是被重写过的,因为object的equals方法是比较的对象的内存地址,而String的equals方法比较的是对象的值. 首先几个知识点: 基本数据类型==比 ...
- PS 色调— —颜色梯度
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- Python 实现「食行生鲜」签到领积分
用过食行生鲜的同学应该知道,每天可以在食行生鲜签到,签到可以领到 20 积分,在购物时可以抵 2 毛钱.钱虽少,但是积少成多,买菜时可以抵扣一两块钱还是不错的. 今天我们就用 Python 来实现自动 ...
- 【旧文章搬运】VC插件中如何获取当前工程的工作目录
原文发表于百度空间,2014-09-24========================================================================== 好难找的资 ...
- 在python 3.6下用pip 安装第三方库,比如pip install requests,老是报错 Fatal error in launcher: Unable to create process using '"'
解决办法:我把python.exe 修改为了python3.exe ,为了兼容python2, 后来把python2从环境变量里删除,把python3.exe修改为了python.exe 就解决了,再 ...