大意: 给定字符串$C$, 只含小写字母和'*', '*'表示可以替换为任意小写字母, 再给定字符串$S,T$, 求$S$在$C$中出现次数-$T$在$C$中出现次数最大值.

设$dp[i][j][k]$表示$C$的前$i$位, $S$和$T$分别匹配到第$j$位和第$k$位的最优解

可以用$kmp$优化转移, 复杂度是$O(26^2m^2n)$, 优化一下$kmp$的匹配的话可以达到$O(26m^2n)$

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1310;
char s1[N], s2[55], s3[55];
int f2[55], f3[55]; void getFail(char *s, int *f) {
int m = strlen(s);
f[0]=f[1]=0;
REP(i,1,m-1) {
int j=f[i];
while (j&&s[i]!=s[j]) j=f[j];
if (s[i]==s[j]) ++j;
f[i+1] = j;
}
REP(i,1,m-1) while (f[i]&&s[i]==s[f[i]]) f[i]=f[f[i]];
} int dp[N][55][55]; int main() {
scanf("%s%s%s", s1+1, s2, s3);
getFail(s2,f2),getFail(s3,f3);
int n1 = strlen(s1+1), n2 = strlen(s2), n3 = strlen(s3);
memset(dp,0xcf,sizeof dp);
int INF = dp[0][0][0], ans = INF;
dp[0][0][0] = 0;
REP(i,1,n1) REP(j,0,n2) REP(k,0,n3) if (dp[i-1][j][k]!=INF) {
int L='a',R='z';
if (s1[i]!='*') L=R=s1[i];
REP(c,L,R) {
int p2=j,p3=k;
while (p2&&s2[p2]!=c) p2=f2[p2];
if (s2[p2]==c) ++p2;
while (p3&&s3[p3]!=c) p3=f3[p3];
if (s3[p3]==c) ++p3;
dp[i][p2][p3] = max(dp[i][p2][p3], dp[i-1][j][k]+(p2==n2)-(p3==n3));
if (i==n1) ans = max(ans, dp[i][p2][p3]);
}
}
printf("%d\n", ans);
}

对两个串建立$AC$自动机优化, 复杂度是$O(26mn)$.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 2e3+10;
int n, T, tot;
char s1[N], s2[N], s3[N];
int ch[N][26], ac[N][26];
int fail[N], sz[N], val[N], sum[N];
queue<int> q; void build(int o) {
REP(i,0,25) ac[o][i]=o;
q.push(o), fail[o]=o;
while (q.size()) {
int x = q.front(); q.pop();
sum[x] = sum[fail[x]]+val[x];
REP(i,0,25) {
if (ch[x][i]) {
int y = ch[x][i];
q.push(y);
fail[y] = ac[fail[x]][i];
ac[x][i] = y;
} else ac[x][i] = ac[fail[x]][i];
}
}
} void add(int &o, char *s, int v) {
if (!o) o=++tot;
if (*s) add(ch[o][*s-'a'],s+1,v);
else val[o] += v;
} int dp[N][N]; int main() {
scanf("%s%s%s",s1+1,s2,s3);
add(T,s2,1),add(T,s3,-1);
build(T);
int n = strlen(s1+1);
memset(dp,0xef,sizeof dp);
dp[0][1]=0;
REP(i,1,n) {
REP(j,1,tot) {
int L='a',R='z';
if (s1[i]!='*') L=R=s1[i];
REP(c,L,R) {
int x = ac[j][c-'a'];
dp[i][x]=max(dp[i][x],dp[i-1][j]+sum[x]);
}
}
}
printf("%d\n", *max_element(dp[n]+1,dp[n]+tot));
}

cf 1163D Mysterious Code (字符串, dp)的更多相关文章

  1. Codeforces 1163D Mysterious Code(AC自动机+DP)

    用 AC自动机 来做有点想不到,捞一手就是学一手. 设 dp[ i ][ j ] 表示字符串 c 中的第 i 位到字典树上节点 j 的最大值是多少, word[ j ] 表示在节点 j 下对答案修改的 ...

  2. Codeforces 1150D(字符串dp)

    反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...

  3. 【BZOJ 2121】 (字符串DP,区间DP)

    2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...

  4. AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)

    引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度  pos[i][j]表示从i开始的j字符最早出现位 ...

  5. NOIP2015Day2T2子串(字符串dp)

    又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...

  6. CF 983B XOR-pyramid(区间dp,异或)

    CF 983B XOR-pyramid(区间dp,异或) 若有一个长度为m的数组b,定义函数f为: \(f(b) = \begin{cases} b[1] & \quad \text{if } ...

  7. HDU 5375——Gray code——————【dp||讨论】

    Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. CF - 1111D Destroy the Colony DP

    题目传送门 题意: 这个题目真的是最近遇到的最难读. 有一个长度n的字符串,每一位字符都代表的是该种种类的敌人. 现在如果一个序列合法的话,就是同一种种类的敌人都在字符串的左半边或者右半边. 现在有q ...

  9. fzu2172 字符串dp

    F - 巡了南山我巡北山 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

随机推荐

  1. HTML标签功能分类

    按功能类别对HTML标签进行分类,源自HTML 参考手册 基础 标签 描述 <!DOCTYPE> 定义文档类型. html 定义 HTML 文档. title 定义文档的标题. body ...

  2. webservice代码编写主要包括服务器端发布和客户端调用。

    一.java工程发布,java工程调用   (一).服务器端的编写 1.在eclipse里新建java project工程,创建功能类,通过关键字@webservice(name="newI ...

  3. java多线程编程详细总结

    一.多线程的优缺点 多线程的优点: 1)资源利用率更好2)程序设计在某些情况下更简单3)程序响应更快 多线程的代价: 1)设计更复杂虽然有一些多线程应用程序比单线程的应用程序要简单,但其他的一般都更复 ...

  4. TCP层sendmsg系统调用的实现分析

    概述 sendmsg系统调用在tcp层的实现是tcp_sendmsg函数,该函数完成以下任务:从用户空间读取数据,拷贝到内核skb,将skb加入到发送队列的任务,调用发送函数:函数在执行过程中会锁定控 ...

  5. python3笔记二十:时间操作time

    一:学习内容 time时间戳 time元组 time字符串 time时间转换图解 二:time 需要引入:import time 1.概念 UTC(世界协调时间):格林尼治天文时间,世界标准时间,在中 ...

  6. JDBC的URL

    JDBC的URL=协议名+子协议名+数据源名. 协议名总是“jdbc”. 子协议名由JDBC驱动程序的编写者决定. 数据源名也可能包含用户与口令等信息:这些信息也可单独提供. 几种常见的数据库连接 o ...

  7. Ironic 裸金属管理服务的网络模型

    目录 文章目录 目录 Bare-Metal networking in Neutron 核心网络类型 网络拓扑 抽象网络拓扑图 Neutron Implementation Neutron 了解裸金属 ...

  8. Servlet 表单数据 接收get post 参数实例

    Servlet 表单数据 很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法. GE ...

  9. Git代码行数统计命令

    统计zhangsan在某个时间段内的git新增删除代码行数 git log --author=zhangsan--since=2018-01-01 --until=2019-04-01 --forma ...

  10. Redux之combineReducers(reducers)详解

    大家好,最近有点忙,忙什么呢?忙着学习一个新的框架Redux,那么这个框架主要是用来做什么的,这篇博客暂时不做介绍,这篇博客针对有一定Redux开发基础的人员,所以今天我讲的重点是Redux里面很重要 ...