题目链接:http://codeforces.com/contest/682/problem/D
思路:dp[i][j][l][0]表示a串前i和b串前j利用a[i] == b[j]所得到的最长子序列,
dp[i][j][l][1]表示a串前i和b串前j不利用a[i] == b[j]所得到的最长子序列,
所以,dp[i][j][l][0] = max(dp[i-1][j-1][l][0] ,max(dp[i-1][j-1][l-1][0],dp[i-1][j-1][l-1][1])) + 1
dp[i][j][l][1] = max(max(dp[i-1][j][l][0] ,dp[i-1][j][l][1]),max(dp[i][j-1][l][0] ,dp[i][j-1][l][1]))
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e3 + 3; char a[N],b[N]; int n,m,k; int dp[N][N][11][2]; int main() { scanf("%d %d %d",&n,&m,&k); scanf("%s %s",a+1,b+1); for(int i = 1 ;i <= n ;i++) { for(int j = 1 ;j <= m ;j++) { for(int l = 1 ;l <= k ;l++) { if(a[i] == b[j]) dp[i][j][l][0] = max(dp[i-1][j-1][l][0] ,max(dp[i-1][j-1][l-1][0],dp[i-1][j-1][l-1][1])) + 1; dp[i][j][l][1] = max(max(dp[i-1][j][l][0] ,dp[i-1][j][l][1]),max(dp[i][j-1][l][0] ,dp[i][j-1][l][1])); } } } printf("%d ",max(dp[n][m][k][0] ,dp[n][m][k][1])); return 0; }