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; }