http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1305

1305: Substring

Time Limit: 2 Sec  Memory Limit: 10 MB Submit: 12  Solved: 2 [Submit][Status][Web Board]

Description

Given a string s. The length of s is smaller than 1000. You are to caculate the number of different substrings of s.

Input

There are multiple test cases. Each test case contains one line, with a string s.You may assume that s only contains lowercase letters. You may assume that there are only ten test cases with the length of string s is bigger than 400.

Output

For each test case, you are only to output one integer,  the answer.

Sample Input

a
ac
abcd

Sample Output

1
3
10 【题解】:
后缀数组:用(1+2+。。。+len)-(height数组之后)
【code】:
 #include <iostream>
#include<string.h>
#include<stdio.h> using namespace std; #define maxn 10100
#define cls(x) memset(x, 0, sizeof(x))
int wa[maxn],wb[maxn],wv[maxn],wss[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];} void da(char *r,int *sa,int n,int m)
{
cls(wa);
cls(wb);
cls(wv);
cls(wss);
int i,j,p,*x=wa,*y=wb,*t;
for(i=;i<m;i++) wss[i]=;
for(i=;i<n;i++) wss[x[i]=r[i]]++;
for(i=;i<m;i++) wss[i]+=wss[i-];
for(i=n-;i>=;i--) sa[--wss[x[i]]]=i;
for(j=,p=;p<n;j*=,m=p)
{
for(p=,i=n-j;i<n;i++) y[p++]=i;
for(i=;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=;i<n;i++) wv[i]=x[y[i]];
for(i=;i<m;i++) wss[i]=;
for(i=;i<n;i++) wss[wv[i]]++;
for(i=;i<m;i++) wss[i]+=wss[i-];
for(i=n-;i>=;i--) sa[--wss[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
}
int rank[maxn],height[maxn];
void calheight(char *r,int *sa,int n)
{
cls(rank);
cls(height);
int i,j,k=;
for(i=;i<n;i++) rank[sa[i]]=i;
for(i=;i<n;height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-];r[i+k]==r[j+k]&&i!=j;k++);
return;
} char ca[maxn * ];
int sa[maxn]; int main()
{
while (cin >> ca)
{
int len = strlen(ca);
da(ca, sa, len+, );
calheight(ca,sa,len+);
int i,sum=;
for(i=;i<=len;i++)
{
sum+=height[i];
}
cout<<len*(len+)/-sum<<endl;
cls(ca);
}
return ;
}

csu 1305 Substring (后缀数组)的更多相关文章

  1. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  2. HDU 5769 Substring 后缀数组

    Substring Problem Description ?? is practicing his program skill, and now he is given a string, he h ...

  3. hdu_1403_Longest Common Substring(后缀数组的应用)

    题目链接:hdu_1403_Longest Common Substring 题意: 给你两个字符串,然你找最长的公共子串 题解: 后缀数组的经典应用,要找两个字符串的公共子串,那么就相当于找两个串的 ...

  4. POJ3693 Maximum repetition substring 后缀数组

    POJ - 3693 Maximum repetition substring 题意 输入一个串,求重复次数最多的连续重复字串,如果有次数相同的,则输出字典序最小的 Sample input ccab ...

  5. 2016多校联合训练4 F - Substring 后缀数组

    Description ?? is practicing his program skill, and now he is given a string, he has to calculate th ...

  6. hdu 5769 Substring 后缀数组 + KMP

    http://acm.hdu.edu.cn/showproblem.php?pid=5769 题意:在S串中找出X串出现的不同子串的数目? 其中1 <= |S| < $10^5$ 官方题解 ...

  7. HDU 5679 Substring 后缀数组判重

    题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...

  8. Substring (后缀数组 + 计数)

    题意:求出字符串中包含了某个字符的字符序列不一样的数量. 思路:其实主要的是找出每个被包含字符的数量,假设除了目标字符之外的所有字符都不一样,那么应该就很好求了,但是显然不可能,所以我们可以枚举每一个 ...

  9. hdu 1403 Longest Common Substring 后缀数组 模板题

    题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...

随机推荐

  1. hdu 4606 简单计算几何+floyd+最小路径覆盖

    思路:将所有的直线的两个端点和城市混在一起,将能直接到达的两个点连线,求一次floyd最短路径.二分枚举bag容量,然后按给的要先后占领的城市由前向后,把能到一步到达的建一条边.然后求一次最小路径覆盖 ...

  2. Centos系统使用代理上网时 yum的代理设置

    yum的存在使centos上软件的安装.配置.升级.卸载变得十分的方便,但是当安装centos的机器是通过代理服务器访问外网的话,yum的 使用就变得无从下手了,以下介绍以下怎样为yum配置代理地址及 ...

  3. HTML+CSS 实现水流流动效果

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

  4. live(),bind(),delegate()等事件绑定方法的区别及应用解析

    1 首先bind()方法是最直观的,但是也是弊端最大的. $('a').bind('click',function(){alert('that tickles!')}) 这和事件冒泡有直接关系,当我们 ...

  5. Stored Procedures with Multiple Result Sets

    Stored Procedures with Multiple Result Sets https://msdn.microsoft.com/en-us/data/jj691402.aspx

  6. 验证hashmap非线程安全

    http://www.blogjava.net/lukangping/articles/331089.html final HashMap<String, String> firstHas ...

  7. springMVC+Hibernate常用的配置文件

    每次写一个新的web项目时都要写配置文件.比较麻烦,现在把常用到的配置文件记录下来,方便以后使用 web.xml <?xml version="1.0" encoding=& ...

  8. 模板:strncpy函数

    参考链接:http://baike.baidu.com/view/1207711.htm strncpy 是 C语言的函数之一,来自 C语言标准库,定义于 string.h,char *strncpy ...

  9. 模板:多Case输入处理

    利用cin实现 while(cin >> value) { } 调试时使用Ctrl + Z 输入文件结束符

  10. hadoop下跑mapreduce程序报错

    mapreduce真的是门学问,遇到的问题逼着我把它从MRv1摸索到MRv2,从年前就牵挂在心里,连过年回家的旅途上都是心情凝重,今天终于在eclipse控制台看到了job completed suc ...