多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > 互聯(lián)網(wǎng) > SPOJ - DISUBSTR Distinct Substrings(后綴數(shù)組求不相同的子串個(gè)數(shù))

SPOJ - DISUBSTR Distinct Substrings(后綴數(shù)組求不相同的子串個(gè)數(shù))

來(lái)源:程序員人生   發(fā)布時(shí)間:2014-10-10 08:00:00 閱讀次數(shù):4081次

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 <= 1000

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.

題意:給定一個(gè)字符串,求不相同的子串。
思路:每個(gè)子串一定是某個(gè)后綴的前綴,那么原問(wèn)題等價(jià)于求所有后綴之間的不相同的前綴的個(gè)數(shù)。如果所有的后綴按照 suffix(sa[1]), suffix(sa[2]),
suffix(sa[3]), …… ,suffix(sa[n])的順序計(jì)算,不難發(fā)現(xiàn),對(duì)于每一次新加
進(jìn)來(lái)的后綴 suffix(sa[k]),它將產(chǎn)生 n-sa[k]個(gè)新的前綴。但是其中有height[k]個(gè)是和前面的字符串的前綴是相同的。所以 suffix(sa[k])將“貢獻(xiàn)”
出 n-sa[k]-height[k]個(gè)不同的子串。累加后便是原問(wèn)題的答案。

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 1010; int sa[maxn]; //SA數(shù)組,表示將S的n個(gè)后綴從小到大排序后把排好序的 //的后綴的開(kāi)頭位置順次放入SA中 int t1[maxn], t2[maxn], c[maxn]; int rank[maxn], height[maxn]; int s[maxn]; char str[maxn]; void build_sa(int s[], int n, int m) { int i, j, p, *x = t1, *y = t2; for (i = 0; i < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[i] = s[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 (j = 1; j <= n; j <<= 1) { p = 0; for (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 < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[y[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-1]] == y[sa[i]] && y[sa[i-1]+j] == y[sa[i]+j] ? p-1 : p++; if (p >= n) break; m = p; } } void getHeight(int s[],int n) { int i, j, k = 0; for (i = 0; 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; } } int check(int n, int k, int mid) { int num = 1; for (int i = 2; i <= n; i++) { if (height[i] >= mid) { num++; if (num >= k) return 1; } else num = 1; } return 0; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%s", str); int n = strlen(str); for (int i = 0; i <= n; i++) s[i] = str[i]; build_sa(s, n+1, 128); getHeight(s, n); int ans = 0; for (int i = 1; i <= n; i++) ans += n - sa[i] - height[i]; printf("%d ", ans); } return 0; }

生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 中国一级毛片国产高清 | 色77777 | 中文字幕在线观看免费视频 | 国产亚洲精品热视频在线观看 | 国产一级一级一级成人毛片 | 99精品国产高清一区二区 | 在线观看视频网站www色 | 一区二区三区四区在线播放 | 国产高清在线精品免费不卡 | free3dvideos性欧洲 | 国产专区一va亚洲v天堂 | 国产精品福利资源在线 | 老司机福利免费 | 日韩免费观看一级毛片看看 | 色噜噜狠狠先锋影音久久 | 97se亚洲综合在线 | 亚洲一区二区三区免费 | 3344成年站福利在线视频免费 | 校园春色 中文字幕 | 国产肥老妇 | 欧美国产日韩久久久 | 日本aaaaa特黄毛片 | 成人一区二区免费中文字幕 | 亚洲伊人久久大香线蕉综合图片 | 欧美xxxx做受视频 | 国产精品成人久久久 | 福利区站 | 精品亚洲福利一区二区 | 亚洲精品国产精品国自产 | 伊人精品视频在线观看 | 中文字幕最新在线 | 最近更新在线中文字幕一页 | 亚洲精品国产精品一区二区 | 中文字幕一区二区三区四区五区 | 福利国产| 亚洲视频在线视频 | 亚洲免费视频一区二区三区 | 亚洲丝袜另类 | 亚洲手机在线 | 免费一级在线 | 日韩一级在线视频 |