HDU 6153 A secret(kmp)

似乎正解是扩展kmp的模版题.
但是kmp也可以做.
kmp有两种.一种是在每一次成功匹配后都算一次.
这种是正解呢.
因为kmp,其实就是遍历了所有会有重叠的情况,但是一些毫无可能的就被忽略了,但是我们只记录成功匹配一个之后的情况的话,会有在while中的匹配被忘记.所以没一个成功匹配到之后,都有他的next被忽略了.
一种是在结束匹配是算一次,这种要等差数列计算.
反正这题很迷幻.
靠我总结不下去了,还是不懂啊.
就当存个kmp,我好菜啊

/* Farewell. */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <set>
using namespace std;
#define gcd __gcd
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define MT make_tuple
#define PB push_back
typedef long long  LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
typedef pair<LL,LL> pll;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
const int INF = 0x7fffffff;
const LL INFF = 0x7f7f7f7fffffffff;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
const int MAXM = 5e3+17;
const int MOD = 1e9+7;
const int MAXN = 1e6+17;
char s[MAXN],p[MAXN];
string tmp;
LL Next[MAXN];
LL arv[MAXN];
void getNext(string p)
{
    memset(Next, 0, sizeof(Next));
    int m = p.length();
    int i = 0,j = -1;
    Next[0] = -1;
    while (i < m)
    {
        if (j == -1 || p[i] == p[j])
        {
            i++;j++;
            Next[i] = j;
        }else j = Next[j];
    }
}
void kmp(string s,string p)
{
    memset(arv, 0, sizeof(arv));
    int i = 0, j = 0,n = s.length(),m = p.length();
    getNext(p);
    for(int i =0 ;i<n;++i)
    {
        while(j > 0 && s[i] != p[j])
        {
            arv[j]++;
            j = Next[j];
        }
        if(s[i] == p[j])
            j++;
        if(j == m)
        {
            arv[j]++;
            j = Next[j];
        }
    }
}
int main(int argc, char const *argv[])
{    
        #ifdef GoodbyeMonkeyKing
    freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
    #endif
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%s%s",s,p);
        string a(s,&s[strlen(s)]);
        string b(p,&p[strlen(p)]);
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        kmp(a,b);
        LL ans = 0;
        for (LL i = 0; i <= b.length(); ++i)
            ans = (ans+(1LL*arv[i]*(1LL*(i+1)*i)/2)%MOD)%MOD;
        cout<<ans%MOD<<endl;
    }
    return 0;    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值