HDU 6153-A Secret(kmp&&ccpc)

本文介绍了一种基于字符串匹配的算法,该算法通过计算两个字符串之间的特定匹配来得出一个秘密数值。具体而言,该算法首先定义了后缀、匹配次数及长度等概念,并通过实例解释了如何计算最终的秘密数值。此外,还提供了一个使用KMP算法扩展实现的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

kmp 拓展 , 网上找的模板, 后面处理就ok

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
int cnt[1000005];
inline ll Add(ll n)
{
    ll m=((n%mod)*((n+1)%mod)/2)%mod;
    return m;
}
char t[1000005],p[1000005];
int Next[1000005],ex[1000005];
void pre(char p[])
{
    int m=strlen(p);
    Next[0]=m;
    int j=0,k=1;
    while(j+1<m&&p[j]==p[j+1]) j++;
    Next[1]=j;
    for(int i=2; i<m; i++)
    {
        int P=Next[k]+k-1;
        int L=Next[i-k];
        if(i+L<P+1) Next[i]=L;
        else
        {
            j=max(0,P-i+1);
            while(i+j<m&&p[i+j]==p[j]) j++;
            Next[i]=j;
            k=i;
        }
    }
}
void exkmp(char p[],char t[])
{
    int m=strlen(p),n=strlen(t);
    pre(p);
    int j=0,k=0;
    while(j<n&&j<m&&p[j]==t[j]) j++;
    ex[0]=j;
    for(int i=1;i<n;i++)
    {
        int P=ex[k]+k-1;
        int L=Next[i-k];
        if(i+L<P+1) ex[i]=L;
        else
        {
            j=max(0,P-i+1);
            while(i+j<n&&j<m&&t[i+j]==p[j]) j++;
            ex[i]=j;
            k=i;
        }
    }
}
int main()
{
    int ss;
    scanf("%d",&ss);
    while(ss--)
    {
        scanf("%s %s",&t,&p);
        int Ti=strlen(t);
        int Pi=strlen(p);
        reverse(t,t+Ti);
        reverse(p,p+Pi);
        pre(p);
        memset(Next, 0,sizeof(Next));
        memset(ex, 0, sizeof(ex));
        exkmp(p,t);
        ll ans=0;
        for(int i=0; i<Ti;++i)
        {
            if(ex[i])
                ans=(ans+Add(ex[i])%mod)%mod;
        }
       printf("%d\n",ans%mod);
    }
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值