题意:给定两个字符串s,t,和一个数k,求最长公共子序列并且每一段连续的子串长度>=k。
分析:先预处理下分别以s[i]和t[j]为结尾的LCP[i][j],直接dp就行了。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1000010;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=998244353;
const int INF=1000000010;
typedef double db;
typedef unsigned long long ull;
char s[2200],t[2200];
int l[2200][2200],dp[2200][2200];
int main()
{
int i,j,k,lens,lent;
while (scanf("%s%s", s, t)!=EOF) {
scanf("%d", &k);
lens=strlen(s);
lent=strlen(t);
for (i=1;i<=lens;i++)
for (j=1;j<=lent;j++)
if (s[i-1]==t[j-1]) l[i][j]=l[i-1][j-1]+1;
else l[i][j]=0;
for (i=1;i<=lens;i++)
for (j=1;j<=lent;j++)
if (l[i][j]==k) dp[i][j]=dp[i-k][j-k]+k;
else if (l[i][j]>k) dp[i][j]=max(dp[i-1][j-1]+1,dp[i-k][j-k]+k);
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
printf("%d\n", dp[lens][lent]);
}
return 0;
}