Codeforces 1163D(kmp、dp)
要点
- \(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)的更多相关文章
- Codeforces 1168C(二进制、dp)
要点 '&'操作暗示二进制上按位思考 对于y为1的位,要求x和y之间要至少有两个此位为1的(包含x.y),这样&起来才不是0.而这些位中只要存在一个是ok的即可 dp去求每个x的每个位 ...
- codeforces 932E Team Work(组合数学、dp)
codeforces 932E Team Work 题意 给定 \(n(1e9)\).\(k(5000)\).求 \(\Sigma_{x=1}^{n}C_n^xx^k\). 题解 解法一 官方题解 的 ...
- 数的划分(DFS、DP)
https://www.luogu.com.cn/problem/P1025 题目描述 将整数n分成k份,且每份不能为空,任意两个方案不相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是 ...
- 九度OJ 1255:骰子点数概率 (递归、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:316 解决:29 题目描述: 把n个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入n,打印出S的所有可能的值出现的概率. 输入: 输入包 ...
- Codeforces 126B. Password(KMP,DP)
Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...
- 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 ...
- Codeforces 808G Anthem of Berland(KMP+基础DP)
题意 给定一个字符串 \(s\) ,一个字符串 \(t\) ,其中 \(s\) 包含小写字母和 "?" ,\(t\) 只包含小写字母,现在把 \(s\) 中的问号替换成任意的小写字 ...
- codeforces 651C(map、去重)
题目链接:http://codeforces.com/contest/651/problem/C 思路:结果就是计算同一横坐标.纵坐标上有多少点,再减去可能重复的数量(用map,pair存一下就OK了 ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
随机推荐
- Apache CGI 配置
在/etc/apache2/apache2.conf末尾添加 ServerName lacalhost:80 然后启动CGI模块: sudo a2enmod cgi 3.重启Apache: syste ...
- HTML5响应式导航
HTML5响应式导航HTML5,响应式,jQuery特效,HTML5导航,HTML5响应式导航是一款基于HTML5实现的深灰色响应式导航菜单. 地址:http://www.huiyi8.com/sc/ ...
- Java接口 详解(一)
一.基本概念 接口(Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合.接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 如果一个类只由 ...
- HDU 1850 Being a Good Boy in Spring Festival(博弈·Nim游戏)
Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32 ...
- OpenCV——PS滤镜 漩涡 vertex
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- Confd 配置指导
Quick Start Guide Before we begin be sure to download and install confd. Select a backend confd supp ...
- 1026 Table Tennis (30)(30 分)
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...
- Python中日志的格式化输出
import logging logfile = 'e:\\a.txt' # logging.basicConfig(filename=logfile,level=logging.INFO) # lo ...
- H+ Se7en WebUI
http://www.zi-han.net/theme/hplus/webim.html
- js中全局变量的一点小知识点
js中有三种方式定义全局变量: 在任何函数外面直接执行var语句,例如:var f="value"; 直接添加一个属性到全局变量上,在web浏览器中,全局对象名为window.例如 ...