hdu5769Substring 字尾陣列

2021-07-15 22:34:47 字數 1276 閱讀 6906

題意:

給你乙個字元x和乙個字串s,問你s中有多少個不相同的子串?且必須含有字元x。

題解:

1、我們可以利用字尾陣列來做這道題。我們求出我們要的三個陣列ra,sa,height。(字尾陣列求出來的三個陣列)

2、我們知道乙個字尾能夠貢獻出n-sa[i]+1-height[i]個不相同的子串,而我們要包含字元x的話,我們就只要標記出某個位置後面第乙個x的位置就可以了(記為nc[i]),我們就能求到乙個字尾能夠貢獻出n-1-max(sa[i]+height[i],nc[sa[i]])。

**:

#include#include#include#include#include#include#include#include#include#include#include#include#include#define ll long long

#define pb push_back

#define pa pair#define clr(a,b) memset(a,b,sizeof(a))

#define lson lr<<1,l,mid

#define rson lr<<1|1,mid+1,r

#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)

#define key_value ch[ch[root][1]][0]

#pragma comment(linker, "/stack:102400000000,102400000000")

const ll mod = 1000000007;

const int n = 1e5+15;

const int maxn = 1e6+15;

const int letter = 130;

const int inf = 1e9+7;

const double pi=acos(-1.0);

const double eps=1e-10;

using namespace std;

inline int read()

while(ch>='0'&&ch<='9')

return x*f;

}char x,s[n];

int sa[n],ra[n],height[n],c[maxn],n,t2[n],t[n],k,nc[n];

void build_sa(int n,int m)

}void build_height(int n)

}ll sum=0;

for(int i=1;i

HDU 5769字尾陣列

題意 給你乙個串,問你含某個特定的字元的不同子串有多少種。思路 使用字尾陣列。很久之前看過做過幾個模板水題。以為自己做不到這麼高深的題目。結果這次就卡了,想hash莽一下發現很不科學,就放棄了。看來字尾陣列也已經是標配了。趕緊再補充補充自己。首先退一步講,字尾陣列如何求不同子串個數。其實就是利用排序...

HDU 5769 Substring 字尾陣列

傳送門 思路 不考慮x字元,先考慮怎麼求本質不同的串有幾個,那麼就是列舉一遍height陣列,去掉字首相同的就可以了。我們考慮x字元的話,就要保證所有求得的串都要包含這個字元。那麼我們事先求乙個陣列,pos i 表示當前字元 包括自身 的右邊的第乙個x字元的位置。然後還是列舉height陣列,當前字...

HDU 5769 Substring(字尾陣列)

求字串 s 本質不同且一定包含字元 x 的子串個數。1 leq s leq 10 5 求乙個長度為 n 的串 s 本質不同的子串個數可以使用公式 displaystyle sum n sa i 1 h i 其中 h 0 1 這個公式是比較好證的。字尾的字首就是子串,我們分析每個字尾有多少個對答案有貢...