"Binary enumeration +lcs" Card Hand sorting

Source: Internet
Author: User

"Binary enumeration +lcs" Card Hand sorting topic description

When dealt cards in the card game plump it was a good idea-to-start by sorting the cards-hand by suit and rank. The different suits should be grouped and the ranks should is sorted within each suit. But the order of the suits does isn't matter and within each suit, the cards is sorted in either ascending or descending Order on rank. It is allowed for some suits to being sorted in ascending order and others in descending order.
Sorting is do by moving one card at a time from it current position to a new position in the hand, at the start, end, O R in between and adjacent cards. What's the smallest number of moves required to sort a given hand of cards?

Input

The. RST line of input contains a 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 the characters. The. RST 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 Increasi Ng order. The second character of a card is from the set {s, H, D, c} representing the suits spades?, Hearts?, Diamonds?, an D clubs?.

Output

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

Sample input

7
9d as 2s Qd 2c Jd 8h

Sample output

2

Take a look, 52, uh. State compression brute force binary enumeration? Then, how to change ah, not. It's so hard ...
The game is over, LCS, yes, it will.
LCS almost can't write ... Embarrassed
Position is judged by the shift character.

#include <bits/stdc++.h>using namespace std; #define LL long longmap<char,int>mp;inline void Init () {for (int    I=2;i<=9;++i) {mp[i+ ' 0 ']=i;    } mp[' T ']=10;    mp[' J ']=11;    mp[' Q ']=12;    mp[' K ']=13;    mp[' A ']=14;    mp[' s ']=0;    mp[' h ']=1;    mp[' d ']=2; mp[' C ']=3;}    struct node{int id;    int Val;    int block; int k;}    S[55];int dp[55],n;inline BOOL CMP (node X,node y) {if (X.K==Y.K) {return x.val<y.val; } return X.K&LT;Y.K;}   inline int LCS () {memset (dp,0,sizeof (DP));   int Ans=1;   Dp[0]=1;    for (int i=1;i<n;++i) {dp[i]=1;        for (int j=0;j<i;++j) {if (s[i].id>s[j].id) {Dp[i]=max (dp[j]+1,dp[i]);   }} Ans=max (Dp[i],ans); } return ans;    int main () {init ();    scanf ("%d", &n);    Char str[3];        for (int i=0;i<n;++i) {scanf ("%s", str);        S[i].val=mp[str[0]];        S[I].BLOCK=MP[STR[1]];    S[i].id=i;    } int arr[4]={0,1,2,3},ans=1e9; do{for (int i=0;i&Lt;16;++i) {for (int j=0;j<n;++j) {S[j].k=arr[s[j].block];            S[j].val=abs (S[j].val) * (((I&GT;&GT;S[J].K) &1)!=1?-1:1);            } sort (s,s+n,cmp);        Ans=min (Ans,n-lcs ());    }}while (Next_permutation (arr,arr+4));//full arrangement printf ("%d\n", ans); return 0;}

Binary Enumeration +lcs Card Hand sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.