1. 程式人生 > >BZOJ5073 小A的咒語(動態規劃)

BZOJ5073 小A的咒語(動態規劃)

  設f[i][j][0/1]為前i位選j段時其中第i位選/不選最多能匹配到哪,轉移時f[i][j][0]→f[i+1][j][0],f[i][j][1]→f[i+1][j][0],f[i][j][1]→f[i+1][j][1],f[i][j][0]→f[i+1][j+1][1]。失配時找到最後一位相同字元,具體見程式碼。感覺非常假,歡迎hack。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include
<algorithm> using namespace std; #define ll long long #define N 100010 #define M 102 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1
;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int T,n,m,t,f[N][M][2],pre[N][26]; char a[N],b[N]; int main() { #ifndef ONLINE_JUDGE freopen("bzoj5073.in","r"
,stdin); freopen("bzoj5073.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif T=read(); while (T--) { n=read(),m=read(),t=read(); scanf("%s",a+1),scanf("%s",b+1); for (int i=0;i<26;i++) pre[0][i]=-1; for (int i=1;i<=m;i++) { for (int j=0;j<26;j++) pre[i][j]=pre[i-1][j]; pre[i][b[i]-'a']=i; } f[0][0][1]=-1; bool flag=0; for (int i=1;i<=n;i++) { f[i][0][1]=-1; for (int j=1;j<=t;j++) { f[i][j][0]=max(f[i-1][j][0],f[i-1][j][1]); f[i][j][1]=pre[max(f[i-1][j-1][0],f[i-1][j][1])+1][a[i]-'a']; if (f[i][j][1]==m) {flag=1;break;} } if (flag) break; } if (flag) puts("YES");else puts("NO"); } return 0; }