HDU 6153 A Secret (KMP)

本文介绍了一种利用KMP算法解决特定字符串匹配问题的方法,旨在寻找两字符串间所有后缀及其长度的组合秘密,通过优化计数过程,实现高效求解。

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

http://acm.hdu.edu.cn/showproblem.php?pid=6153

题目:

 

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.

题意:给定两个字符串A, B,定义Suffix(B, n)为从B串的第n个字符到B串末尾的那个子串, 计算(所有Suffix * 各自的长度)的加和。

题解:KMP= =, 多加一个cnt数组,只记录每一次匹配过程中出现的最大匹配长度的次数, 比如一次匹配匹配失败是最大的匹配长度为len, 则cnt[len] += 1,这样一遍kmp跑完后就可以算结果了。有递推式:

for(i = strlen(B);i >= 0;i++)
    cnt[i] += cnt[i + 1];

这样递推完成后每个cnt[i]就代表长度为i的Suffix出现的次数, 直接计算结果就可以了。

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int ne[1000005];
char ma[1000005], mi[1000005];
char m1[1000005], m2[1000005];
long long ans = 0;
long long cnt[1000005] = {0};
void getnext(char *a)
{
    int len = strlen(a);
    int i = 0, j = -1;
    ne[0] = -1;
    while(i < len){
        if(j == -1 || a[i] == a[j])
            ne[++i] = ++j;
        else j = ne[j];
    }
}

void kmp(char *a, char *b)
{
    int lena = strlen(a);
    int lenb = strlen(b);
    int i, j = 0;
    for(i = 0;i < lena + 1;i++){
        while(j && a[i] != b[j]){
            cnt[j] += 1;
            j = ne[j];
        }
        if(a[i] == b[j])
            j++;
        if(j == lenb){
            cnt[j]++;
            j = ne[j];
        }
    }
}
int main()
{
    int i, j, T;
    scanf("%d", &T);
    while(T--){
        scanf("%s", ma);
        scanf("%s", mi);
        for(i = strlen(ma) - 1, j = 0;i >= 0;i--)
            m1[j++] = ma[i];
        for(i = strlen(mi) - 1, j = 0;i >= 0;i--)
            m2[j++] = mi[i];
        m2[strlen(m2)] = '.';
        getnext(m2);
        kmp(m1, m2);
        for(i = strlen(m2);i >= 0;i--){
            cnt[i] += cnt[i + 1];
            ans = (ans % 1000000007 + (cnt[i] % 1000000007 * i % 1000000007) % 1000000007) % 1000000007;
        }
        printf("%lld\n", ans);
        ans = 0;
        memset(ne, 0, sizeof(ne));
        memset(ma, 0, sizeof(ma));
        memset(mi, 0, sizeof(mi));
        memset(m1, 0, sizeof(m1));
        memset(m2, 0, sizeof(m2));
        memset(cnt, 0, sizeof(cnt));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值