题目大意:RT

 
分析:练手题目....后缀数组确实很强大.....多理解height数组, 切勿使用模版,后缀数组本身就有很多细节,多犯错更有利理解这个算法。
 
代码如下:
=====================================================================================================================
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN = ; struct SuffixArr
{
int tempx[MAXN], tempy[MAXN], text[MAXN];
int rank[MAXN], sa[MAXN], sum[MAXN], height[MAXN];
int *x, *y, N, MaxId; void GetText(char s[])
{
N = strlen(s)+;
x = tempx, y = tempy, MaxId=; for(int i=; i<N; i++)
{
text[i] = x[i] = (int)s[i];
y[i] = i;
}
}
bool cmp(int i, int len)
{
if(sa[i]+len>N || sa[i-]+len>N)
return false;
if(y[sa[i]] != y[sa[i-]] || y[sa[i]+len] != y[sa[i-]+len])
return false; return true;
}
void BaseSort()
{
for(int i=; i<MaxId; i++)
sum[i] = ;
for(int i=; i<N; i++)
sum[ x[ y[i] ] ] += ;
for(int i=; i<MaxId; i++)
sum[i] += sum[i-];
for(int i=N-; i>=; i--)
sa[ --sum[ x[ y[i] ] ] ] = y[i];
}
void GetSa()
{
BaseSort(); for(int len=; len<=N; len<<=)
{
int id = ; for(int i=N-len; i<N; i++)
y[id++] = i;
for(int i=; i<N; i++)if(sa[i] >= len)
y[id++] = sa[i]-len; BaseSort();
swap(x, y);
x[ sa[] ] = id = ; for(int i=; i<N; i++)
{
if(cmp(i, len) == true)
x[sa[i]] = id;
else
x[sa[i]] = ++id;
} MaxId = id+; if(MaxId >= N)break;
}
}
void GetHeight()
{
for(int i=; i<N; i++)
rank[sa[i]] = i; for(int k=, i=; i<N; i++)
{
if(!rank[i])
{
height[] = k = ;
continue;
}
if(k)k--; int pre = sa[ rank[i]- ]; while(text[i+k] == text[pre+k])
k++;
height[rank[i]] = k;
}
}
int TotalSonArr()
{
int sum = ; for(int i=; i<N; i++)
sum += N-sa[i]--height[i]; return sum;
}
void debug(int p[])
{
for(int i=; i<N; i++)
printf("%d ", p[i]);
printf("\n");
}
}; SuffixArr suf;
char s[MAXN]; int main()
{
int T; scanf("%d", &T); while(T--)
{
scanf("%s", s); suf.GetText(s);
suf.GetSa();
suf.GetHeight();
int ans = suf.TotalSonArr(); printf("%d\n", ans);
} return ;
}

Distinct Substrings - spoj 694(不重复子串个数)的更多相关文章

  1. SPOJ 694 && SPOJ 705 (不重复子串个数:后缀数组)

    题意 给定一个字符串,求它的所有不重复子串的个数 思路 一个字符串的子串都必然是它的某个后缀的前缀.对于每一个sa[i]后缀,它的起始位置sa[i],那么它最多能得到该后缀长度个子串(n-sa[i]个 ...

  2. Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)

    Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...

  3. ACdream 1430——SETI——————【后缀数组,不重叠重复子串个数】

    SETI Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statist ...

  4. SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)

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

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

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

  6. Distinct Substrings SPOJ - DISUBSTR 后缀数组

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

  7. Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)

    求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...

  8. spoj 694. Distinct Substrings 后缀数组求不同子串的个数

    题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...

  9. [SPOJ]DISUBSTR:Distinct Substrings&[SPOJ]SUBST1:New Distinct Substrings

    题面 Vjudge Vjudge Sol 求一个串不同子串的个数 每个子串一定是某个后缀的前缀,也就是求所有后缀不同前缀的个数 每来一个后缀\(suf(i)\)就会有,\(len-sa[i]+1\)的 ...

随机推荐

  1. UITableView中容易忽略的知识点

    1.取消余下的分割线 tableView.tableFooterView = UIView() 2.分割线顶格 override func viewDidLayoutSubviews() { self ...

  2. Python:字典

    #!/usr/bin/python3 #dict 字典 #字典是可变的 dict1 = {"name":"张三","age":22} pri ...

  3. mysql 刘道成视频教程 第4-8课 --- 数据类型

    数据类型大纲图: 注:在mysql中,输入时,除了数值型,不要加单引号,其他的都要加上单引号,养成一种好习惯. 一.数值型: 整数型: 1)从数学上来讨论tinyint 1. 占据空间 2.存储范围 ...

  4. mysql 刘道成视频教程1、2课----------大致结构

    第1课 第2课 一.整体结构 1.mysql -h localhost -u root -p *** 如果-h不写,则默认连接localhost. 2.连接服务器成功,-------> 显示数据 ...

  5. MySQL设置

    在MySQL的使用中很容易出现乱码的情况. 实际上在MySQL中有个地方表明了系统中所用到的所有的字符集. 例如: 从中可以看出,对于server和database的默认字符集都是latin1,这样很 ...

  6. javascript图片预先加载

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. apache2.4配置虚拟主机

    step1 启用 httpd-vhosts.conf 找到E:/apache/Apache24/conf 中httpd.conf 文件,取消注释下面这句话 step2 在 httpd-vhosts.c ...

  8. baba 运动网

    import com.sun.image.codec.jpeg.* 找不到包     在Myeclipse中编译项目时,如果提示类似 com.sun.image.codec.jpeg.* 下: imp ...

  9. initWithCoder: 与initWithFrame:

    之前一直用代码来编写画面,现在着手使用storyboard和xib来构筑画面,遇到initWithCoder方法, 故查了下,initWithCoder方法的调用,看了篇博客,链接如下: http:/ ...

  10. svn代码统计

    http://chenzhou123520.iteye.com/blog/1436653