Substring
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 485 Accepted Submission (s): 202
Problem Description?? Is practicing his program skill, and now he's given a string, he has to calculate the total number of its distinct substr Ings.
But?? Thinks that's too easy, he wants to make this problem more interesting.
?? Likes a character x very much, so he wants to know the number of distinct substrings which contains at least one X.
However,?? is unable to solve it, please help him.
Input the first line of the input gives the number of test cases T; T test Cases follow.
Each test case is consist of 2 lines:
First line was a character X, and second line is a string S.
X is a lowercase letter, and S contains lowercase letters (' a '-' Z ') only.
T<=30
1<=| S|<=10^5
The Sum of | s| In all the test cases are no more than 700,000.
Output for each test case, output one line containing ' case #x: Y ' (without quotes), where x is the ' test Case number (Starti Ng from 1) and Y-is the answer.
Sample Input
2 a ABC b BBB
Sample Output
Case #1:3 case #2:3 Hint In first case, all distinct substrings containing at least one a:a, AB, ABC. In second case, all distinct substrings containing at least one b:b, BB, BBB.
Test instructions: give you a character and a string that asks you the number of different substrings in the string that contain the character.
Analysis: Find out the position of the nearest required character behind all suffixes f[i], for suffix sa[i], the contribution to the answer is N-max (Sa[i]+height[i],f[sa[i]).
#include <iostream> #include <string> #include <algorithm> #include <cstdlib> #include < cstdio> #include <set> #include <map> #include <vector> #include <cstring> #include <stack
> #include <cmath> #include <queue> using namespace std;
#define INF 0x3f3f3f3f//rank starting from 0//sa starting from 1, because the last character (the smallest) is ranked in No. 0 bit//height starting from 2 because it represents sa[i-1] and sa[i] const int MAXN = 100005;
int T,N,M,RANK[MAXN],SA[MAXN],X[MAXN],Y[MAXN],HEIGHT[MAXN],S[MAXN],F[MAXN];
int BUC[MAXN];
Char str[maxn],c[2];
void Calheight (int n) {int I, j, k = 0;
for (i = 1; I <= n; i++) rank[sa[i]] = i;
for (i = 0; i < n; height[rank[i++]] = k) for (k?k--:0, j = sa[rank[i]-1]; s[i+k] = = S[j+k]; k++); } BOOL CMP (int *r,int a,int b,int l) {return (r[a] = = R[b] && r[a+l] = = R[b+l]);} void suffix (int n,int m =
() {int i, L, p, *x = x, *y = y;
for (i = 0; i < m; i + +) buc[i] = 0; for (i = 0; i < n; i + +) buc[X[i] =S[i]] + +;
for (i = 1; i < m; i + +) Buc[i] + = buc[i-1];
for (i = n-1; I >= 0; i-) sa[--buc[x[i]] = i;
for (L = 1,p = 1; p < n; m = p, l *= 2) {p = 0;
for (i = n-l; i < n; i + +) y[p++] = i;
for (i = 0; i < n; i + +) if (Sa[i] >= l) y[p++] = sa[i]-l;
for (i = 0; i < m; i + +) buc[i] = 0;
for (i = 0; i < n; i + +) buc[X[y[i]] + +;
for (i = 1; i < m; i + +) Buc[i] + = buc[i-1];
for (i = n-1; I >= 0; i-) sa[--buc[x[y[i]] [= Y[i]; for (Swap (x, y), x[sa[0]] = 0, i = 1, p = 1; i < n; i + +) x[Sa[i]] = CMP (y,sa[i-1],sa[i],l)?
P-1: p++;
} calheight (n-1);//suffix array The key is to find the height, so when the SA is the way to find rank and height of the} int main () {scanf ("%d", &t);
for (int t = 1;t <= t;t++) {scanf ("%s", C);
scanf ("%s", str);
int n = strlen (str);
for (int i = 0;i < n;i++) S[i] = Str[i];
S[n] = 0;
int now = n; for (int i = N-1;i >= 0;i--) {if (Str[i] = = C[0]) now = i;
F[i] = now;
} suffix (n+1,128);
Long Long tot = 1ll*n-max (sa[1],f[sa[1]]);
for (int i = 2;i <= n;i++) tot + = 1ll*n-max (Sa[i]+height[i],f[sa[i]]);
printf ("Case #%d:%i64d\n", T,tot); }
}