New Distinct Substrings

题目大意

给定一个字符串,求本质不同的子串个数

题解

SA常见思想:每一个子串都是某个后缀的前缀

考虑每一个后缀的贡献,首先他拥有n - sa[i]个(我是用的模板中,sa[i]的大小是0....n-1)前缀,这些前缀有height[i]个跟sa[i-1]相同,要减去。剩下的部分不可能与sa[i-1]之前的想通了,不然sa[i]会排在sa[i-1]前面

还要注意本题的字符集是小写字母(鬼知道样例是什么东西)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <cmath> void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
void swap(int* &a, int* &b){int *tmp = a;a = b;b = tmp;}
int max(int a, int b){return a > b ? a : b;}
int min(int a, int b){return a < b ? a : b;}
void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXN = 50000 + 10; struct SuffixArray
{
int s[MAXN], sa[MAXN], rank[MAXN], height[MAXN];
int t[MAXN], t2[MAXN], c[MAXN];
int n;
void clear(){n = 0;memset(sa, 0, sizeof(sa));} void build_sa(int m)
{
++ n;
int i,*x=t,*y=t2;
for(i=0;i<m;++i) c[i]=0;
for(i=0;i<n;++i) x[i]=s[i];
for(i=0;i<n;++i) c[x[i]]++;
for(i=1;i<m;++i) c[i]+=c[i-1];
for(i=n-1;i>=0;--i) sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(i=n-k;i<n;++i) y[p++]=i;
for(i=0;i<n;++i) if(sa[i]>=k) y[p++]=sa[i]-k;
for(i=0;i<m;++i) c[i]=0;
for(i=0;i<n;++i) c[x[i]]++;
for(i=1;i<m;++i) c[i]+=c[i-1];
for(i=n-1;i>=0;--i) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;x[sa[0]]=0;
for(i=1;i<n;++i)
x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k])?p-1:p++;
if(p>=n) break;
m=p;
}
-- n;
} void build_height()
{
int i,j,k=0;
for(i=1;i<=n;++i) rank[sa[i]]=i;
for(i=0;i<n;++i)
{
if(k) k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
}A; int t, ans;
char tmp[MAXN]; int main()
{
read(t);
for(;t;-- t)
{
ans = 0;
scanf("%s", tmp);
A.s[0] = tmp[0] - 'a' + 1;
for(A.n = 1;tmp[A.n];++ A.n) A.s[A.n] = tmp[A.n] - 'a' + 1;
A.s[A.n] = 0;
A.build_sa(30);
A.build_height(); //调试信息
/*for(int i = 1;i <= A.n;++ i)
printf("sa[%d]:%s\n", i, tmp + A.sa[i]);
for(int i = 1;i <= A.n;++ i)
printf("height[%d]:%d\n", i, A.height[i]); */ for(int i = 1;i <= A.n;++ i)
ans += A.n - A.sa[i] - A.height[i];
printf("%d\n", ans);
}
return 0;
}

SPOJ694 New Distinct Substrings的更多相关文章

  1. spoj694 DISUBSTR - Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  2. 【SPOJ694】Distinct Substrings (SA)

    求不相同子串个数    该问题等价于求所有后缀间不相同前缀的个数..也就是对于每个后缀suffix(sa[i]),将贡献出n-sa[i]+1个,但同时,要减去那些重复的,即为height[i],故答案 ...

  3. Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))

    Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...

  4. 后缀数组---New Distinct Substrings

    Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...

  5. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

  6. 后缀数组:SPOJ SUBST1 - New Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  7. DISUBSTR - Distinct Substrings

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  8. 705. New Distinct Substrings spoj(后缀数组求所有不同子串)

    705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...

  9. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

随机推荐

  1. iOS 多层级的immutable objects 转换成 mutable objects

    第一种方法是:将多层级的递归转换 方法: +(id) recursiveMutable:(id)object { if([object isKindOfClass:[NSDictionary clas ...

  2. hdu多校第九场 1005 (hdu6684) Rikka with Game 博弈

    题意: 给一个小写字母组成的字符串,每回合轮到某人时,此人可以选择让某位+1(如果是z则变回a),或者直接结束游戏. 先手希望游戏结束时字符串字典序尽量小,后手希望游戏结束时字符串字典序尽量大,求游戏 ...

  3. iOS开发之3D Touch

    1.简介 3DTouch是在6s之后苹果的一项技术,只能在6s及其以上机型真机运行,Xcode的模拟器是不支持的. Quick Actions(点击icon的快捷方式) Peek&Pop(应用 ...

  4. Python实现全局变量的两个解决方法

    Python实现全局变量的两个解决方法 本文针对Python的全局变量实现方法简述如下: 先来看下面一段测试程序:     count = 0 def Fuc(count):   print coun ...

  5. 基于Netty的RPC架构学习笔记(六):netty5案例学习

    文章目录 netty5服务端入门案例 netty5客户端入门案例 单客户端多连接程序 知识普及 线程池原理图 对象池原理图 对象组原理图 结论 理论结合实际 开干开干 总结 netty5服务端入门案例 ...

  6. Apache Solr 远程命令+XXE执行漏洞(CVE-2017-12629)

    Apache Solr 最近有出了个漏洞预警,先复习一下之前的漏洞 命令执行 先创建一个listener,其中设置exe的值为我们想执行的命令,args的值是命令参数 POST /solr/demo/ ...

  7. 剑指offer——16二进制中1的个数

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 有可能引起死循环解法: 每次判断最右端是不是1[与 & 1即可],是就cnt++,然后右移一位,直到num为0,结束 ...

  8. 13-MySQL-Ubuntu-数据表的查询-条件查询(二)

    条件查询 1,比较查询(>,<,>=,<=,=)注:SQL查询语句的等于号(=) (1)查询学生表中年龄大于18岁的学生姓名和性别信息 select name,gender f ...

  9. 002-Java数据类型

    Java数据类型 基本数据类型 和 引用数据类型 基本数据类型 整型:byte - 8bit / short - 2字节 / int - 4字节 / long - 8字节 浮点型:float doub ...

  10. boost相关函数

    1.boost::scoped_ptr是一个比较简单的智能指针,它能保证在离开作用域之后它所管理对象能被自动释放 #include <iostream> #include <boos ...