HDU 5769 Substring 多校赛 (后缀数组)

本文介绍了一种利用后缀数组解决特定字符子串计数问题的方法,通过高效算法实现不同子串的快速计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
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.
 

Author
FZU
 

Source
 
用后缀数组将不同串的个数求出,然后再将不符合条件的,即没有指定字符和与之前已经处理的字符串中存在重复的字符串减去就可以求出答案了
#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] = 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值