hdu 6153 A Secret KMP&&扩展KMP

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

题解:

KMP做法。
反转俩个字符串,用kmp找T在S中出现的次数,每匹配一位成功,就代表那一位后缀出现加一。思路就这样,稍稍改改kmp的板子就行。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1e6+100;
const int mod = 1e9+7;
typedef long long LL;
int nt[maxn];
char s[maxn],t[maxn];
int tlen,slen;
LL cnt[maxn];

void getNext()
{
    int j,k;
    j=0,k=-1,nt[0]=-1;
    while(j<tlen)
    {
        if(k==-1||t[j]==t[k])
        {
            nt[++j]=++k;
        }
        else
        {
            k=nt[k];
        }
    }
}

void KMP_count()
{
   getNext();
   int i,j=0;
   s[slen]='#';

   for(i=0;i<slen+1;i++)
   {
       while(j>0&&s[i]!=t[j])
       {
           cnt[j]++;
           j=nt[j];
       }
       if(s[i]==t[j])
       {
           ++j;
       }
       if(j==tlen)
       {
           cnt[j]++;
           j=nt[j];
       }
   }
    LL sum=0;
    for(long long i = tlen;i >= 0;i--) //按照题目要求求和
    {
        cnt[i] += cnt[i+1];

    }
    for(long long i = tlen;i >= 0;i--)
    {
        sum+=(i*cnt[i])%mod;
    }
    cout<<sum%mod<<endl;

}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(cnt,0,sizeof(cnt));

        scanf("%s",s);
        scanf("%s",t);
        slen=strlen(s);
        tlen=strlen(t);

        strrev(s);
        strrev(t);

        getNext();
        KMP_count();

    }
    return 0;
}

扩展KMP题解:

把两个串反转一下,求后缀就变成了求前缀。
extend数组是位置i与模式串的最长公共前缀。
当一个较长前缀出现过一次,代表较短的前缀也出现一次。
如extend[i]=3 其实代表1,2,3长度的前缀串都出现一次,我们都相加即可。就是等差数列求和公式。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1e6+100;
const int mod = 1e9+7;

char s1[maxn];
char s2[maxn];
typedef long long LL;
int nt[maxn];
int extend[maxn];

void preEKMP(char x[], int m, int next[])
{
    next[0] = m;
    int j = 0;
    while (j + 1 < m && x[j] == x[j + 1])
    {
        j++;
    }
    next[1] = j;
    int k = 1;
    for (int i = 2; i < m; i++)
    {
        int p = next[k] + k - 1;
        int L = next[i - k];
        if (i + L < p + 1)
        {
            next[i] = L;
        }
        else
        {
            j = std::max(0, p - i + 1);
            while (i + j < m && x[i + j] == x[j])
            {
                j++;
            }
            next[i] = j;
            k = i;
        }
    }
    return ;
}

void EKMP(char x[], int m, char y[], int n, int next[], int extend[])
{
    preEKMP(x, m, next);
    int j = 0;
    while (j < n && j < m && x[j] == y[j])
    {
        j++;
    }
    extend[0] = j;
    int k = 0;
    for (int i = 1; i < n; i++)
    {
        int p = extend[k] + k - 1;
        int L = next[i - k];
        if (i + L < p + 1)
        {
            extend[i] = L;
        }
        else
        {
            j = std::max(0, p - i + 1);
            while (i + j < n && j < m && y[i + j] == x[j])
            {
                j++;
            }
            extend[i] = j;
            k = i;
        }
    }
    return ;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%s",s1);
        scanf("%s",s2);
        int s1len = strlen(s1);
        int s2len = strlen(s2);
        strrev(s1);
        strrev(s2);

        EKMP(s2,s2len,s1,s1len,nt,extend);

        LL sum=0;
        for(int i=0;i<s1len;i++)
        {
            if(extend[i])
             sum+=((LL)extend[i]*(extend[i]+1)/2)%mod;
        }
        cout<<sum%mod<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值