[kmp] hdu6153 A Secret

13 篇文章 0 订阅
11 篇文章 0 订阅

@(ACM题目)[字符串, kmp]

Description

Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,which have a big secret.SF is interested in this secret and ask VS how to get it.There are the things that VS tell:
Suffix(S2,i) = S2[i…len].Ni is the times that Suffix(S2,i) occurs in S1 and Li is the length of Suffix(S2,i).Then the secret is the sum of the product of Ni and Li.
Now SF wants you to help him find the secret.The answer may be very large, so the answer should mod 1000000007.

Input

Input contains multiple cases.
The first line contains an integer T,the number of cases.Then following T cases.
Each test case contains two lines.The first line contains a string S1.The second line contains a string S2.
1<=T<=10.1<=|S1|,|S2|<=1e6.S1 and S2 only consist of lowercase ,uppercase letter.

Output

For each test case,output a single line containing a integer,the answer of test case.
The answer may be very large, so the answer should mod 1e9+7.

Sample Input

2
aaaaa
aa
abababab
aba

Sample Output

13
19

Hint

case 2:
Suffix(S2,1) = “aba”,
Suffix(S2,2) = “ba”,
Suffix(S2,3) = “a”.
N1 = 3,
N2 = 3,
N3 = 4.
L1 = 3,
L2 = 2,
L3 = 1.
ans = (3*3+3*2+4*1)%1000000007.

题目分析

题目给定模式串 P 和文本串T,求 ΣLiti ,其中 Li P 的后缀i的长度, ti 为它在 T 中出现的次数。

将两个字符串反转,转化为前缀相关问题。对于T中每个前缀 Ti ,统计其后缀对答案的贡献 resi ,则 Σresi 即为答案。

现计算 resi ,在“ T 的前缀Ti”的所有后缀中,找到一个最长的后缀,使它是 P 的前缀,记这个P的前缀为 Pj ,显然 Pj 可以用kmp求得。

现在我们拿到了这个 Pj ,怎样计算 resi 呢?注意到这个 Pj 是模式串 P 的前缀,完全可以在kmp构建fail指针的过程中顺便计算出它对答案的贡献。

sumj+1 Pj 对答案的贡献,则只要将 Pj 中所有“前缀等于后缀”的子串的长度累和即可。

举例说明,对于字符串aabaax来说,考察x处的 sum5=lengthaabaa+lengthaa+lengtha=5+2+1=8

为什么计算“前缀等于后缀”的子串呢?以上面的例子说明,若在 Ti 到后缀中匹配到aabaa这个字符串,如前所说,aabaa也是 P <script type="math/tex" id="MathJax-Element-696">P</script>的前缀,那么由aabaa的后缀aabaaaaaaabaa的前缀,就可知它们也是P的前缀了。

如果觉得此法不容易想,我们可以把它当做模板题来做,详见这里

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

const int maxn = 1e6 + 5;
const LL M = 1e9 + 7;
char a[maxn]{0}, b[maxn]{0};
LL sum[maxn];
int fail[maxn];

void getFail(char* P)
{
    int m = strlen(P);
    fail[0] = fail[1] = 0;
    for(int i = 0; i <= m; ++i) sum[i] = i;
    for(int i = 1; i < m; i++)
    {
        int j = fail[i];
        while(j && P[i] != P[j]) j = fail[j];
        if(P[i] == P[j])
        {
            fail[i + 1] = j + 1;
            sum[i + 1] = (sum[i + 1] + sum[j + 1]) % M;
        }
        else fail[i + 1] = 0;
    }
}

LL finda(char* T, char* P)
{
    LL res = 0;
    int n = strlen(T), m = strlen(P);
    getFail(P);
    int j = 0;
    for(int i = 0; i < n; i++)
    {
        while(j && T[i] != P[j]) j = fail[j];
        if(T[i] == P[j]) ++ j;
        res = (res + sum[j]) % M;
    }
    return res;
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        scanf("%s%s", a, b);
        reverse(a, a + strlen(a));
        reverse(b, b + strlen(b));
        cout << finda(a, b) << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值