DreamGrid has just found a set of Mahjong with suited tiles and a White Dragon tile in his pocket. Each suited tile has a suit (Character, Bamboo or Dot) and a rank (ranging from 1 to ), and there is exactly one tile of each rank and suit combination.

Character tiles whose rank ranges from 1 to 9

Bamboo tiles whose rank ranges from 1 to 9

Dot tiles whose rank ranges from 1 to 9

White Dragon tile
As DreamGrid is bored, he decides to play with these tiles. He first selects one of the suited tiles as the "lucky tile", then he picks tiles from the set of tiles and sorts these tiles with the following rules:
The "lucky tile", if contained in the tiles, must be placed in the leftmost position.
For two tiles and such that neither of them is the "lucky tile", if
is a Character tile and is a Bamboo tile, or
is a Character tile and is a Dot tile, or
is a Bamboo tile and is a Dot tile, or
and have the same suit and the rank of is smaller than the rank of ,
White Dragon tile is a special tile. If it's contained in the tiles, it's considered as the original (not-lucky) version of the lucky tile during the sorting. For example, consider the following sorted tiles, where "3 Character" is selected as the lucky tile. White Dragon tile, in this case, is considered to be the original not-lucky version of "3 Character" and should be placed between "2 Character" and "4 Character".

As DreamGrid is quite forgetful, he immediately forgets what the lucky tile is after the sorting! Given sorted tiles, please tell DreamGrid the number of possible lucky tiles.
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains two integers and (, ), indicating the number of sorted tiles and the maximum rank of suited tiles.
For the next lines, the -th line describes the -th sorted tile counting from left to right. The line begins with a capital letter (), indicating the suit of the -th tile:
If , then an integer () follows, indicating that it's a Character tile with rank ;
If , then an integer () follows, indicating that it's a Bamboo tile with rank ;
If , then an integer () follows, indicating that it's a Dot tile with rank ;
If , then it's a White Drangon tile.
It's guaranteed that there exists at least one possible lucky tile, and the sum of in all test cases doesn't exceed .
Output
For each test case output one line containing one integer, indicating the number of possible lucky tiles.
Sample Input
4 3 9 C 2 W C 4 6 9 C 2 C 7 W B 3 B 4 D 2 3 100 C 2 W C 9 3 9 C 1 B 2 D 3
Sample Output
2 4 7 25
Hint
For the first sample, "2 Character" and "3 Character" are possible lucky tiles.
For the second sample, "8 Character", "9 Character", "1 Bamboo" and "2 Bamboo" are possible lucky tiles.
Author: CHEN, Shihan
Source: The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
题面很长,反复看了很久,其实最后题意和麻将没太多关系
就是说有3种title的牌和一个白板,每种title的牌有m个,从1到m,比如m==9的话,万字牌就是从一万到九万,条就是一条到九条,然后一饼到九饼,再加白板,一共3m+1=28张牌。然后题目要做的事情是,先在这些牌里面选一张牌(是选,不是拿出来)作为"lucky title",然后再从中拿出n张牌(可能包含刚才选的那张"lucky title",也可能不包含)按以下规则进行排序:
1.如果有"lucky title","lucky title"要放在最左边。
2.对于不是"lucky title"的牌,Character要放在Bamboo和Dot的左边,Bamboo要放在Dot的左边,也就是要按Character、Bamboo、Dot的顺序放,然后再按牌上的数字从小到大放。
3.如果有白板,白板当做"lucky title",但是不放在最左边,而是放在"lucky title"那张牌原本该在的位置。比如题面中的那张图,三万比二万大还放在二万左边,所以三万是被强制放在第一个的,是"lucky title",所以白板当做三万,放在了三万原本该在的位置。
现在题目给你一个排好序的麻将序列,问你有多少张牌可能是“lucky title”,就直接把所有可能的情况列出来然后算呗,看代码吧。
#include <bits/stdc++.h>
#include <cstring>
#define INF 0x3f3f3f3f
#define MOD 1e9+7
using namespace std;
struct node
{
char s;
int rank;
}arr[1000005];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d ",&n,&m);
int flag=-1;
for(int i=0;i<n;i++)
{
arr[i].s=getchar();
if(arr[i].s!='W')
{
scanf("%d",&arr[i].rank);
}
else
flag=i;
getchar();
//cout<<arr[i].s<<endl;
}
int num=0;
if(flag==1)
num++;
if(n==1)
{
cout<<3*m<<endl;
continue;
}
if(arr[0].s==arr[1].s&&arr[0].rank>=arr[1].rank)
{
cout<<1<<endl;
continue;
}
if(arr[0].s=='D'&&(arr[1].s=='B'||arr[1].s=='C'))
{
cout<<1<<endl;
continue;
}
if(arr[0].s=='B'&&arr[1].s=='C')
{
cout<<1<<endl;
continue;
}
if(flag==-1)
{
cout<<3*m-(n-1)<<endl;
continue;
}
if(flag==0)
{
if(arr[flag+1].s=='C')
{
cout<<arr[flag+1].rank-1<<endl;
continue;
}
else if(arr[flag+1].s=='B')
{
cout<<arr[flag+1].rank-1+m<<endl;
continue;
}
else
{
cout<<arr[flag+1].rank-1+2*m<<endl;
continue;
}
}
if(flag==n-1)
{
if(arr[flag-1].s=='D')
{
cout<<m-arr[flag-1].rank+num<<endl;
continue;
}
else if(arr[flag-1].s=='B')
{
cout<<2*m-arr[flag-1].rank+num<<endl;
continue;
}
else if(arr[flag-1].s=='C')
{
cout<<3*m-arr[flag-1].rank+num<<endl;
continue;
}
}
//cout<<arr[flag-1].s<<' '<<arr[flag+1].s<<endl;
if(arr[flag-1].s==arr[flag+1].s)
{
cout<<arr[flag+1].rank-arr[flag-1].rank-1+num<<endl;
continue;
}
else if(arr[flag-1].s=='C')
{
if(arr[flag+1].s=='B')
{
cout<<m-arr[flag-1].rank+arr[flag+1].rank-1+num<<endl;
continue;
}
else if(arr[flag+1].s=='D')
{
cout<<2*m-arr[flag-1].rank+arr[flag+1].rank-1+num<<endl;
continue;
}
}
else if(arr[flag-1].s=='B')
{
if(arr[flag+1].s=='D')
{
cout<<m-arr[flag-1].rank+arr[flag+1].rank-1+num<<endl;
continue;
}
}
}
return 0;
}