A - A Secret(扩展KMP)

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.

题目大意:

给出两个字符串,求第二个字符串的每个后缀在第一个字符串中出现的次数与后缀长度的乘积和;

思路概括:

我们将两个字符串翻转,然后就成为扩展kmp的题,在第一个字符串中找第二个字符串的前缀,求出extand数组,这里需要好好理解extand数组表示的是什么,然后对于每个extend求(1~extand)的和就行;

举个例子:第二组样例翻转后为

str1:babababa

str2:aba

extand:03030301;

extand[1]=3;表示从str1[1]开始能与str2的前三个字符匹配,所以在这个点a,ab,aba都出现了一次,次数乘以长度的和便为1+2+3,讲到这应该是很好懂了;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
char s[maxn];
char t[maxn];
int lens, lent;
int Next[maxn];
int extend[maxn];
void get_next()
{
    Next[0] = lent;
    int a = 0;
    int p = 0;
    for(int i=1;i<lent;i++)
    {
        if(i >= p || i + Next[i - a] >= p)
        {
            if(i >= p) p = i;
            while(p < lent && t[p] == t[p - i]) p++;
            Next[i] = p - i;
            a = i;
        }
        else Next[i] = Next[i - a];
    }
}
void exkmp()
{
    int a = 0;
    int p = 0;
    get_next();
    for(int i=0;i<lens;i++)
    {
        if(i >= p || i + Next[i - a] >= p)
        {
            if(i >= p) p = i;
            while(p < lens && p - i < lent && s[p] == t[p - i]) p++;
            extend[i] = p - i;
            a = i;
        }
        else extend[i] = Next[i - a];
    }
}
ll add(ll x)
{
    ll ans=x%mod*((x+1)%mod)%mod*500000004%mod;
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--)
    {
        cin >> s >> t;
        lens = strlen(s);
        lent = strlen(t);
        reverse(s, s + lens);
        reverse(t, t + lent);
        exkmp();
        ll ans = 0;
        for(int i=0;i<lens;i++)
        {
            if(extend[i])
            {
                ans = (ans + add(extend[i]))%mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值