A Secret(HDU 6153 扩展KMP)

A Secret

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



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.
 

//题意:给定母串str1,匹配串str2,str2[i-n]在str1中出现的次数为num[i],str2[i-n]的长度为len[i],求∑ (num[i]*len[i])  (0<=i<=n,n为strlen(str2)-1) 。

//思路:显然,如果str1里的某一段和str2里的某一段能匹配的话,将str1和str2逆序后,这两段还是能够匹配的。所以,将str1和str2同时逆序,那么现在就是str2的所有前缀去str1里匹配。还有1个点就是,如果str1里有个子串为“abc”,str2里也有一个前缀为“abc”,那么这个前缀能产生的价值就是(1+2+3)=(3+1)*3/2,因为“abc”能去匹配“abc”,同时“bc”能去匹配“bc”,“c”能去匹配“c”,所以,假设这个前缀能匹配的长度为m的话,产生的价值就是m*(m+1)/2 。而前缀能匹配的长度可以通过扩展KMP来求。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

const int MAX=1e6+10;
const int mod=1e9+7;

char str1[MAX],str2[MAX];
int NEXT[MAX],EXTEND[MAX];

void GetNext(char *s, int *Next)
{
    int i = 0, j, po, len = strlen(s);
    Next[0] = len;
    while (s[i] == s[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&&s[j] == s[i + j])
                j++;
            Next[i] = j;
            po = i;
        }
    }
}

//扩展KMP模板,s1是母串,s2是匹配串
void ExKmp(char *s1, char *s2, int *Next, int *extend)
{
    int i = 0, j, po, len1 = strlen(s1), len2 = strlen(s2);
    GetNext(s2,Next);
    while (s1[i] == s2[i] && i < len2&&i < len1)
        i++;
    extend[0] = i;
    po = 0;
    for (i = 1; i < len1; i++)
    {
        if (Next[i - po] + i < extend[po] + po)
            extend[i] = Next[i - po];
        else
        {
            j = extend[po] + po - i;
            if (j < 0)
                j = 0;
            while (i + j < len1&&j < len2&&s1[i + j] == s2[j])
                j++;
            extend[i] = j;
            po = i;
        }
    }
}

int main()
{
        int T;
        scanf("%d",&T);
        while(T--)
        {
                scanf("%s",str1);
                scanf("%s",str2);
                int len1=strlen(str1);
                int len2=strlen(str2);
                memset(NEXT,0,sizeof(NEXT));
                memset(EXTEND,0,sizeof(EXTEND));
                //逆序一开始自己写TLE了(首尾互换),用reverse()函数就可以...
                reverse(str1,str1+len1);
                reverse(str2,str2+len2);
                ExKmp(str1,str2,NEXT,EXTEND);
                long long sum=0;
                for(int i=0;i<len1;i++)
                {
                    if(EXTEND[i]>0)
                        sum=(sum+(1LL*EXTEND[i]*(EXTEND[i]+1)/2)%mod)%mod;
                }
                printf("%lld\n",sum%mod);
        }
        return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值