D - A Secret HDU - 6153

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   

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define mod 1000000007

using namespace std;
typedef long long ll;
ll Add(ll n)
{
    ll m=((n%mod)*((n+1)%mod)/2)%mod;
    return m;
}

void pre_exmp(char *x, int m, int ne[])
{
    ne[0] = m;
    int j = 0;
    while(j + 1 < m && x[j] == x[j + 1])j++;
    ne[1] = j;
    int k = 1;
    for(int i = 2; i < m; i++)
    {
        int p = ne[k] + k - 1;
        int L = ne[i - k];
        if(i + L < p + 1)ne[i] = L;
        else
        {
            j = max(0, p - i + 1);
            while(i + j < m && x[i + j] == x[j])j++;
            ne[i] = j;
            k = i;
        }
    }
}
void exmp(char *x, int m, char *y, int n, int ne[], int extend[])
{
    pre_exmp(x, m, ne);
    int j = 0;
    while(j < n && j < m && x[j] == y[j])j++;
    extend[0] = j;
    int k = 0;
    for(int i = 1; i < n; i++)
    {
        int p = extend[k] + k - 1;
        int L = ne[i - k];
        if(i + L < p + 1)extend[i] = L;
        else
        {
            j = max(0, p - i + 1);
            while(i + j < n && j < m && y[i + j] == x[j])j++;
            extend[i] = j;
            k = i;
        }
    }
}
char y[1001000];
int extend[1001000];
int ne[1001000];
char x[1001000];
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        cin >> y >> x;
        int n = strlen(y);
        int m = strlen(x);
        reverse(y, y + n);
        reverse(x, x + m);
        exmp(x, m, y, n, ne, extend);
        ll ans = 0;
        for(int i = 0; i < n; ++ i)
        {
            if(extend[i])
                ans = (ans + Add(extend[i]) % mod) % mod;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值