练习题:blackcat
百度文库:传送门
LIS学习:传送门
LCIS学习:传送门
关于输出LCS的所有答案:传送门
LCS输出路径
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 100 + 10;
const int INF = 1e9 + 10;
string a[qq], b[qq];
int dp[qq][qq], path[qq][qq];
int cnt;
void Print(int x, int y) {
if(x < 1 || y < 1) return;
if(path[x][y] == 0) {
Print(x - 1, y - 1);
} else if(path[x][y] == 1) {
Print(x - 1, y);
} else {
Print(x, y - 1);
}
if(a[x] == b[y]) {
if(cnt == 0) cnt++;
else cout << " ";
cout << a[x];
}
}
int main(){
string st;
int cnt1, cnt2;
while(cin >> st) {
cnt = cnt1 = cnt2 = 0;
while(st[0] != '#') {
a[++cnt1] = st;
cin >> st;
}
cin >> st;
while(st[0] != '#') {
b[++cnt2] = st;
cin >> st;
}
for(int i = 1; i <= cnt1; ++i) {
for(int j = 1; j <= cnt2; ++j) {
if(a[i] == b[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
path[i][j] = 0;
} else if(dp[i - 1][j] > dp[i][j - 1]) {
dp[i][j] = dp[i - 1][j];
path[i][j] = 1;
} else {
dp[i][j] = dp[i][j - 1];
path[i][j] = -1;
}
}
}
Print(cnt1, cnt2);
if(dp[cnt1][cnt2] != 0) cout << endl;
}
return 0;
}
LCIS 输出路径
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 500 + 10;
const int INF = 1e9 + 10;
int dp[qq][qq], a[qq], b[qq];
int px[qq][qq], py[qq][qq];
void Print(int x, int y, bool &f) {
if(x == -1 && y == -1) return;
Print(px[x][y], py[x][y], f);
if(a[x] == b[y]) {
if(f) f = false;
else printf(" ");
printf("%d", a[x]);
}
}
int main(){
int n1, n2;
scanf("%d", &n1);
for(int i = 1; i <= n1; ++i) {
scanf("%d", a + i);
}
scanf("%d", &n2);
for(int i = 1; i <= n2; ++i) {
scanf("%d", b + i);
}
mst(dp, 0);
mst(px, 0), mst(py, 0);
int ans = 0, ax, ay;
for(int i = 1; i <= n1; ++i) {
int maxn = 0;
int x1 = -1, y1 = -1, id;
for(int j = 1; j <= n2; ++j) {
dp[i][j] = dp[i - 1][j];
px[i][j] = i - 1;
py[i][j] = j;
if(b[j] < a[i] && dp[i - 1][j] > maxn) {
maxn = dp[i - 1][j], x1 = i - 1, y1 = j;
}
if(a[i] == b[j]) {
dp[i][j] = maxn + 1, px[i][j] = x1, py[i][j] = y1;
}
if(dp[i][j] > ans) {
ans = dp[i][j], ax = i, ay = j;
}
}
}
if(ans) {
bool r = true;
printf("%d\n", ans), Print(ax, ay, r);
puts("");
} else {
printf("0\n");
}
return 0;
}
LCIS 一维写法
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e3 + 10;
const int INF = 1e9 + 10;
int a[qq], b[qq], f[qq];
int n, m;
int main(){
int t; scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", a + i);
}
scanf("%d", &m);
for(int i = 1; i <= m; ++i) {
scanf("%d", b + i);
}
mst(f, 0);
for(int i = 1; i <= n; ++i) {
int maxn = 0;
for(int j = 1; j <= m; ++j) {
if(a[i] > b[j] && maxn < f[j]) {
maxn = f[j];
}
if(a[i] == b[j]) {
f[j] = maxn + 1;
}
}
}
int ans = 0;
for(int i = 1; i <= m; ++i) {
ans = max(ans, f[i]);
}
printf("%d\n", ans);
}
return 0;
}