技术笔记:SCU4484

简介: 技术笔记:SCU4484

The Graver Robbers' Chronicles


Description


One day, Kylin Zhang and Wu Xie are trapped in a graveyard. They find an ancient piece of parchment with a cipher string.


After discussion and analysis, they find the password is the total number of the cipher string's distinct substrings.


Although Kylin Zhang is powerful and always help his friends get rid of danger, this time he is helpless beacause


he is not good at math and programming.


As a smart Acmer, can you help them solve this problem so that they can escape from this horrible graveyard.


Input


The first line is an integer T stands for the number of test cases.


Then T test case follow.


For each test case there is one string.


Constraints:


T is no bigger than 30.


The length of string is no bigger than 50000.


Every string only contains lowercase letters.


Output


For each test case, output the answer in a single line. one number saying the number of distinct substrings.


Sample Input


2


aaaaa


cacac


Sample Output


5


9


Hint


For test case 2, the distinct substrings of 'cacac' are as follows:


len = 1 : c , a


len = 2 : ca , ac


len = 3 : cac , aca


len = 4 : caca , acac


len = 5 : cacac


Thus, total number of distinct substrings is 9.


分析:字符串的处理问题,查重的时候使用后缀数组就可以了。


算出总的子串 len(len+1)/2


再减去重复的 Height(i)


即可


套用了一下 大大的后缀数组模板,地址:


代码如下:


#include


#include


#include


#define LL long long


#define ULL unsigned long long


using namespace std;


const int MAXN=100010;


//以下为倍增算法求后缀数组


int wa【MAXN】,wb【MAXN】,wv【MAXN】,Ws【MAXN】;


int cmp(int //代码效果参考:http://www.lyjsj.net.cn/wz/art_23208.html

r,int a,int b,int l)

{return r【a】==r【b】&&r【a+l】==r【b+l】;}


/< 传入参数:str,sa,len+1,ASCII_MAX+1 /


void da(const char r【】,int sa【】,int n,int m)


{


int i,j,p,x=wa,y=wb,t;


for(i=0; i

for(i=0; i

for(i=1; i

for(i=n-1; i>=0; i--) sa【--Ws【x【i】】】=i;


/cout["SA"[endl;;


for(int i=0;i<n+1;i++)cout[sa【i】[' ';/


for(j=1,p=1; p

{


for(p=0,i=n-j; i

for(i=0; i=j) y【p++】=sa【i】-j;


for(i=0; i

for(i=0; i

for(i=0; i

//代码效果参考:http://www.lyjsj.net.cn/wz/art_23206.html

for(i=1; i

for(i=n-1; i>=0; i--) sa【--Ws【wv【i】】】=y【i】;


for(t=x,x=y,y=t,p=1,x【sa【0】】=0,i=1; i

x【sa【i】】=cmp(y,sa【i-1】,sa【i】,j)?p-1:p++;


}


return;


}


int sa【MAXN】,Rank【MAXN】,height【MAXN】;


//求height数组


/< str,sa,len /


void calheight(const char r,int sa,int n)


{


int i,j,k=0;


for(i=1; i<=n; i++) Rank【sa【i】】=i;


for(i=0; i

for(k?k--:0,j=sa【Rank【i】-1】; r【i+k】==r【j+k】; k++);


// Unified


for(int i=n;i>=1;--i) ++sa【i】,Rank【i】=Rank【i-1】;


}


int main()


{


char r【MAXN】;


int m,n,t;


LL H,ans;


cint;


while(t--)


{


cinr;


n=strlen(r);


m=127;


da(r,sa,n+1,m+1);


calheight(r,sa,n);


H=n;


ans=(H+1)H/2;


for(int i=2;i<=n;i++)


{


ans-=height【i】;


}


cout[ans[endl;


}


}

相关文章
|
2月前
|
CDN
阿里云CDN怎么收费?看这一篇就够了,CDN不同计费模式收费价格全解析
阿里云CDN的费用由基础费用和增值费用组成。基础费用有三种计费方式:按流量、按带宽峰值和月结95带宽峰值,默认为按流量计费,价格根据使用量阶梯递减。增值费用包括静态HTTPS请求、QUIC请求等,按实际使用量收费,不使用不收费。具体收费标准和详细规则可参考阿里云官方页面。
|
5月前
|
弹性计算
新手必看,阿里云国际购买服务器带宽如何选择
新手必看,阿里云国际购买服务器带宽如何选择
|
10月前
|
文字识别 API
印刷文字识别产品使用合集之购买了共享资源包该怎么使用
印刷文字识别(Optical Character Recognition, OCR)技术能够将图片、扫描文档或 PDF 中的印刷文字转化为可编辑和可搜索的数据。这项技术广泛应用于多个领域,以提高工作效率、促进信息数字化。以下是一些印刷文字识别产品使用的典型场景合集。
|
弹性计算 关系型数据库 MySQL
最全阿里云双11优惠活动攻略价格表,看这一篇就够!
最全阿里云双11优惠活动攻略价格表,看这一篇就够!2023阿里云双11优惠活动开启了,轻量2核2G3M带宽服务器87元一年、2核4G4M带宽165元一年,云服务器ECS经济型e实例2核2G3M固定带宽优惠价格99元一年,新老用户同享,并且续费不涨价,第二年99元续费
1529 2
|
存储 弹性计算 大数据
阿里云服务器简介、优势以及如何购买比较节省钱
阿里云服务器全方位介绍包括云服务器ECS优势、云服务器租用价格、云服务器使用场景及限制说明,阿里云服务器网分享云服务器ECS介绍
|
存储 关系型数据库 文件存储
云存储Clouder认证:基于存储产品快速搭建网盘—课时3:主要的存储类型
云存储Clouder认证:基于存储产品快速搭建网盘—课时3:主要的存储类型
|
存储 弹性计算 安全
《阿里云存储手册》——存储容量单位包SCU
《阿里云存储手册》——存储容量单位包SCU
243 0
|
弹性计算
《公共云弹性计算最佳实践-省钱窍门之三:突发性能实例》电子版地址
公共云弹性计算最佳实践-省钱窍门之三:突发性能实例
97 0
《公共云弹性计算最佳实践-省钱窍门之三:突发性能实例》电子版地址
|
存储 人工智能 Cloud Native
开发者社区精选直播合集(三十一)| 弹性、灵活、极速的文件存储NAS
大数据时代下云存储得到了飞速发展,现代存储行业历经30多年的发展,迭代出多种存储产品。NAS便是其一。
开发者社区精选直播合集(三十一)|  弹性、灵活、极速的文件存储NAS