求助,样例没过
  • 板块AT_dp_f LCS
  • 楼主Floating_Trees
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/12/24 20:23
  • 上次更新2023/12/24 22:34:59
查看原帖
求助,样例没过
953181
Floating_Trees楼主2023/12/24 20:23
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define pii pair<int,int>
#define mp make_pair

using namespace std;

const int N = 3030;
char s[N], t[N];
int dp[N][N];
pii from[N][N];
bool flag[N][N];

void print(int i, int j)
{
//	cout << i << " " << j << endl;
	if (i == -1 && j == -1)
		return ;
	print(from[i][j].first, from[i][j].second);
	if (s[i] == t[j])
		putchar(s[i]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	
	cin >> (s + 1) >> (t + 1);
	int lens = strlen(s + 1), lent = strlen(t + 1);
	for (int i = 0;i <= lens;i++)
		for (int j = 0;j <= lent;j++)
			from[i][j].first = -1, from[i][j].second = -1;
	for (int i = 1;i <= lens;i++)
		for (int j = 1;j <= lent;j++)
		{
			if (dp[i-1][j] > dp[i][j-1])
				dp[i][j] = dp[i-1][j], from[i][j] = mp(i-1,j);
			else
				dp[i][j] = dp[i][j-1], from[i][j] = mp(i,j-1);
			if (s[i] == t[j])
				dp[i][j] = dp[i-1][j-1], from[i][j] = mp(i-1,j-1);
		}
	print(lens,lent);
	
	/*
	cout << endl;
	for (int i = 1;i <= lens;i++)
		for (int j = 1;j <= lent;j++)
			cout << "from[" << i << "][" << j << "] = " << from[i][j].first << ", " << from[i][j].second << endl;
	*/
	
	return 0;
}
2023/12/24 20:23
加载中...