题目描述
A. Red-Blue Shuffle
There are n cards numbered 1,…,n. The card i has a red digit ri and a blue digit bi written on it.
We arrange all n cards in random order from left to right, with all permutations of 1,…,n having the same probability. We then read all red digits on the cards from left to right, and obtain an integer R. In the same way, we read all blue digits and obtain an integer B. When reading a number, leading zeros can be ignored. If all digits in a number are zeros, then the number is equal to 0. Below is an illustration of a possible rearrangement of three cards, and how R and B can be found.
Two players, Red and Blue, are involved in a bet. Red bets that after the shuffle R>B, and Blue bets that R<B. If in the end R=B, the bet results in a draw, and neither player wins.
Determine, which of the two players is more likely (has higher probability) to win the bet, or that their chances are equal. Refer to the Note section for a formal discussion of comparing probabilities.
Input
The first line contains a single integer T (1≤T≤100) — the number of test cases.
Descriptions of T test cases follow. Each test case description starts with a line containing a single integer n (1≤n≤1000) — the number of cards.
The following line contains a string of n digits r1,…,rn — red digits on cards 1,…,n respectively.
The following line contains a string of n digits b1,…,bn — blue digits on cards 1,…,n respectively.
Note that digits in the same line are not separated with any delimiters.
Output
Print T answers for the test cases in order, one per line.
If Red has a strictly higher change to win, print “RED”.
If Blue has a strictly higher change to win, print “BLUE”.
If both players are equally likely to win, print “EQUAL”.
Note that all answers are case-sensitive.
输入样例
blablabla
输出样例
blablabla
题目大意
T组询问,每次给出字符串长度n和两个字符串a和b,分别进行全排列,比较每次a串和b串的大小,如果a比b大的总次数要更多,则输出RED,一样多EQUAL,否则BLUE。
全排列时如果一个数要比另一个数大,要保证其高位尽可能比另一个数大。
只需要设一个cnt变量。
该位a比b大时,cnt++,a比b小时,cnt–;
最后判断cnt与0的关系即可
C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
char s1[N], s2[N];
int n,t;
int main(){
scanf("%d", &t);
while(t--){
memset(s1,0,sizeof s1); //每次询问之前清空字符串s1,s2
memset(s2,0,sizeof s2);
scanf(" %d %s %s", &n, s1, s2);
int cnt = 0, len = strlen(s1);
for(int i = 0; i<len; i++)
{
if(s1[i]> s2[i]) cnt++;
if(s1[i]< s2[i]) cnt--;
}
if(cnt == 0) printf("EQUAL\n");
if(cnt >0) printf("RED\n");
if(cnt < 0) printf("BLUE\n");
}
return 0;
}