题目大意: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. Maven3(笔记二)

    笔记本二   在Eclipse 中使用Maven 第一节:m2eclipse 插件安装 打开Eclipse,点击菜单Help - > Install New Software 点击Add 按钮N ...

  2. ios专题 - socket(1)

    二,BSD socket API 简介 BSD socket API 和 winsock API 接口大体差不多,下面将列出比较常用的 API: API接口 讲解 int socket(int add ...

  3. 原生与jqueryDOM

    总结与复习原生与jquery的DOM操作. 获取元素节点: $(".class") $("#id") $(".class div") $(& ...

  4. 解决打不开jar包

    Java应用程序jar文件可以由 JVM(Java虚拟机)直接执行,只要操作系统安装了JVM便可以运行作为Java应用程序的jar文件,其跨平台特性使得很多工具软件都用jar方式来部署分发,比如用于H ...

  5. Windows下的 mysql 5.5主从同步配置

    环境说明:   Master:127.0.0.1 3306 Slave:127.0.0.1 3307     MySQL 的 Master 配置:   配置my.ini:   [mysqld]   # ...

  6. YZOI Easy Round 2_回文串 string

    原文链接:http://laphets1.gotoip3.com/?id=18 Description 给出一个由小写字母组成的字符串,其中一些字母被染黑了,用?表示.已知原来的串不是 一个回文串,现 ...

  7. http与https差异

    HTTPS和HTTP的区别: https协议需要到ca申请证书,一般免费证书很少,需要交费. http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议 http的连接很 ...

  8. php多行字符串输出

      $content_header =<<<CONTENT_HEADER <section class="content-header"> <h ...

  9. 移动端触摸滑动插件Swiper

    移动端触摸滑动插件Swiper 04/02/2015 一.了解Swiper 目前移动端项目一般都需要具有触屏焦点图的效果,如果你也需要实现这一功能的话,Swiper是一个不错的选择. 1.他不需要加载 ...

  10. C#快递单号查询源码

    源码本人测试过,没有啥问题,能查询快递单号,支持的快递还挺多,圆通快递.申通快递.韵达快递的都支持单号查询的,程序是通过向爱快递(www.aikuaidi.cn)接口传输参数来查询快递单号,我直接把代 ...