HDU-6153---A Secret (扩展kmp)(2017ccpc网络赛)

A Secret

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



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求出extand数组,这里需要好好理解extand数组表示的是什么,然后对于每个extend求(1~extand)的和就行;

举个例子:第二组样例翻转后为

str1:babababa

str2:aba

extand:03030301;

extand[1]=3;表示从str1[1]开始能与str2的前三个字符匹配,所以在这个点a,ab,aba都出现了一次,次数乘以长度的和便为1+2+3,讲到这应该是很好懂了;

AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define mod 1000000007
using namespace std;
const int K=1000005;
int  nt[K],extand[K];
char S[K],T[K];
//***********模板**********
void Getnext(char *T,int *next)
{
    int len=strlen(T),a=0;
    next[0]=len;
    while(a<len-1 && T[a]==T[a+1]) a++;
    next[1]=a;
    a=1;
    for(int k=2; k<len; k++)
    {
        int p=a+next[a]-1,L=next[k-a];
        if( (k-1)+L >= p)
        {
            int j = (p-k+1)>0 ? (p-k+1) : 0;
            while(k+j<len && T[k+j]==T[j]) j++;
            next[k]=j;
            a=k;
        }
        else
            next[k]=L;
    }
}

void GetExtand(char *S,char *T,int *next)
{
    Getnext(T,next);
    int slen=strlen(S),tlen=strlen(T),a=0;
    int MinLen = slen < tlen ? slen : tlen;
    while(a<MinLen && S[a]==T[a]) a++;
    extand[0]=a;
    a=0;
    for(int k=1; k<slen; k++)
    {
        int p=a+extand[a]-1, L=next[k-a];
        if( (k-1)+L >= p)
        {
            int j= (p-k+1) > 0 ? (p-k+1) : 0;
            while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
            extand[k]=j;
            a=k;
        }
        else
            extand[k]=L;
    }
}
//*************
long long add(long long int x)
{
    long long ans;
    ans=((((1+x)%mod)*(x%mod))/2)%mod;//开始这里少加了一个括号wa了一发
    return ans;
}
int main(void)
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",S,T);
        int len1=strlen(S);
        int len2=strlen(T);
        reverse(S,S+len1);
        reverse(T,T+len2);
        GetExtand(S,T,nt);
        long long  ans=0;
        for(int i=0; i<len1; i++)
        {
            if(extand[i])
                ans=(ans+add(extand[i])%mod)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值