luoguP2679 子串

个人感觉\(noip\)系列中挺好的一道DP题目.

题面有点难理解.

我们设\(f_{i,j,k,0/1}\)表示\(A\)串前\(i\)个字符,匹配\(B\)串前\(j\)个字符,正在用第\(k\)的子串,且第\(i\)个字符选或者不选的方案数

则有\(f_{i,j,k,0} = f_{i - 1,j,k,0} + f_{i - 1,j,k,1}\)

如果\(A_i == A_j\),那么有

\(f_{i,j,k,1} = f_{i - 1,j - 1,k - 1,1} + f_{i - 1,j - 1,k - 1,0}+f_{i - 1,j - 1,k,1}\)

否则

\(f_{i,j,k,1} = 0\)(因为无法匹配)

但是,空间复杂度不够优秀。

将第一维滚掉就好了

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e3 + 3;
const int M = 2e2 + 2;
const int mod = 1e9 + 7;
int f[2][M][M][2];
int n,m,kk;
char s1[N],s2[M];
int main(){
scanf("%d%d%d",&n,&m,&kk);
scanf("%s%s",s1 + 1,s2 + 1);
int now = 0;
f[1][0][0][0] = f[0][0][0][0] = 1;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= m;++j)
for(int k = 1;k <= kk;++k){
f[now][j][k][0] = (1ll * f[now ^ 1][j][k][0] + f[now ^ 1][j][k][1]) % mod;
if(s1[i] == s2[j])
f[now][j][k][1] = (1ll * f[now ^ 1][j - 1][k][1]
+ f[now ^ 1][j - 1][k - 1][0] + f[now ^ 1][j - 1][k - 1][1]) % mod;
else f[now][j][k][1] = 0;
}
now ^= 1;
}
printf("%d\n",(f[now ^ 1][m][kk][0] + f[now ^ 1][m][kk][1]) % mod);
return 0;
}

luoguP2679 子串的更多相关文章

  1. [luoguP2679] 子串(DP)

    传送门 气死我了,自己YY的方法只能得70分. 一个下午都在搞这道题. 至于正解,真的不想写了. 请移步 here #include <cstdio> #define M 201 #def ...

  2. 子串 NOIP2015 D2T2 luoguP2679 字符串处理+DP

    AC通道! 题目大意: 给定两个长度分别为 n 和 m 的字符串 A 和 B,选取 A 中的 k 个子串,使这 k 个子串按照先后顺序连接起来后等于 B 子串.   输入输出样例 输入 #1 6 3 ...

  3. LeetCode[5] 最长的回文子串

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  5. C语言计算字符串子串出现的次数

    #include<stdio.h>#include<string.h>int substring(char *str,char *str1);//函数原型int main(vo ...

  6. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  7. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. SDUT-3364_欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在哥尼斯堡的一个公园里,有七座桥将普雷格 ...

  2. docker保存容器的修改

    docker保存容器修改 通过在容器中运行某一个命令,可以把对容器的修改保存下来, 这样下次可以从保存后的最新状态运行该容器.docker中保存状态的过程称之为committing, 它保存的新旧状态 ...

  3. JavaScript 错误

    try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. JavaScript 错误 当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错 ...

  4. docker学习笔记(总纲)

    阿里容器Docker简介 什么是Docker 为什么要用Docker 基本认识 Docker EE/Docker CE简介与版本规划 镜像 容器 仓库 数据卷 阿里容器服务的基本概念与其它名词解释 C ...

  5. sql查询报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题

    报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题 我的查询sql是 String sql2 =& ...

  6. F4NNIU 的常用 Linux 命令(2019-08-24)

    目录 F4NNIU 的常用 Linux 命令 停止防火墙 查看 IP 址 启动 deepin 的桌面 查看当前时区 查看 CPU 和内存信息 用户相关 日志 F4NNIU 的常用 Linux 命令 记 ...

  7. HDFS概念名称节点和数据节点-名称节点-文件系统元数据的持久状态

  8. 解决移动端1px边框问题的几种方法

    1.边框粗细原因 在移动端下设置border为1px,在某些设备上看比1px粗. 这些由于不同的手机有不同的像素密度.在window对象中有一个devicePixelRatio属性,他可以反应css中 ...

  9. 【[Offer收割]编程练习赛9 B】水陆距离

    [题目链接]:http://hihocoder.com/problemset/problem/1478 [题意] [题解] 一开始把所有的水域的位置都加入到队列中去; 然后跑一个bfs. 第一次到达的 ...

  10. Docker 领衔 OpenSource.com 2014十佳开源软件排行榜

    Docker 领衔 OpenSource.com 2014十佳开源软件排行榜 每年 Opensource.com 都会收集最佳的十个开源软件,今年也不例外,废话不多说,直接进入主题. Docker 应 ...