【KMP+DP】Count the string
KMP算法的综合练习
DP很久没写搞了半天才明白。本题结合Next[]的意义以及动态规划考察对KMP算法的掌握。
Problem Description
Input
Output
Sample Input
1
4
abab
Sample Output
6
Author
Source
#include<iostream> //KMP+DP
#include<memory.h>
using namespace std;
char s[];
int Next[],DP[]; //DP[i]表示子串s[0~i]共含有以s[i]为结尾的前缀的数目
int l; void GetNext(){
int i=,j=-;
Next[]=-;
while(i<l){
if(j==-||s[i]==s[j]){
i++;
j++;
Next[i]=j;
}
else
j=Next[j];
}
} int main()
{
int n,k,num;
cin>>n;
while(n--){
cin>>l>>s;
GetNext();
num=;
memset(DP,,sizeof(DP));
for(k=;k<=l;k++){
DP[k]=DP[Next[k]]+; //s[i]结尾的前缀数就是自己本身加上以s[Next[i]]结尾的前缀数
num=(num+DP[k])%;
}
cout<<num<<endl;
}
return ;
}
【KMP+DP】Count the string的更多相关文章
- 【HDU 3336】Count the string(KMP+DP)
Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...
- Codeforces Beta Round #71 C【KMP+DP】
Codeforces79C 题意: 求s串的最大子串不包含任意b串: 思路: dp[i]为以i为起点的子串的最长延长距离. 我们可以想到一种情况就是这个 i 是某个子串的起点,子串的长度-1就是最短, ...
- P5404-[CTS2019]重复【KMP,dp】
正题 题目链接:https://www.luogu.com.cn/problem/P5404 题目大意 给出一个字符串\(S\),然后求有多少个长度为\(m\)的串\(T\)满足.无限多个串\(T\) ...
- 洛谷P4591 [TJOI2018]碱基序列 【KMP + dp】
题目链接 洛谷P4591 题解 设\(f[i][j]\)表示前\(i\)个串匹配到位置\(j\)的方案数,匹配一下第\(i\)个串进行转移即可 本来写了\(hash\),发现没过,又写了一个\(KMP ...
- 【期望DP】
[总览] [期望dp] 求解达到某一目标的期望花费:因为最终的花费无从知晓(不可能从$\infty$推起),所以期望dp需要倒序求解. 设$f[i][j]$表示在$(i, j)$这个状态实现目标的期望 ...
- Kattis - bank 【简单DP】
Kattis - bank [简单DP] Description Oliver is a manager of a bank near KTH and wants to close soon. The ...
- HDOJ 1501 Zipper 【简单DP】
HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...
- Vijos 1565 多边形 【区间DP】
描述 zgx给了你一个n边的多边形,这个多边形每个顶点赋予一个值,每条边都被标上运算符号+或*,对于这个多边形有一个游戏,游戏的步骤如下:(1)第一步,删掉一条边:(2)接下来n-1步,每步对剩下的边 ...
- Vijos 1451 圆环取数 【区间DP】
背景 小K攒足了路费来到了教主所在的宫殿门前,但是当小K要进去的时候,却发现了要与教主守护者进行一个特殊的游戏,只有取到了最大值才能进去Orz教主…… 描述 守护者拿出被划分为n个格子的一个圆环,每个 ...
随机推荐
- tail和head命令
[root@rhel7 ~]# cat rusky --cat命令查看文件内容 line1 line2 line3 line4 line5 line6 line7 line8 line9 line10 ...
- hdu 2201
题意: 一共有n个人,m表示第m个人,然后问你第i个人不做到m号位置的概率,最后相乘.... 水题(注意下格式输出) AC代码: #include <iostream> using nam ...
- IE8兼容placeholder的方案
用JavaScript解决Placeholder的IE8兼容问题 placeholder属性是HTML5新添加的属性,当input或者textarea设置了该属性后,该值的内容将作为灰色提示显示在文本 ...
- 如何缩减Try{}Catch{}Finally{}代码----定义一个公用的Try{}Catch{}Finally{}
public class Process { public Process() { } public static void Execute(Action action) { try { //ACTI ...
- EF中使用Contains方法
第一种情况 var db=new ECEntities(); var list=new []{"8","9"}; var result=from a in db ...
- c - 字符串的拼接.
完整代码: #include <stdio.h> #include <string.h> #include <malloc.h> #define TRUE 1 #d ...
- SQL2008 存储过程参数相关
使用inputparame时,使用的是 varchar(20),和数据库中的DEPARTNAME完全匹配,可以查出值: USE [test] GO SET ANSI_NULLS OFF GO SE ...
- 执行start-dfs.sh后,datenode没有启动
Hadoop2.2.0启动异常 – Incompatible clusterIDs 2014年08月29日 ⁄ 综合 ⁄ 共 2399字 ⁄ 字号 小 中 大 ⁄ 评论关闭 今天启动Hadoop2.2 ...
- DELL R410升级网卡驱动
官方链接http://zh-cn.broadcom.com/support/ethernet_nic/netxtremeii.php(官方驱动的名字偶尔会改) 注意确保服务器的kernel-dev ...
- 经典shell面试题整理
一.取出/etc/passwd文件中shell出现的次数 问题:下面是一个/etc/passwd文件的部分内容.题目要求取出shell并统计次数,shell是指后面的/bin/bash,/sbin/n ...