zoj3587(kmp+dp)

Marlon's String

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Long long ago, there was a coder named Marlon. One day he picked two string on the street. A problem suddenly crash his brain...

Let Si..j denote the i-th character to the j-th character of string S.

Given two strings S and T. Return the amount of tetrad (a,b,c,d) which satisfy Sa..b + Sc..d = T , ab and cd.

The operator + means concate the two strings into one.

Input

The first line of the data is an integer Tc. Following Tc test cases, each contains two line. The first line is S. The second line is T. The length of S and T are both in range [1,100000]. There are only letters in string S and T.

Output

For each test cases, output a line for the result.

Sample Input
1
aaabbb
ab
Sample Output

9


最开始看到这题就觉得是个dp,但是不知道从何做起,,还以为要用exkmp,(还不怎么会),后来发现也是用next数组性质就可以解决的题,,想复杂了。。。。假设前一个串是S串,后一个是T串,可以通过kmp求出T串第I个位置位置之前子串为前缀时在S串中出现过几次,同理反转以后也可以求出i+1位置之后的子串为后缀时出现在S串中的次数,相乘既是当前组合出现方案数,然后全加起来就行,而当前位置失败不匹配时有vis[next[i]]+=vis[i];所以dp搞一搞就好了。注意字符串很长,所以要用long long(在这里WA了一次T.T)

#include <iostream>
#include <stdio.h>
#include <string.h>
#define mem(a) memset((a),0,sizeof(a))
#define LL long long
using namespace std;
char s[100000+10];
char t[100000+10];
int ntr[100000+10];
LL vis1[100000+10];
LL vis2[100000+10];
void get_ntr(char *s)
{
    int i=0,j=-1;
    ntr[i]=-1;
    int len=strlen(s);
    while(i<=len)
    {
        if(j==-1||s[i]==s[j])
        {
            i++;
            j++;
            ntr[i]=j;
        }
        else
            j=ntr[j];
    }
}
void kmp1(char *s,char *t)
{
    int i=0,j=0;
    int lens=strlen(s);
    while(i<lens)
    {
        if(j==-1||s[i]==t[j])
        {
            if(j!=-1) vis1[j+1]++;
            i++;
            j++;
        }
        else j=ntr[j];
    }
}
void kmp2(char *s,char *t)
{
    int i=0,j=0;
    int lens=strlen(s);
    while(i<lens)
    {
        if(j==-1||s[i]==t[j])
        {
            if(j!=-1) vis2[j+1]++;
            i++;
            j++;
        }
        else j=ntr[j];
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        scanf("%s",t);
        int lens=strlen(s);
        int lent=strlen(t);
        mem(vis1);
        mem(vis2);
        get_ntr(t);
        kmp1(s,t);
        for(int i=lent; i>0; i--)
            if(ntr[i]!=-1) vis1[ntr[i]]+=vis1[i];
        for(int i=0; i<lens/2; i++)
        {
            char ch;
            ch=s[i];
            s[i]=s[lens-i-1];
            s[lens-i-1]=ch;
        }
        for(int i=0; i<lent/2; i++)
        {
            char ch;
            ch=t[i];
            t[i]=t[lent-i-1];
            t[lent-i-1]=ch;
        }
        mem(ntr);
        get_ntr(t);
        kmp2(s,t);
        for(int i=lent; i>0; i--)
            if(ntr[i]!=-1)vis2[ntr[i]]+=vis2[i];
        LL ans=0;
        for(int i=1; i<lent; i++)
        {
            ans+=vis1[i]*vis2[lent-i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值