cf 1163D Mysterious Code (字符串, dp)
Posted uid001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf 1163D Mysterious Code (字符串, dp)相关的知识,希望对你有一定的参考价值。
大意: 给定字符串$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)的主要内容,如果未能解决你的问题,请参考以下文章
LightOJ 1220 Mysterious Bacteria