CF——Technical Support

题目

题目传送门
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 t (1≤t≤500). Description of the test cases follows.

The first line of each test case contains one integer n (1≤n≤100) — the total number of messages in the dialog.

The second line of each test case consists of n 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,就会有一个或者多个回答Q。给你一个测试例子数,然后,接收测试例子长度,再接收测试例子(一个字符串)。判断这个字符串是否合法。

思想

一开始,我是这样子想的,判断整个字符串的Q和A的数量就好。先遍历一遍,统计整个字符串Q和A的数量。然后,最后一个字母不能为Q,然后,最后一段QA组合里,必须要A的数目大于等于Q。【因为保证整个字符串A的数量大于等于A,最后一段的QA组合中,A的数目也大于等于Q。所以在最后一段前面的QA一定是Q小于等于A】。
本来以为这个思想会天衣无缝,结果还是WA。然后我思考了很久,举出来一个反例。

1
14
QAAAAQQQAAQQAA

这个例子可以避开上面的规则。

其实就最后一段符合要求,但是如果把最后一段去了,倒数第二段就规避了上面的要求。

本来我想着那么就从后往前,一段一段拿出来比较,发现这样太麻烦了。

但是转念一想,为了保证每一个Q至少都有一个A。那么从后往前遍历,必须保证每时每刻的Q的数目都是小于等于A的。一旦出现这种Q大于A的情况,那么就表明整个字符串不符合要求了。

于是问题迎刃而解!

代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int t = reader.nextInt(); //  the numbers of cases.
        for (int i = 0; i < t; ++i) {
            int length = reader.nextInt();// the length of every case.
            reader.nextLine();// enter
            String example = reader.nextLine(); // the example
            int num_question = 0;
            int num_answer = 0;
            String result = "Yes";
            char temp = ' ';
            for (int j = length - 1; j >= 0; --j) {
                temp = example.charAt(j);
                if (temp == 'A')
                    ++num_answer;
                else
                    ++num_question;

                if (num_question > num_answer) {
                    result = "No";
                    break;
                }
            }


            System.out.println(result);
        }
    }
}

结果

WA了四次,终于AC了!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值