A Secret(扩展KMP)

A Secret

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

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

/*
    问题描述 : 给一个主串 和 一个 模式串 问 模式串中后缀在 主串中出现的次数
    乘以 这个后缀的长度
    http://acm.hdu.edu.cn/showproblem.php?pid=6153
    参考 博客 : https://segmentfault.com/a/1190000008663857
    (这个人讲解的特别到位)

*/

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdlib.h>
#define MAXN 1000007

using namespace std;
const int MOD = 1e9+7;
char T[MAXN];//主串
char S[MAXN];//模式串
long long Next[MAXN];//next[i] 记录的是 T[i..m-1] 与 T 串最长相同前缀的长度
long long extend[MAXN];//extend[i] 记录的是 从 S[i] 开始 S 与 T的匹配最长长度

void GetNext(int len)//求next值 next 的值就是 T 与 自己构造一个扩展数组
{
    Next[0] = len;//初始化Next[0] 因为 T[0..m-1]与 T[0..m-1] 最长的公共前缀就是它本身
    int a,p;
    //p 就是一个 匹配的最大点的下表
    //a 就是一个 从a + Next[a] = p
    for(int i =1,j = -1;i<len;i++,j--)//j 的含义就是 p 点与 i 的距离
    {
        if(j < 0 || i + Next[i - a] >= p) //i 点 的 next 数组的值 肯定是 已经超过点a 的 最远距离了
        {
            if(j<0)//i > p
            {
                p = i,j = 0;//更新最远距离的点
            }//S 从 i 开始 T 从 0 开始从头匹配
            while(p < len && T[p] == T[j]) p++,j++;
            Next[i] = j;
            a = i;//更新 a 点 和 p 的值
        }
        else
        {
            Next[i] = Next[i-a];//表示 还在 最远访问的距离p 内 呢说明 我这个点的 next的值已经知道了就是next[i-a]的值
        }
    }
}

void GetExtend(int lens ,int lent)
{
    GetNext(lent);
    int a,p;//p 记录匹配成功的 字符 的最远位置 p 及其其实地址a
    for(int i = 0,j = -1 ;i<lens;i++,j--)
    {
        if(j <0 || i + Next[i-a] >= p)
        {
            if(j <0)
            {
                p = i;
                j =0;
            }
            while(p<lens && j < lent && S[p] == T[j]) p++,j++;
            extend[i] = j;
            a = i;
        }
        else
            extend[i] = Next[i-a];
    }
}
int main()
{
    int ncase;
    while(scanf("%d",&ncase) != EOF)
    {
        while(ncase --)
        {
            scanf("%s%s",S,T);
            int lens = strlen(S);
            int lent = strlen(T);
            /*
                这里翻转的目的是 为了 将问题转化为 扩展KMP 问题
                扩展 kmp 解决的是 S[i] 与 T 的最长公共前缀的 长度
                题目是让求 T 的后缀 在 S 中出现的次数 呢不就是 将两个串 翻转 之后求
                S[i] 与  T 的 最长公共前缀的长度
            */
            reverse(S,S + lens);
            reverse(T, T + lent);
            GetExtend(lens,lent);
            long long ans = 0;
            for(int i =0;i<lens;i++)
            {
                /*
                    这里为什么要  (1+extend[i])*extend[i]/2求
                    这就需要理解好 extend 数组的含义
                    extend[i] 代表的是 S[i] 与 T 匹配的长度
                    举个例子:
                    假设 : extend [2] = 3 那么就是 从 S[2] 开始 到 S[5] 结束这个
                    子串 与 T[0] ~ T[2] 完全匹配成功
                    那么 是不是 就是 T[0] ~ T[2] 中 所有的 字串都在 S[2] ~ S[5] 中出现了 一次
                    所以就是 1*1 + 2*1 + 3*1 所以 才是(1+extend[i])*extend[i]/2
                */
                ans=(ans+(1+extend[i])*extend[i]/2)%MOD;
            }
           printf("%lld\n",ans);
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值