# 【DP && 两个字符串求 k 个顺序子串相同最大长度】CodeForces - 682D Alyona and Strings

Step1 Problem

Step2 Ideas:

dp[i][j][k][1]：第一个串第 i 位， 第二个串第 j 位，当中 k 个连续的子串，s1[i] == s2[j] 且是最后一个子串末尾
dp[i][j][k][0]：第一个串第 i 位， 第二个串第 j 位，当中 k 个连续的子串，s1[i] 和 s2[j] 不同时是最后一个子串末尾

dp[i][j][k][0] = max(dp[i][j][k][0], dp[i-1][j][k][0]);
dp[i][j][k][0] = max(dp[i][j][k][0], dp[i][j-1][k][0]);
dp[i][j][k][0] = max(dp[i][j][k][0], dp[i-1][j][k][1]);
dp[i][j][k][0] = max(dp[i][j][k][0], dp[i][j-1][k][1]);

dp[i][j][k][1] = max(dp[i-1][j-1][k][1], dp[i-1][j-1][k-1][0])+1;

Step3 Code:

#include<bits/stdc++.h>
using namespace std;
const int N = 1005;
int dp[N][N][15][2];
char s1[N], s2[N];
int main()
{
int n, m, K;
scanf("%d %d %d", &n, &m, &K);
scanf("%s %s", s1+1, s2+1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(s1[i] == s2[j]) {
for(int k = 1; k <= K; k++)
{
dp[i][j][k][1] = max(dp[i-1][j-1][k][1], dp[i-1][j-1][k-1][0])+1;
}
}
for(int k = 1; k <= K; k++)
{
dp[i][j][k][0] = max(dp[i][j][k][0], dp[i-1][j][k][0]);
dp[i][j][k][0] = max(dp[i][j][k][0], dp[i][j-1][k][0]);
dp[i][j][k][0] = max(dp[i][j][k][0], dp[i-1][j][k][1]);
dp[i][j][k][0] = max(dp[i][j][k][0], dp[i][j-1][k][1]);
}
}
}
printf("%d\n", max(dp[n][m][K][0], dp[n][m][K][1]));
}