HDU 5769 Substring (后缀数组)

Substring

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2060    Accepted Submission(s): 842

 

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.

Source

2016 Multi-University Training Contest 4

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769

题目大意:求一个字符串中,包含给定字符且唯一的子串个数

题目分析:首先考虑唯一的子串,∑(1,n) n - sa[i] - height[i] + 1,要求包含给定字符X则对于每个sa[i],找到其后包含X的最近位置记为nxt[sa[i]],这个可以提前预处理出来,于是答案变成∑(1,n) n - max(sa[i] + height[i], nxt[sa[i]]) + 1

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
char s[MAX], ch[1];
int n, m, sa[MAX], height[MAX], nxt[MAX];
int rk[MAX], tp[MAX], tax[MAX];

bool cmp(int* r, int a, int b, int k) {
    return r[a] == r[b] && r[a + k] == r[b + k];
}

void radix_sort() {
    for (int i = 0; i <= m; i++) {
        tax[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
        tax[rk[tp[i]]]++;
    }
    for (int i = 1; i <= m; i++) {
        tax[i] += tax[i - 1];
    }
    for (int i = n; i >= 1; i--) {
        sa[tax[rk[tp[i]]]--] = tp[i];
    }
}

void get_sa() {
    for (int i = 1; i <= n; i++) {
        rk[i] = s[i] - 'a' + 1;
        tp[i] = i;
        m = max(m, s[i] - 'a' + 1);   
    }
    radix_sort();
    for (int j = 1, p = 0; p < n; j <<= 1, m = p) {
        p = 0;
        for (int i = n - j + 1; i <= n; i++) {
            tp[++p] = i;
        }
        for (int i = 1; i <= n; i++) {
            if (sa[i] > j) {
                tp[++p] = sa[i] - j;
            }
        }
        radix_sort();
        swap(rk, tp);
        rk[sa[1]] = p = 1;
        for (int i = 2; i <= n; i++) {
            rk[sa[i]] = cmp(tp, sa[i], sa[i - 1], j) ? p : ++p;
        }
    }
}

void get_height() {
    for (int j = 0, i = 1; i <= n; i++) {
        if (j) {
            j--;
        }
        int prevPos = sa[rk[i] - 1];
        while (i + j <= n && prevPos + j <= n && s[i + j] == s[prevPos + j]) {
            j++;
        }
        height[rk[i]] = j;
    }
}

int main() {
    int T;
    scanf("%d", &T);
    for (int ca = 1; ca <= T; ca++) {
        printf("Case #%d: ", ca);
        scanf("%s%s", ch, s + 1);
        n = strlen(s + 1);
        get_sa();
        get_height();
        // for (int i = 1; i <= n; i++) {
        //     printf("sa[%d] = %d height[%d] = %d rk[%d] = %d\n", i, sa[i], i, height[i], i, rk[i]);
        // }
        int okPos = n + 1;
        for (int i = n; i >= 1; i--) {
            if (s[i] == ch[0]) {
                okPos = i;
            }
            nxt[i] = okPos;
        }
        ll ans = 0;
        for (int i = 1; i <= n; i++) {
            ans += (ll) (n - max(sa[i] + height[i], nxt[sa[i]]) + 1);
        }
        printf("%I64d\n", ans);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值