要点

  • \(dp[i][j][k]\)表示主串已经到第\(i\)位时,\(s\)匹配在\(j\)位、\(t\)匹配在\(k\)位的最大得分
  • 本来就要试填一层循环,如果转移也写在循环里的化复杂度承受不了,于是开两个表kmp预处理一下。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; char ch[1010], s[55], t[55];
int len, ls, lt, Ns[55], Nt[55];
int Ps[55][30], Pt[55][30];
int dp[1010][55][55], ans = 0xcfcfcfcf; void GetNext(char *s, int len, int *Next, int P[][30]) {
Next[1] = 0;
for (int i = 2, j = 0; i <= len; i++) {
while (j && s[j + 1] != s[i]) j = Next[j];
if (s[i] == s[j + 1]) j++;
Next[i] = j;
}
for (int i = 0; i <= len; i++)
for (int c = 0; c < 26; c++) {
int j = i;
while (j && s[j + 1] != c + 'a') j = Next[j];
if (s[j + 1] == c + 'a') j++;
P[i][c] = j;
}
} int main() {
scanf("%s%s%s", ch + 1, s + 1, t + 1);
len = strlen(ch + 1), ls = strlen(s + 1), lt = strlen(t + 1);
GetNext(s, ls, Ns, Ps), GetNext(t, lt, Nt, Pt); memset(dp, 0xcf, sizeof dp);
dp[0][0][0] = 0;
for (int i = 0; i < len; i++)
for (int j = 0; j <= ls; j++)
for (int k = 0; k <= lt; k++)
for (int c = 0; c < 26; c++) {
if (ch[i + 1] != '*' && ch[i + 1] - 'a' != c) continue;
if (dp[i][j][k] == 0xcfcfcfcf) continue;
int a = Ps[j][c], b = Pt[k][c];
dp[i + 1][a][b] = max(dp[i + 1][a][b], dp[i][j][k] + (a == ls) - (b == lt));
}
for (int i = 0; i <= ls; i++)
for (int j = 0; j <= lt; j++)
ans = max(ans, dp[len][i][j]);
return !printf("%d\n", ans);
}

Codeforces 1163D(kmp、dp)的更多相关文章

  1. Codeforces 1168C(二进制、dp)

    要点 '&'操作暗示二进制上按位思考 对于y为1的位,要求x和y之间要至少有两个此位为1的(包含x.y),这样&起来才不是0.而这些位中只要存在一个是ok的即可 dp去求每个x的每个位 ...

  2. codeforces 932E Team Work(组合数学、dp)

    codeforces 932E Team Work 题意 给定 \(n(1e9)\).\(k(5000)\).求 \(\Sigma_{x=1}^{n}C_n^xx^k\). 题解 解法一 官方题解 的 ...

  3. 数的划分(DFS、DP)

    https://www.luogu.com.cn/problem/P1025 题目描述 将整数n分成k份,且每份不能为空,任意两个方案不相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是 ...

  4. 九度OJ 1255:骰子点数概率 (递归、DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:316 解决:29 题目描述: 把n个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入n,打印出S的所有可能的值出现的概率. 输入: 输入包 ...

  5. Codeforces 126B. Password(KMP,DP)

    Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...

  6. Codeforces Round #579 (Div. 3) Complete the Projects(贪心、DP)

    http://codeforces.com/contest/1203/problem/F1 Examples input 1 - - output 1 YES input 2 - - output 2 ...

  7. Codeforces 808G Anthem of Berland(KMP+基础DP)

    题意 给定一个字符串 \(s\) ,一个字符串 \(t\) ,其中 \(s\) 包含小写字母和 "?" ,\(t\) 只包含小写字母,现在把 \(s\) 中的问号替换成任意的小写字 ...

  8. codeforces 651C(map、去重)

    题目链接:http://codeforces.com/contest/651/problem/C 思路:结果就是计算同一横坐标.纵坐标上有多少点,再减去可能重复的数量(用map,pair存一下就OK了 ...

  9. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

随机推荐

  1. Object.is() Pollyfill

    if (!Object.is) { Object.is = function(x, y) { // SameValue algorithm if (x === y) { // Steps 1-5, 7 ...

  2. 三款功能强大代码比较工具Beyond compare、DiffMerge、WinMerge

    我们经常会遇到需要比较同一文件的不同版本,特别是代码文件.如果人工去对比查看,势必费时实力还会出现纰漏和错误,因此我们需要借助一些代码比较的工具来自动完成这些工作.这里介绍3款比较流行且功能强大的工具 ...

  3. 每天一个linux命令(4):pwd命令

    版权声明更新:2017-05-08博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下命令cd. 2 开发 ...

  4. 1119 Pre- and Post-order Traversals(30 分)

    1119 Pre- and Post-order Traversals(30 分) Suppose that all the keys in a binary tree are distinct po ...

  5. Linux 切换字符界面和图形界面

    1. 切换方式 # root 权限 systemctl get-default # 获取当前模式 systemctl set-default graphical.target # 设置开机为图形界面 ...

  6. 连接Oracle数据库的Hibernate配置…

    连接Oracle数据库的Hibernate配置文件 连接Oracle的Hibernate配置文件有两种格式,一种是xml格式的,另一种是Java属性文件格式的.下面分别给出这两种格式配置文件的代码. ...

  7. ASP.NET MVC (Umbraco)中如何设置网站超时自动退出

    原文章请参考  https://edgewebware.com/2014/06/automatically-log-out-members-send-login-page-umbraco/ 在网站开发 ...

  8. MySQL写入中文乱码

    这点确实很迷,我的数据库属性确实设置了utf-8字符集,但写入中文还是乱码,后来是直接修改了全局配置才修改过来. 1.进入MySQL的本地安装路径,我的安装路径是"C:\Program Fi ...

  9. John 尼姆博弈

    John Little John is playing very funny game with his younger brother. There is one big box filled wi ...

  10. HTML5学习笔记(二)新元素和功能

    <canvas> 新元素(必须使用脚本来绘制图形) 标签 描述 <canvas> 标签定义图形,比如图表和其他图像.该标签基于 JavaScript 的绘图 API HTML5 ...