• 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了!
    在这里插入图片描述

  • 相关阅读:
    内核查找符号指针函数kallsyms_lookup_name
    pytest教程-47-钩子函数-pytest_sessionfinish
    iPhone 的健康数据采用的是 FHIR 传输格式
    HashMap的运用小练习
    完成基础实验【硬件课程设计】
    数据导入与预处理-拓展-pandas时间数据处理01
    K8S简单学习
    程序员刚毕业,先去大厂镀金还是先去小厂攒经验?
    网络编程 - UDP协议
    【微服务 31】超细的Spring Cloud 整合Seata实现分布式事务(排坑版)【云原生】
  • 原文地址:https://blog.csdn.net/weixin_44750790/article/details/127802571