题目:
You work in the quality control department of technical support for a large company. Your job is to make sure all client issues have been resolved.
Today you need to check a copy of a dialog between a client and a technical support manager. According to the rules of work, each message of the client must be followed by one or several messages, which are the answer of a support manager. However, sometimes clients ask questions so quickly that some of the manager's answers to old questions appear after the client has asked some new questions.
Due to the privacy policy, the full text of messages is not available to you, only the order of messages is visible, as well as the type of each message: a customer question or a response from the technical support manager. It is guaranteed that the dialog begins with the question of the client.
You have to determine, if this dialog may correspond to the rules of work described above, or the rules are certainly breached.
Input:
Each test contains multiple test cases. The first line contains the number of test cases tt (1 \le t \le 5001≤t≤500). Description of the test cases follows.
The first line of each test case contains one integer nn (1 \le n \le 1001≤n≤100) — the total number of messages in the dialog.
The second line of each test case consists of nn characters "Q" and "A", describing types of messages in the dialog in chronological order. Character "Q" denotes the message with client question, and character "A" — the message with technical support manager answer. It is guaranteed that the first character in the line equals to "Q".
Output:
For each test case print "Yes" (without quotes) if dialog may correspond to the rules of work, or "No" (without quotes) otherwise.
题意:
客户会问Q(问题),然后由经理来回答(A)
规则:
客户可以一个个问Q,经理一个个回答
QAQA-->(Yes)
客户可以连续问Q,经理的回答A,必须要大于等于客户问题的数
示例如下:
QQAA-->(Yes) //经理的回答A等于客户问题Q
QQAAA-->(Yes) //经理的回答A大于客户问题Q
QQAAQAQAA-->(Yes)
QQA-->(No)
QQAQAQAA-->(No)
解题思路:
我们可以这样考虑:
用q_nums来记录未被回答的问题的数量。
AC代码如下:
#include<stdio.h>
int main()
{
int n,q=0,a=0;
int m,i,j,q_nums=0;//q_nums记录未被A消去的Q的数量
char s[600];
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&m);
scanf("%s",&s);
for(j=0;j<m;j++){
if(s[j]=='Q'){
q_nums++;//如果出现Q,则q_nums++
}
if(s[j]=='A'){
q_nums--;//如果出现A,则q_nums--
}
if(q_nums<0){
q_nums=0;//如果在Q后面出现的A的数量大于Q,则符合规定,Q被回答完毕
}//q_nums=0-->无剩余的Q需要被回答
}
if(q_nums>0){//若q_nums>0,则任然有Q没被回答
printf("No\n");
}
else{
printf("Yes\n");
}
q_nums=0;
}
return 0;
}