开始读错题意了,题目要求的是相同花色之间有序,知道这一点以后就枚举一下序列跑几个LCS就好了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
char str[maxn];
int n, cnt[4], a[4][maxn];
struct Node {
int val, type;
}pre[maxn], cur[maxn];
void input(int x, int idx) {
if (str[1] == 's') a[0][cnt[0]++] = x, pre[idx].type = 0;
else if (str[1] == 'h') a[1][cnt[1]++] = x, pre[idx].type = 1;
else if (str[1] == 'd') a[2][cnt[2]++] = x, pre[idx].type = 2;
else a[3][cnt[3]++] = x, pre[idx].type = 3;
}
bool cmp1(int x, int y) { return x < y; }
bool cmp2(int x, int y) { return x > y; }
int dp[maxn][maxn];
int main() {
scanf("%d", &n);
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; i++) {
scanf("%s", str);
if (isdigit(str[0])) input(str[0]-'0', i), pre[i].val = str[0]-'0';
else if (str[0] == 'T') input(10, i), pre[i].val = 10;
else if (str[0] == 'J') input(11, i), pre[i].val = 11;
else if (str[0] == 'Q') input(12, i), pre[i].val = 12;
else if (str[0] == 'K') input(13, i), pre[i].val = 13;
else input(14, i), pre[i].val = 14;
}
int ans = n;
int b[4] = {0, 1, 2, 3};
do {
for (int s = 0; s < 16; s++) {
int num = 0;
for (int i = 0; i < 4; i++) {
if (s & (1<<i)) {
sort(a[b[i]], a[b[i]]+cnt[b[i]], cmp1);
}
else {
sort(a[b[i]], a[b[i]]+cnt[b[i]], cmp2);
}
for (int j = 0; j < cnt[b[i]]; j++) {
cur[num].val = a[b[i]][j], cur[num].type = b[i];
num++;
}
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (pre[i-1].val == cur[j-1].val && pre[i-1].type == cur[j-1].type) dp[i][j] = dp[i-1][j-1]+1;
else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
ans = min(ans, n-dp[n][n]);
}
} while (next_permutation(b, b+4));
printf("%d\n", ans);
return 0;
}