HDU 5769 Substring

来源:互联网 发布:手机淘宝怎样上架宝贝 编辑:程序博客网 时间:2024/05/23 19:44
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.
 
用后缀自动机搞定本质不同的子串,然后二分一下统计有几个包含给出的字母。
#include<set>#include<map>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-8;const int mod = 1e9 + 7;const int N = 1e5 + 10;const int INF = 0x7FFFFFFF;int T, sum[N], cas = 0;char x[2], s[N];class SAM{    const static int maxn = 500005;   //节点个数      const static int size = 26;     //字符的范围      const static char base = 'a';     //字符的基准      class node    {    public:        node *fa, *next[size];        int len, cnt;        node* clear(int x)        {            fa = 0; len = x; cnt = 0;            memset(next, 0, sizeof(next));            return this;        }    }nd[maxn];                      //节点的设置      node *root, *last;              //根节点,上一个节点      int tot;                        //总节点数  public:    void clear()    {        last = root = &nd[tot = 0];        nd[0].clear(0);        nd[0].cnt = 1;    }                               //初始化      int insert(char ch)    {        node *p = last, *np = nd[++tot].clear(p->len + 1);        last = np;        int x = ch - base;        while (p&&p->next[x] == 0) p->next[x] = np, p = p->fa;        if (p == 0) { np->fa = root; return np->cnt = np->len - np->fa->len; }        node* q = p->next[x];        if (p->len + 1 == q->len) { np->fa = q; return np->cnt = np->len - np->fa->len; }        node *nq = nd[++tot].clear(p->len + 1);        memcpy(nq->next, q->next, sizeof q->next);        nq->fa = q->fa;        q->fa = np->fa = nq;        while (p &&p->next[x] == q) p->next[x] = nq, p = p->fa;        return np->cnt = np->len - np->fa->len;    }                               //插入操作  }sam;int main(){    scanf("%d", &T);    while (T--)    {        scanf("%s%s", x, s + 1);        sam.clear();        LL ans = sum[0] = 0;        for (int i = 1; s[i]; i++) sum[i] = sum[i - 1] + (s[i] == x[0]);        for (int i = 1; s[i]; i++)        {            int x = sam.insert(s[i]);            int l = 1, r = x;            while (l <= r)            {                int mid = l + r >> 1;                if (sum[i] - sum[mid - 1]) l = mid + 1;                else r = mid - 1;            }            ans += l - 1;        }        printf("Case #%d: %lld\n", ++cas, ans);    }    return 0;}


0 0