【二进制枚举+LCS】Card Hand Sorting
题目描述
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?输入
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 the minimum number of card moves required to sort the hand as described above.
样例输入
7
9d As 2s Qd 2c Jd 8h
样例输出
2
看一眼,52,嗯 状态压缩 暴力 二进制枚举?然后,怎么换啊,不会。好难。。。
比赛结束,LCS,嗯,会了 LCS差点不会写...尴尬 通过移位符判断位置。#includeusing namespace std;#define ll long longmap 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 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 >s[j].k)&1)!=1?-1:1); } sort(s,s+n,cmp); ans=min(ans,n-lcs()); } }while(next_permutation(arr,arr+4));//全排列 printf("%d\n",ans); return 0;}