题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少。

此题要用KMP的next和DP来做。

next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置。

下标从0开始。

设j=next[i],那么

如果j==0,回溯到起点说明该字符不匹配。

其他情况,说明字符串S[0,...j-1]与字符串S[0,..i-1]的某个后缀(准确的说是S[i-j,i-1])相同,这样的话,S[0,..i-1]的后缀(S[i-j,i-1])一定包含字符串S[0,..i-1]的后缀能够匹配的前缀个数,除此之外,S[0,..i-1]的后缀还应该包括自身这种情况。

设dp[i]表示字符串S[0,..i-1]的后缀可以匹配的前缀个数。

这样dp[i]=dp[next[i]]+1

答案ans=sum{dp[i]}(1<=i<=n)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define MOD  10007
#define MAXN 200005
using namespace std;
char str[MAXN];
int next[MAXN];
int dp[MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        scanf("%s",str);
        ;i<n;++i)
        {
            int j=next[i];
            while(j&&str[i]!=str[j]) j=next[j];
            next[i+]=(str[i]==str[j])?j+:;
        }
        ;
        ;i<=n;++i)
        {
            dp[i]=(dp[next[i]]+)%MOD;
            ans=(ans+dp[i])%MOD;
        }
        printf("%d\n",ans);
    }
    ;
}

HDU 3336 - Count the string(KMP+递推)的更多相关文章

  1. hdu 3336 Count the string KMP+DP优化

    Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...

  2. hdu 3336 Count the string -KMP&dp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  3. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  4. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...

  5. HDU 3336 Count the string ( KMP next函数的应用 + DP )

    dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...

  6. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu 3336 Count the string(next数组)

    题意:统计前缀在串中出现的次数 思路:next数组,递推 #include<iostream> #include<stdio.h> #include<string.h&g ...

  10. HDU 3336 Count the string

    题解:利用next数组来保存前缀位置,递推求解. #include <cstdio> #include <cstring> char pat[200005]; int next ...

随机推荐

  1. License Manager 10.3启动失败解决方法

    参考:http://jingyan.baidu.com/article/c275f6bac8763ae33d756788.html 安装ARCGIS10.3过程中无法启动license. 解决办法: ...

  2. 默认选择radio的第一个

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. fork&exec

    进程是系统进行资源分配和调度的基本单位,包括代码.数据和PCB进程控制块等资源. fork函数通过系统调用创建一个与原进程相同的子进程. 在调用进程(父进程)中返回一次,返回子进程ID:在子进程返回0 ...

  4. input覆盖select实现select可写可选择

    1.有时需要一个select选择框,但是对select选择框希望是既可以选择也可以进行输入.下边的代码就是在select选择框上放了一个input框,覆盖在select上面.实现伪装的select效果 ...

  5. mysql str_to_date字符串转换为日期

    mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期. 示例:分隔符一致,年月日要一致 select str_to_date('2008-4-2 15:3:28','% ...

  6. [转]JavaSE 8—新的时间和日期API

    为什么我们需要一个新的时间日期API Java开发中一直存在一个问题,JDK提供的时间日期API一直对开发者没有提供良好的支持. 比如,已有的的类(如java.util.Date和SimpleDate ...

  7. mke2fs/mks.etc3/fstab/mount指令

    一.mke2fs指令mkfs.etc3 /dev/sdb1指令 主要新学习 cat /etc/filesystem  //查看文件类型 mkfs. tab键有提示    //按照系统默认的值格式化 m ...

  8. Swift - 自动布局库SnapKit的使用详解2(约束的更新、移除、重做)

    在之前的文章中我介绍了如何使用SnapKit的 snp_makeConstraints 方法进行各种约束的设置.但有时我们的页面并不是一直固定不变的,这就需要修改已经存在的约束.本文介绍如何更新.移除 ...

  9. bzoj 2730: [HNOI2012]矿场搭建

    #include<cstdio> #include<cstring> #include<iostream> #define M 508 using namespac ...

  10. JavaScript自定义右键菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...