HDU 5769 Substring 多校赛 (后缀数组)
2016-08-01 21:12
399 查看
Substring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 793 Accepted Submission(s): 318
Problem Description
?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings.
But ?? thinks that is 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 is 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 is 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(starting from 1) and y is the answer you get for that case.
Sample Input
2
a
abc
b
bbb
Sample Output
Case #1: 3
Case #2: 3
HintIn 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.
Author
FZU
Source
2016 Multi-University Training Contest 4
用后缀数组将不同串的个数求出,然后再将不符合条件的,即没有指定字符和与之前已经处理的字符串中存在重复的字符串减去就可以求出答案了
#include <cstdio> #include <cstdlib> #include <sstream> #include <iostream> #include <cmath> #include <cstring> #include <algorithm> #include <string> #include <utility> #include <vector> #include <queue> #include <map> #include <set> using namespace std; typedef long long ll; #define DEBUG(x) cout<< #x << ':' << x << endl #define FOR(i,s,t) for(int i = (s);i <= (t);i++) #define FORD(i,s,t) for(int i = (s);i >= (t);i--) #define REP(i,n) FOR(i,0,n-1) #define REPD(i,n) FORD(i,n-1,0) #define PII pair<int,int> #define PB push_back #define MP make_pair #define ft first #define sd second #define lowbit(x) (x&(-x)) #define INF (1<<30) const int maxn = 1e5 + 5; char s[maxn]; int sa[maxn],t1[maxn],t2[maxn],c[maxn]; char f[2]; int dp[maxn]; int _rank[maxn],height[maxn]; void getHeight(int n) { int k = 0; for(int i=1; i<=n; i++)_rank[sa[i]] = i; for(int i=0; i<n; i++) { if(k)k--; int j = sa[_rank[i]-1]; while(s[i+k]==s[j+k])k++; height[_rank[i]] = k; } } bool cmp(int *r,int a,int b,int l) { return (r[a]==r[b] && r[a+l]==r[b+l]); } void build_sa(int m,int n) { int i,*x=t1,*y=t2,k,p; 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(k=1,p=0; p<n; m=p,k<<=1) { p = 0; for(i=n-k; i<n; i++)y[p++] = i; for(i=0; i<n; i++)if(sa[i]>=k)y[p++] = sa[i]-k; 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]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++; } getHeight(n-1); } ll solve(int n) { ll ans = 0; for(int i=1; i<=n; i++) { int Max = max((dp[sa[i]] - sa[i]), height[i]); ans += n - sa[i] - Max; } return ans; } int main() { //freopen("in","r",stdin); //freopen("out","w",stdout); int T; cin>>T; int cas = 1; while(T--) { scanf("%s%s",f,s); int n = strlen(s); dp = n; for(int i = n - 1; i >= 0; i --) { if(s[i] == f[0]) dp[i] = i; else dp[i] = dp[i + 1]; } build_sa(255,n+1); printf("Case #%d: %lld\n",cas ++, solve(n)); } return 0; }
相关文章推荐
- HDU 5769 Substring (后缀数组)
- HDU-5769-Substring(后缀数组)
- hdu_5769_Substring(后缀数组)
- HDU 5769 Substring(后缀数组)
- HDU 5769 Substring (后缀数组)
- HDU 5769 Substring(后缀数组)
- HDU 5769 Substring(后缀数组)
- HDU 5769 Substring(后缀数组)
- HDU 5769 Substring (后缀数组)
- HDU 5769 Substring(后缀数组)
- hdu 5769 Substring 后缀数组
- HDU 5769 Substring(后缀数组)
- poj - 2774 - Long Long Message / hdu - 1403 - Longest Common Substring(后缀数组)
- hdu-5769-Substring-后缀数组
- Hdu 5769 Substrings (后缀数组)
- hdu 1403 Longest Common Substring (后缀数组模板)
- hdu 5769 Substring 后缀数组
- HDU 5769 Substring
- hdu 5769-后缀数组
- HDU 1403-Longest Common Substring (后缀数组)