the link http://codeforces.com/problemset/problem/682/D
giving you two strings s and t,find k disjoint subsequences p1p2….pk,
that s and t can be represented as a1p1a2p2…akpkak+1 a refer to any character .
We can easy remind of LCS but this problem have to be more careful.
The state dp[i][j][k][e]
means in the prefix of s and k subseq have been made . e represent whether we add this character or not.
if we decide to no to add the current character as new subseq, we can only consider k as remain otherwise we can trans state to k+ 1.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#define ll long long
#define x first
#define y second
using namespace std;
const int maxn = 1005;
int dp[maxn][maxn][12][3];
int main()
{
string s,t;
int n,m,K;
cin>>n>>m>>K;
cin>>s>>t;
memset(dp,-1,sizeof(dp));
dp[0][0][0][0] = 0;
for(int i =0;i <=n;i++)
{
for(int j = 0 ; j<=m;j++)
{
for(int k = 0 ; k <= K;k++)
{
for(int e = 0; e <= 1;e++)
{
if(dp[i][j][k][e] == -1) continue;
dp[i+1][j][k][0] = max(dp[i+1][j][k][0],dp[i][j][k][e]);
dp[i][j+1][k][0] = max(dp[i][j+1][k][0],dp[i][j][k][e]);
if(s[i] == t[j])
{
dp[i+1][j+1][k+1][1] = max(dp[i+1][j+1][k+1][1],dp[i][j][k][e]+1);
if(e == 1)
{
dp[i+1][j+1][k][1] = max(dp[i+1][j+1][k][1],dp[i][j][k][e]+1);
}
}
}
}
}
}
cout<<max(dp[n][m][K][1],dp[n][m][K][0])<<endl;
return 0;
}