2017中国大学生程序设计竞赛 - 网络选拔赛 1004 A Secret hdoj 6153

A Secret

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 3407    Accepted Submission(s): 1228


 

Problem 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.

 

 

Source

2017中国大学生程序设计竞赛 - 网络选拔赛

 

 

Recommend

liuyiding

 

简单的说,就是给你2个字符串,a,b,让你求  b满足Suffix(S2,i)规律的子串  在 a中出现的次数子串本身长度 的乘积,然后累加。

爆搜显然是不行,最开始用了KMP,但是由于不是特别熟悉而且搞了n^2的复杂度,姿势不太好,无限TLE。后来用了扩展KMP,但是由于不知道数组开小了,TLE了2小时,年老眼花了。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define mod 1000000007
const int maxn = 1000010;
long long Next[maxn], ex[maxn];
char a[maxn], b[maxn];


void GETNext(char *str)
{
    int i = 0, j, po, len = strlen(str);
    Next[0] = len;
    while (str[i] == str[i + 1] && i + 1<len) i++;
    Next[1] = i;
    po = 1;
    for (i = 2; i<len; i++)
    {
        if (Next[i - po] + i<Next[po] + po)
            Next[i] = Next[i - po];
        else
        {
            j = Next[po] + po - i;
            if (j<0)j = 0;
            while (i + j<len&&str[j] == str[j + i])
                j++;
            Next[i] = j;
            po = i;
        }
    }
}


void EXKMP(char *s1, char *s2)
{
    int i = 0, j, po, len = strlen(s1), l2 = strlen(s2);
    GETNext(s2);
    while (s1[i] == s2[i] && i<l2&&i<len)
        i++;
    ex[0] = i;
    po = 0;
    for (i = 1; i<len; i++)
    {
        if (Next[i - po] + i<ex[po] + po)
            ex[i] = Next[i - po];
        else
        {
            j = ex[po] + po - i;
            if (j<0)j = 0;
            while (i + j<len && j<l2 &&s1[j + i] == s2[j])
                j++;
            ex[i] = j;
            po = i;
        }
    }
}
char *revstr(char *str, size_t len)
{
    char *start = str;
    char *end = str + len - 1;
    char ch;
    if (str != NULL)
    {
        while (start < end)
        {
            ch = *start;
            *start++ = *end;
            *end-- = ch;
        }
    }
    return str;
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        for (int i = 0; i < maxn; i++)
            a[i] = b[i] = Next[i] = ex[i] = 0;
        scanf("%s %s", a, b);
        int len2 = strlen(b);
        int len = strlen(a);
        revstr(a, len); //因为扩展KMP求得是最大前缀长度,而我们要用的是后缀,所以翻转
        revstr(b, len2);
        EXKMP(a, b);
        long long ans = 0;
        for (int i = 0; i < len; i++)
        {
            ans = (ex[i] * (ex[i] + 1) / 2 % mod + ans) % mod;
        }
        printf("%lld\n", ans);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值