hdu 6153 A secret(拓展KMP)

题目链接: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.

这个题目如果直接想要直接做的话,求s2的extend数组在一个一个遍历s1会超时,所以不妨换个思路

首先将s1,s2都反过来,这样只需要求一次s1的extend数组,ex[i]=k表示t1,t1t2,...t1t2...tk均在S中出现了一次,那么总的出现次数就是k*(k+1)/2,于是我们遍历一遍extend数组即可

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define scn(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=1e6+10;
const ll mod=1e9+7;
int nexte[N];             
int ex[N];              
char s1[N],s2[N];
void getnexte(char *str)        
{
    int len=strlen(str);
    int po=1,i=0;             
    nexte[0]=len;
    while(str[i]==str[i+1]&&i+1<len)             
           i++;
    nexte[1]=i;
    for(int i=2;i<len;i++){         
        int p=nexte[po]+po-1;
        int l=nexte[i-po];
        if(nexte[i-po]+i<nexte[po]+po)           
                  k+len<p
            nexte[i]=nexte[i-po];                  
        else {                
            int j=nexte[po]+po-i;
            if(j<0)                //next[po]+po<i
                j=0;
            while(str[j]==str[j+i]&&j+i<len)      //计算next[i]
                j++;
            nexte[i]=j;
            po=i;
        }
    }
}
void getex(char *s1,char *s2)
{
    int i=0;
    int l1=strlen(s1),l2=strlen(s2);
    getnexte(s2);
    while(s1[i]==s2[i]&&i<l1&&i<l2)
        i++;
    ex[0]=i;
    int po=0;
    for(int i=1;i<l1;i++){
        if(nexte[i-po]+i<ex[po]+po)
            ex[i]=nexte[i-po];
        else {
            int j=ex[po]+po-i;
            if(j<0)
                j=0;
            while(i+j<l1&&s1[j+i]==s2[j])
                j++;
            ex[i]=j;
            po=i;
        }
    }
}
ll count(ll x)
{
    return ((x%mod)*((x+1)%mod)/2);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
            scanf("%s%s",&s1,&s2);
        int l1=strlen(s1);
        int l2=strlen(s2);
        reverse(s1,s1+l1);
        reverse(s2,s2+l2);
        ll ans=0;
        getex(s1,s2);
       for(int i=0;i<l1;i++){
        if(ex[i])
            ans=(ans+count(ex[i])%mod)%mod;
       }
        printf("%lld\n",ans);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值