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

此题要用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. React添加事件

    定义个组件 组件首字母大写,调用: ReactDOM.render(<Hello></Hello>,document.getElementById('box'));

  2. echarts饼图--数据交互

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  4. ExpandableListView getChildView 不执行,不显示子列表

    原因很简单: 在 GroupView 里面不要加入 button 等可点击空间,否则 和 点击 Groupview 展开相冲突. 去掉就好了getGroupView

  5. svg矢量图绘制以及转换为Android可用的VectorDrawable资源

    项目需要 要在快速设置面板里显示一个VoWiFi图标(为了能够区分出来图形,我把透明的背景填充为黑色了) 由于普通图片放大后容易失真,这里我们最好用矢量图(SVG(Scalable Vector Gr ...

  6. CSDN中的Bug

    简述 在用CSDN的过程中,发现了许许多多的Bug.之前没有做记录,直接反馈给客服了,有时没图说得不太清楚,现在不都追求有图有真相么O(∩_∩)O~,所以在此记录一下. 作为程序员的一份子,Bug对我 ...

  7. C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)

    要实现下面程序,首先我们需要三个文件 detours.h ,detours.lib ,detver.h(可以去网上下载) 1. 首先让我们看看,一个最简单的C程序,如何劫持system函数. #inc ...

  8. #ifdef __cplusplus extern "C"

    #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #endif首先,__cplusplus是cp ...

  9. 数据库索引<一> 索引结构表结构

    有很长时间没有更新博客了,再过几天都2月分了,如果再不更新一篇,我1月分都没有更新,保持连续,今天更新一篇. 最近没有什么看技术方面的东西,游戏,画画搞这些去了.我发现我每年一到年底就是搞这些东西,其 ...

  10. Java多线程-新特性-线程池

    Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定可靠的多线程程序 ...