1. 程式人生 > >哈爾濱理工大學軟體與微電子學院第八屆程式設計競賽同步賽(高年級) E 小樂樂匹配字串 【最長公共子序列】

哈爾濱理工大學軟體與微電子學院第八屆程式設計競賽同步賽(高年級) E 小樂樂匹配字串 【最長公共子序列】

傳送門:https://ac.nowcoder.com/acm/contest/301/E

 

求最長公共子序列。

立個 flag 搞dp。

 

AC code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1e3+10;
char str1[MAXN], str2[MAXN];
int dp[MAXN][MAXN]; int main() { scanf("%s%s", &str1, &str2); memset(dp, 0, sizeof(dp)); int len1 = strlen(str1); int len2 = strlen(str2); for(int i = 1; i <= len1; i++){ for(int j = 1; j <= len2; j++){ if(str1[i-1] == str2[j-1]) { dp[i][j]
= max(dp[i-1][j-1]+1, dp[i][j]); } else{ dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } } } printf("%d\n", dp[len1][len2]); return 0; }