• 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
    
    • 1
    • 2
    • 3

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

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

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

    但是转念一想,为了保证每一个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);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    结果

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

  • 相关阅读:
    DDoS攻击与CC攻击:网络安全的两大挑战
    多目标优化两种算法:加权、智能优化算法
    Redis4.0源码编译和启动
    前微软CEO的“离别礼物“:Cortana差点改名为“Bingo”
    网鼎杯初赛--web1
    SpringBoot:整合监听器/过滤器和拦截器
    气象绘图软件Panoply使用教程 (不定时更新)
    如何Python统计文件中词出现的频率?(简单易上手版)
    1.2 监督学习
    02.5 自动微分
  • 原文地址:https://blog.csdn.net/weixin_44750790/article/details/127802571