第一场-C-Card Hand Sorting

题目链接:点击打开链接

(一)题面:

Description

When dealt cards in the card game Plump it is a good idea to start by sorting the cards in hand by suit and rank. The different suits should be grouped and the ranks should be sorted within each suit. But the order of the suits does not matter and within each suit, the cards may be sorted in either ascending or descending order on rank. It is allowed for some suits to be sorted in ascending order and others in descending order. Sorting is done by moving one card at a time from its current position to a new position in the hand, at the start, end, or in between two adjacent cards. What is the smallest number of moves required to sort a given hand of cards?

Input

There will be several test cases. For the each case, the first line of input contains an integer n (1 ≤ n ≤ 52), the number of cards in the hand. The second line contains n pairwise distinct space-separated cards, each represented by two characters. The first character of a card represents the rank and is either a digit from 2 to 9 or one of the letters T, J, Q, K, and A representing Ten, Jack, Queen, King and Ace, respectively, given here in increasing order. The second character of a card is from the set {s, h, d, c} representing the suits spades ♠, hearts ♥, diamonds ♦, and clubs ♣.

Output

Output the minimum number of card moves required to sort the hand as described above.

Sample Input

4
2h Th 8c Qh
7
9d As 2s Qd 2c Jd 8h
4
2h 3h 9c 8c

Sample Output

1
2
0

(二)题目大意:

给定一些扑克牌(不包括大小王)需要你进行排序,牌总共有四种花色,每种花色13张,给定牌地序列后,你每次操作可以将一张牌取出然后插入到任意位置,重复操作,直至牌地序列有序,求最少地操作步骤。有序的定义为:
同一花色的的牌必须相邻放置,且同一花色的牌要么升序排列,要么降序排列,花色与花色之间没有先后要求。


(三)解题思路:

  1. 总共四种花色,每种花色的牌的摆放顺序只有两种,我们显然可以枚举出最终所有的有序状态:
    ①按花色的先后顺序有4!共有24种可能。
    ②按每种花色的排序方式共有2⁴种可能。
  2. 我们求解初始状态到各个最终状态之间个最少操作数,跟新最小值就是结果。
  3. 问题转换为:给定两个序列,求按上述调整方法由一个序列得到另外一个序列需要的最少操作数。
  4. 如果我们把第一个序列的每一个数看成是从小到大排列的(相当于自定义了大小关系),那么我们求第二个序列的最长单调递增子序列(按照自定义的大小关系),用总的长度减去子序列的长度得到就是需要操作的最少次数(想想,理解一下)。
  5. 我这里没有用4中提到的方法,而是直接求两个序列的最长公共子序列,然后总序列长度减去子序列的长度就是需要操作的最少次数。

(四)具体代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int n,dp[60][60];
char card[2];
map<char,int>mp;
vector<int>v[6];
int main(){
    mp['s']=0;mp['h']=1;mp['d']=2;mp['c']=3;mp['2']=0;mp['3']=1;mp['4']=2;mp['5']=3;mp['6']=4;
    mp['7']=5;mp['8']=6;mp['9']=7;mp['T']=8;mp['J']=9;mp['Q']=10;mp['K']=11,mp['A']=12;
    freopen("in.txt","r",stdin);
    while(~scanf("%d\n",&n)){
        for(int i=0;i<6;i++)v[i].clear();v[4].push_back(-1);
        for(int i=0;i<n;i++){
            scanf("%s",card);
            v[4].push_back(mp[card[0]]+13*mp[card[1]]);
            v[mp[card[1]]].push_back(mp[card[0]]+13*mp[card[1]]);
        }
        sort(v[0].begin(),v[0].end());
        sort(v[1].begin(),v[1].end());
        sort(v[2].begin(),v[2].end());
        sort(v[3].begin(),v[3].end());
        v[5].push_back(-1);
        int order[4]={0,1,2,3};
        int ans=100;
        do{
            for(int i=0;i<16;i++){
                for(int k=0;k<4;k++){
                    if(!v[order[k]].size())continue;
                    if((i>>k)&1)
                        for(int p=0;p<v[order[k]].size();p++)
                            v[5].push_back(v[order[k]][p]);
                    else
                        for(int p=v[order[k]].size()-1;p>=0;p--)
                            v[5].push_back(v[order[k]][p]);
                }
                memset(dp,0,sizeof dp);
                for(int i=1;i<=n;i++){
                    for(int j=1;j<=n;j++){
                        if(v[4][i]==v[5][j]) 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]);
                v[5].clear();v[5].push_back(-1);
            }
        }while(next_permutation(order,order+4));
        printf("%d\n",ans);
    }
    return 0;
}

(五)总结:每次补题的时候,才发现许多的题面也没有想像中的那么难,但有时候就是没有想下去的决心,有点被打击惯了味道,以后做题得时候还是得对自己抱有解决希望,实在不会也要嘴炮嘴炮?。


猜你喜欢

转载自blog.csdn.net/xbb224007/article/details/79968907