计蒜客 17412 Card Hand Sorting 最长公共子序列

链接:

https://nanti.jisuanke.com/t/17412

题意:

给你n张扑克,问你最少移动多少次使得排列好

排列好是指同种花色在一起,并且递增或者递减

题解:

生成所有排列好的数列,然后求lcs

代码:

31 int n;
32 int p[MAXN];
33 int dp[MAXN][MAXN];
34 VI v[4];
35 
36 int main() {
37     ios::sync_with_stdio(false), cin.tie(0);
38     cin >> n;
39     rep(i, 0, n) {
40         string s;
41         cin >> s;
42         int a, b;
43         if (s[0] == 'T') a = 10;
44         else if (s[0] == 'J') a = 11;
45         else if (s[0] == 'Q') a = 12;
46         else if (s[0] == 'K') a = 13;
47         else if (s[0] == 'A') a = 14;
48         else a = s[0] - '0';
49 
50         if (s[1] == 's') b = 0;
51         else if (s[1] == 'h') b = 1;
52         else if (s[1] == 'd') b = 2;
53         else b = 3;
54 
55         int c = a + b * 13;
56         p[i] = c;
57         v[b].pb(c);
58     }
59     rep(i, 0, 4) sort(all(v[i]));
60     int a[4] = { 0,1,2,3 };
61     int ans = INF;
62     do {
63         rep(i, 0, 1 << 4) {
64             VI vec;
65             rep(j, 0, 4) {
66                 VI t = v[a[j]];
67                 if ((i >> j) & 1) reverse(all(t));
68                 rep(i, 0, t.size()) vec.pb(t[i]);
69             }
70             memset(dp, 0, sizeof(dp));
71             rep(i, 1, n + 1) rep(j, 1, n + 1) {
72                 if (p[i - 1] == vec[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
73                 else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
74             }
75             ans = min(ans, n - dp[n][n]);
76         }
77     } while (next_permutation(a, a + 4));
78     cout << ans << endl;
79     return 0;
80 }

 

你可能感兴趣的:(计蒜客 17412 Card Hand Sorting 最长公共子序列)