H - ZCC loves straight flush
Description
We call a five-card hand a Straight Flush when all five cards are consecutive and of the same suit. You are given a five-card hand. Please tell ZCC how many cards must be changed so as to get a Straight Flush.
Cards are represented by a letter('A', 'B', 'C', 'D') which denotes the suit and a number('1', '2', $\cdots$, '13') which denotes the rank.
Note that number '1' represents ace which is the largest actually. "1 2 3 4 5" and "10 11 12 13 1" are both considered to be consecutive while "11 12 13 1 2" is not.
Input
For each test case, there are five short strings which denote the cards in a single line. It's guaranteed that all five cards are different.
Output
Sample Input
3 A1 A2 A3 A4 A5 A1 A2 A3 A4 C5 A9 A10 C11 C12 C13
Sample Output
0 1 2
一开始这个题感觉很复杂,要考虑的情况太多,后来发现其实只要枚举就行,一开始考虑的程序只是考虑了数字连续的情况,认为A1 A5 C1 C10 D8这种情况下没有连续的数字,应当改变的牌的个数是4,后来发现其实是改变三张,加上A2 A3 A4就可以了,因为没考虑到这种情况,所以一直A不过,==...,最后发现真的是服自己了,菜死了,做题考虑情况太不全面
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int t[15][5];
int main()
{
int n,num,max,cnt;
char suit;
string s;
cin>>n;
while(n--)
{
max=1,cnt=0,num=0;
memset(t,0,sizeof(t));
for(int i=0;i<5;i++)
{
cin>>s;
for(int j=1;s[j]!='\0';j++)
{
num=num*10+s[j]-'0';
}
suit=s[0];
t[num][suit-'A'+1]=1;
if(num==1)
t[14][suit-'A'+1]=1;
num=0;
}
for(int i=1;i<=4;i++)//关键代码部分
{
for(int j=1;j<=10;j++)
{
cnt=0;
for(int k=0;k<5;k++)
{
if(t[j+k][i]==1)
cnt++;
}
if(max<cnt)
max=cnt;
}
}
cout<<5-max<<endl;
}
return 0;
}