简单,数据为 1e3
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
char a[N], b[N];
int n, m, f[N][N];
void solve(){
cin >> n >> m >> a + 1 >> b + 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if (a[i] == b[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
}
cout << f[n][m] << "\n";
}
int main(){
solve();
return 0;
}
acwing模板:https://www.acwing.com/problem/content/899/
数据为 1e5
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 10; //数组大小
int n, a[maxn], b[maxn], f[maxn], p[maxn];
int main(){
cin >> n;
for (int i = 1; i <= n; i++){
scanf("%d", &a[i]);
p[a[i]] = i; //将第二个序列中的元素映射到第一个中
}
for (int i = 1; i <= n; i++){
scanf("%d", &b[i]);
f[i] = INF;
}
int len = 0;
f[0] = 0;
for (int i = 1; i <= n; i++){
if (p[b[i]] > f[len]) f[++len] = p[b[i]];
else {
int l = 0, r = len;
while (l < r){
int mid = (l + r) >> 1;
if (f[mid] > p[b[i]]) r = mid;
else l = mid + 1;
}
f[l] = min(f[l], p[b[i]]);
}
}
cout << len << "\n";
return 0;
}