SOJ-4484-后缀数组

题目链接 :  SOJ-4484


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.

Author

_L


题目大意:给你一个字符串,求出有多少种不同的子串(连续)包括本身  见样例


题目思路:  看了很久的后缀数组了,,一直都不太懂怎么来的,之后用模板处理一些简单的问题,,这道题关键是对后缀数组height数组的理解,,具体我这里就不描述了,,答案就是所有的子串的个数减去height数组里的值,,这里会超int,,所以要用long long  


AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

const int maxn = 1e5;

int cntA[maxn],sa[maxn],Rank[maxn],cntB[maxn],height[maxn],A[maxn],B[maxn],tsa[maxn];
char ch[maxn];
int n;
void solve()
{
    for (int i = 0; i < 256; i ++) cntA[i] = 0;
    for (int i = 1; i <= n; i ++) cntA[ch[i-1]] ++;
    for (int i = 1; i < 256; i ++) cntA[i] += cntA[i - 1];
    for (int i = n; i; i --) sa[cntA[ch[i-1]] --] = i;
    Rank[sa[1]] = 1;
    for (int i = 2; i <= n; i ++)
    {
        Rank[sa[i]] = Rank[sa[i - 1]];
        if (ch[sa[i]-1] != ch[sa[i - 1]-1]) Rank[sa[i]] ++;
    }
    for (int l = 1; Rank[sa[n]] < n; l <<= 1)
    {
        for (int i = 0; i <= n; i ++) cntA[i] = 0;
        for (int i = 0; i <= n; i ++) cntB[i] = 0;
        for (int i = 1; i <= n; i ++)
        {
            cntA[A[i] = Rank[i]] ++;
            cntB[B[i] = (i + l <= n) ? Rank[i + l] : 0] ++;
        }
        for (int i = 1; i <= n; i ++) cntB[i] += cntB[i - 1];
        for (int i = n; i; i --) tsa[cntB[B[i]] --] = i;
        for (int i = 1; i <= n; i ++) cntA[i] += cntA[i - 1];
        for (int i = n; i; i --) sa[cntA[A[tsa[i]]] --] = tsa[i];
        Rank[sa[1]] = 1;
        for (int i = 2; i <= n; i ++)
        {
            Rank[sa[i]] = Rank[sa[i - 1]];
            if (A[sa[i]] != A[sa[i - 1]] || B[sa[i]] != B[sa[i - 1]]) Rank[sa[i]] ++;
        }
    }
    for (int i = 1, j = 0; i <= n; i ++)
    {
        if (j) j --;
        while (ch[i + j-1] == ch[sa[Rank[i] - 1] + j-1]) j ++;
        height[Rank[i]] = j;
    }
}

int main()
{
    int t;cin>>t;
    while(t--){
    scanf("%s",ch);
    n=strlen(ch);
    solve();
    long long ans = (long long)(1+n)*n/2;
    for(int i=1;i<=n;i++)
        ans-=(long long)height[i];
    cout<<ans<<endl;
    }
    return 0;
}


全部评论

相关推荐

05-10 16:48
门头沟学院 Java
程序员小白条:主要原因,投递太晚了,快手应该早点溜了,你都从去年9月开始的,半年也差不多3月跑路了,这样的话,至少有5个以上的面试机会
点赞 评论 收藏
分享
今天 10:10
已编辑
门头沟学院 人工智能
写这篇之前我犹豫了挺久。一方面是怕被人骂,&quot;又一个收割焦虑的转行帖&quot;;另一方面是看了太多用&nbsp;GPT&nbsp;套娃出来的「学习路线」文章,AI&nbsp;味重得让人没法读完。所以这篇全是亲身踩过的坑,时间线、用过的项目、当时的心路全都尽量原样写出来。如果你是大学生在迷茫要不要转&nbsp;AI,或者已经在转的路上,希望能给点参考。&nbsp;一个反共识的开场:你以为进&nbsp;OpenAI&nbsp;的人都是博士?&nbsp;先讲个故事,跟我没关系,但跟所有想转&nbsp;AI&nbsp;的人都有关系。&nbsp;OpenAI&nbsp;的&nbsp;Sora&nbsp;团队(就是搞文生视频那个)一共&nbsp;13&nbsp;个人。这里面有两个人特别有意思:&nbsp;Will&nbsp;DePue,密歇根大学计算机系,直接辍学了。17...
_hengheng:我也本,也算是做ai相关,我最开始感觉做ai工程师有多么多么困难,后来发现懂了原理后整体训练完全可以看成一个流程化的内容,开源方案太多了,大多基本都是按着模子在自家业务上做各种操作,就算是大厂的小部门也没那么多资源去训基模,反而更多的是像怎么把技术往业务方向靠近了,不过当前时代如果本科学历没那么好加上自己执行力不是特别强还真不建议走ai工程师这条路,可以试试其他ai的偏业务方向,不然校招不太好杀出来
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务