后缀数组(不相同的子串个数)——SPOJ 705
来源:互联网 发布:c语言做游戏 编辑:程序博客网 时间:2024/06/05 23:01
对应SPOJ题目:点击打开链接
Distinct Substrings
Time Limit: 159MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
Description
Given a string, we need to find the total number of its distinct substrings.
Input
T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 50000
Output
For each test case output one number saying the number of distinct substrings.
Example
Sample Input:
2
CCCCC
ABABA
Sample Output:
5
9
Explanation for the testcase with string ABABA:
len=1 : A,B
len=2 : AB,BA
len=3 : ABA,BAB
len=4 : ABAB,BABA
len=5 : ABABA
Thus, total number of distinct substrings is 9.
Source
题意:求给出的每个字符串的不相同子串数目思路:后缀数组(不相同子串个数)的基本应用
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MS(x, y) memset(x, y, sizeof(x)) const int MAXN = 1000+10; const int INF = 1<<30; int wa[MAXN],wb[MAXN],wv[MAXN],ws[MAXN]; int rank[MAXN],r[MAXN],sa[MAXN],height[MAXN]; char str[MAXN]; int cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a+l] == r[b+l]; } void da(int *r, int *sa, int n, int m) { int i, j, p, *x = wa, *y = wb, *t; for(i=0; i<m; i++) ws[i] = 0; for(i=0; i<n; i++) ws[x[i] = r[i]]++; for(i=1; i<m; i++) ws[i] += ws[i-1]; for(i=n-1; i>=0; i--) sa[--ws[x[i]]] = i; for(j=1,p=1; p<n; j<<=1, m=p){ for(p=0,i=n-j; i<n; i++) y[p++] = i; for(i=0; i<n; i++) if(sa[i] >= j) y[p++] = sa[i] - j; for(i=0; i<n; i++) wv[i] = x[y[i]]; for(i=0; i<m; i++) ws[i] = 0; for(i=0; i<n; i++) ws[wv[i]]++; for(i=1; i<m; i++) ws[i] += ws[i-1]; for(i=n-1; i>=0; i--) sa[--ws[wv[i]]] = y[i]; for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++) x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++; } return; } void calheight(int *r, int *sa, int n) { int i, j, k = 0; for(i=1; i<n; i++) rank[sa[i]] = i; for(i=0; i<n-1; height[rank[i++]] = k) for(k ? k-- : 0,j=sa[rank[i]-1]; r[i+k] == r[j+k]; k++); return; } int main(){//freopen("in.txt", "r", stdin);int T;scanf("%d", &T);while(T--){MS(r, 0);MS(sa, 0);MS(rank, 0);MS(height, 0);MS(ws, 0);MS(wv, 0);MS(wa, 0);MS(wb, 0);scanf("%s", str);int i;int len = strlen(str);for(i=0; i<len; i++)r[i] = (int)str[i] - 31;//可显示字符的范围是 32~126,r[len++] = 0;da(r, sa, len, 100);calheight(r, sa, len);int ans = 0;for(i=1; i<len; i++)ans += len - 1 - sa[i] - height[i];printf("%d\n", ans);}return 0;}
0 0
- 后缀数组(不相同的子串个数)——SPOJ 705
- SPOJ 694. Distinct Substrings (不相同的子串的个数--后缀数组)
- SPOJ 694 / SPOJ DISUBSTR Distinct Substrings【后缀数组】不相同的子串的个数
- SPOJ - Distinct Substrings / SPOJ - New Distinct Substrings(后缀数组 - 不相同子串个数)
- SPOJ DISUBSTR - Distinct Substrings(后缀数组[不相同的子串的个数])
- SPOJ SUBST1 - New Distinct Substrings(后缀数组[不相同的子串的个数])
- SPOJ - DISUBSTR Distinct Substrings(后缀数组求不相同的子串个数)
- SPOJ 705(后缀数组求单个子串的不重复子串个数)
- 后缀数组(不相同子串的个数)spoj694
- SPOJ 694,705(不相同的子串个数)
- SPOJ 694 Distinct Substrings(后缀数组 所有不相同子串个数)
- SPOJ 705 子串个数 后缀数组
- spoj694 不相同的子串的个数(后缀数组)
- SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)
- spoj 694 不相同的子串的个数
- spoj 694 不相同的字串的个数 对后缀数组又了解了一些
- 【后缀数组求不相同的子串的个数】spoj694 spoj705
- SPOJ694 Distinct Substrings, 后缀数组, 不相同的子串的个数
- 数论里面的定理
- 【动态调试so文件】 + AliCrackMe_2分析记录
- 驯服腾讯的QQ(在此我谴责一下国内的软件制造商)
- android 日记
- USACO5.3.2 Window Area(window)
- 后缀数组(不相同的子串个数)——SPOJ 705
- Hadoop中Partitioner解析
- 编程珠玑之第三章习题5
- 关于正则表达式的常规用法
- Unity3D_IOS研究院之构建游戏框架与导出IOS项目(一)
- BASIC-20 数的读法
- Machine Learning - III. Linear Algebra Review线性代数 (Week 1, Optional)
- C Socket Programming server client
- 统揽《运筹学基础》