ZOJ 4034 Mahjong Sorting 第15届浙江省省赛K题

Mahjong Sorting

Time Limit: 1 Second      Memory Limit: 65536 KB

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 ,

    then must be placed to the left 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.



题目意思自行百度。来让我们排一下可能性。

1。有没有W    

2。W前面零个,一个,两个  

3。W后面有没有

c1 w c3  //包含第一个

c1 c2 w c4 //不包含第一个

c1 w //全部

c2 w //不包含c2前面的

c1 c2 w //比上面少两

w //全部

c1 //全部

w c2 //第一位到c2

c1 b1 d1 //3m-n+1


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,shu;
    long long int a,b;
    int i,j,k,f;
    long long int s=0;
    vector<int>ss;
    char pai;
    scanf("%d",&n);
    while(n--)
    {
        j=0;f=0;k=0;ss.clear();
        scanf("%d%d",&a,&b);
        getchar();
        for(i=0;i<a;i++)
        {
            s=0;
            scanf("%c",&pai);
            getchar();
            if(pai=='B')s=b;
            else if(pai=='D')s=b+b;
            else if(pai=='W')
            {
                f=1;j=i;
                 if(ss.size()==1){s=ss[ss.size()-1];ss.push_back(s);}
                 else if(ss.size()==0){ss.push_back(1);    }
                 else if(ss.size()>1){s=ss[ss.size()-1]+1;ss.push_back(s);}
                continue;
            }
            scanf("%d",&shu);
            getchar();
            s=s+shu;
            ss.push_back(s);
        }
        for(i=0;i<ss.size()-1;i++)
        {
            if(ss[i]>ss[i+1]){printf("1\n");k=1;break;}
        }
        if(k==1)continue;
        if(f==1)
        {
            if(ss.size()==1)printf("%d\n",3*b);
            else if(j==a-1 && ss.size()==2)printf("%d\n",(3*b)-ss[j]+1);
            else if(j==a-1)printf("%d\n",(3*b)-ss[j]);
            else printf("%d\n",ss[j+1]-ss[j]);
        }
        if(f==0)
        {
            printf("%d\n",3*b-ss.size()+1);
        }
    }    
}


猜你喜欢

转载自blog.csdn.net/paycho/article/details/80186521