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

此题要用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. Unity3D研究院编辑器之不实例化Prefab获取删除更新组件(十五)

    http://www.xuanyusong.com/archives/3727 感谢楼下的牛逼回复更正一下,我表示我也是才知道.. 其实不需要实例化也能查找,你依然直接用GetComponentsIn ...

  2. WebViewJavascriptBridge-Obj-C和JavaScript互通消息的桥梁

    转载至:http://www.cocoachina.com/ios/20150629/12248.html 译者:@coderyi9 本文翻译自Marcus Westin的开源框架WebViewJav ...

  3. C# Form内存回收

    namespace WebBrowserMemoryTest { public partial class Form1 : Form { private int _Pages; public Form ...

  4. HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)

    Destroying the bus stations                                                                          ...

  5. 【待整理】PS切图基础教程

    http://www.w3cfuns.com/article-442-1-1.html http://www.w3cfuns.com/article-443-1-1.html 其他专题研究: floa ...

  6. tableviewcell边距和设置值不符

    解决方法: 将Table View Cell的Layout Margins由Default改为Explicit,然后修改上下左右的边距.

  7. BZOJ1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

    先预处理出来每个点对之间的最短距离 然后二分答案,网络流判断是否可行就好了恩 /************************************************************ ...

  8. async 和 await 以及Action Func

    C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. 看个例子: public class MyClass { public MyClass() { Displa ...

  9. IKAnalyzer 和 solr4.3 冲突

    solr4.3 运行之后发现异常:Exception in thread "main" java.lang.VerifyError: class org.wltea.analyze ...

  10. Java中的String与常量池[转帖]

    string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...