LCS & LCIS
LCS
操作对象:两个长度不一定相等的字符串。
string s, t;
int f[maxn][maxn];
int main()
{
cin >> s >> t;
int ls = s.length(), lt = t.length();
for (int i = 1; i <= ls; i++)
for (int j = 1; j <= lt; j++)
{
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if (s[i - 1] == t[j - 1])
f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
}
cout << f[ls][lt] << endl;
return 0;
}
LCIS
操作对象:两个长度不一定相等的数列。
const int maxn = 1005;
ll n, m, a[maxn], b[maxn], ans;
ll f[maxn][maxn], lcis[maxn][maxn];
int main()
{
n = read();
for (int i = 1; i <= n; ++i)
a[i] = read();
m = read();
for (int i = 1; i <= m; ++i)
b[i] = read();
for (int i = 1; i <= n; ++i)
{
for (int j = 1, k = 0; j <= m; ++j)
{
if (a[i] == b[j])
{
f[i][j] = f[i - 1][k] + 1;
for (int p = 1; p <= f[i - 1][k]; ++p)
lcis[j][p] = lcis[k][p];
lcis[j][f[i][j]] = a[i];
}
else
f[i][j] = f[i - 1][j];
if (b[j] < a[i] && f[i][j] > f[i][k])
k = j;
}
}
for (int i = 1; i <= m; ++i)
if (f[n][i] > f[n][ans])
ans = i;
printf("%lld\n", f[n][ans]);
for (int p = 1; p <= f[n][ans]; ++p)
printf("%lld ", lcis[ans][p]);
puts("");
return 0;
}