HDU 6153 (kmp拓展)

 LinK: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.


题意:

给定两个串,求其中一个串 sp 的每个后缀在另一个串 tt 中出现的次数

思路:

刚开始想到自动机但数组开到52会爆内存。正确解法是先把两个串reverse一下,得到p‘,t'就变成求p'的每个前缀,再t'中出现的次数.其实就是KMP拓展的问题。


#include <cstdio>  
#include <cstring>  
#define size 1000000  
#define ll   long long 
char str[size+5],mode[size+5];  
int next[size+5],extend[size+5];  
int mod = 1e9 + 7;  
ll add(ll n)  
{  
    ll m=((n%mod)*((n+1)%mod)/2)%mod;  
    return m;  
}  
void reverse(char s[])  
{  
    char c;  
    for(int i =0,j=strlen(s)-1;i<j; ++i,--j)  
    {  
        c=s[i];  
        s[i]=s[j];  
        s[j]=c;  
    }  
}  
void getNext(char *mode,int* next,int strlen)  
{  
    int i,a,p;  
    a=p=0;next[0]=strlen;  
    for(i=1;i<strlen;i++)  
    {  
        if(i>=p || i+next[i-a]>=p)  
        {  
            if(i>=p) p=i;  
            while(p<strlen && mode[p]==mode[p-i])  
                ++p;  
            next[i]=p-i,a=i;  
        }  
        else next[i]=next[i-a];  
    }  
}  
void getExtend(char* str,int strlen,int* extend,char* mode,int modeLen)  
{  
    getNext(mode,next,modeLen);  
    int a=0,p=0,sum=0;  
    for(int i=0;i<strlen;i++)  
    {  
        if(i>=p || i+next[i-a]>=p)  
        {  
            if(i>=p)  
                p=i;  
            while(p<strlen&&p-i<modeLen && str[p]==mode[p-i])  
                p++;  
            extend[i]=p-i,a=i;  
        }  
        else   
            extend[i]=next[i-a];  
    }  
}  
int main()  
{  
    int n;  
    scanf("%d",&n);  
    while(n--)  
    {  
        scanf("%s%s",str,mode);  
        reverse(str);  
        reverse(mode);  
        int strLen=strlen(str),modeLen=strlen(mode);  
        getExtend(str,strLen,extend,mode,modeLen);  
       	ll ans =0; 
        for(int i=0;i<strLen;i++)  
        {  
            if(extend[i])  
            ans=(ans+add(extend[i]))%mod;  
        }  
        printf("%lld\n",ans);  
    }  
    return 0;  
}  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值