跳到正文

题解

题解:P11034 【MX-X3-T1】「RiOI-4」What beats RiOI

___OnO___约 7 分钟
原文

这是 2026年2月6日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年9月14日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 7 分钟
题目
P11034

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

P11034 题解

题目传送门

1. 题目大意

输入两个字符串 ss ,tt 。检查 ss , tt 转小写后是否包含rioi字串:

    1. 若两者都有 rioi 子串,则输出 Either is ok! 。
    2. 若只有一个字符串拥有 rioi 子串,则输出那个字符串,再输出 for sure! 。
    3. 若两个字符串都没有 rioi 子串,则输出 Try again! 。

2. 代码实现

AC 代码(C++)
C++
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string s, t;

bool is_shan(string str) { // 判断字符串是不是“闪”的
    for (char& ch : str) {
        ch = tolower(ch);
    } // 转小写

    if (str.find("rioi") != string::npos) { // 判断是否有rioi子串
        return true;
    }

    return false;
}

int main() {
    cin >> s >> t;

    if (is_shan(s)) {
        if (is_shan(t)) { // s 和 t 满足
            cout << "Either is ok!";
        } else { // 仅 s 满足
            cout << s << " for sure!"; 
        }
    } else { 
        if (is_shan(t)) { // 仅 t 满足
            cout << t << " for sure!";
        } else { // 没有字符串满足
            cout << "Try again!";
        }
    }

    return 0;
}

3. 这就完了……吗?

我们可以简化一下代码,用一个变量answer存储所有的可能, answer 为 2 * is_shan(s) + is_shan(t) :

ss的情况(true 或 false)tt的情况 (true 或 false)answer
falsefalse00
falsetrue11
truefalse22
truetrue33

这样的话,我们的answer和 ss , tt 一一对应了!

AC 代码 Pro(C++)
C++
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string s, t;

bool is_shan(string str) { // 判断字符串是不是“闪”的
    for (char& ch : str) {
        ch = tolower(ch);
    } // 转小写

    if (str.find("rioi") != string::npos) { // 判断是否有rioi子串
        return true;
    }

    return false;
}

int main() {
    cin >> s >> t;
    int answer = 2 * is_shan(s) + is_shan(t);

    switch (answer) {
        case 0: cout << "Try again!"; break; // 没有一个满足
        case 1: cout << t << " for sure!"; break; // t 满足
        case 2: cout << s << " for sure!"; break; // s 满足
        case 3: cout << "Either is ok!"; break; // t, s 都满足
    }
    
    return 0;
}
AC 代码 Pro(Python)
Python
str1, str2 = input(), input()
answer = (str1.lower().count("rioi") != 0) * 2 +(str2.lower().count("rioi") != 0)

match answer: # Python 3.10+ 的模式匹配match,相当于switch
    case 0:
        print("Try again!")
    case 1:
        print(f"{str2} for sure!")
    case 2:
        print(f"{str1} for sure!")
    case 3:
        print("Either is ok!")

# 以下是不用match的版本 ↓

"""
if answer == 0:
      print("Try again!")
elif answer == 1:
      print(f"{str2} for sure!")
elif answer == 2:
      print(f"{str1} for sure!")
elif answer == 3:
      print("Either is ok!")
"""

4. 最短写法

AC 代码压行版(Python)
Python
str1, str2 = input(), input(); print(["Try again!", f"{str2} for sure!", f"{str1} for sure!", "Either is ok!"][(str1.lower().count("rioi") != 0) * 2 +(str2.lower().count("rioi") != 0)])

5. Q & A

这里会处理大家可能遇到的问题。

  • Q1: 为什么选取的是 answer 为 2 * is_shan(s) + is_shan(t) ?
  • A1:其实把所有情况一一对应就可以了, a * is_shan(s) + b * is_shan(t) ( a≠ba \neq b )其实都是可以的。
  • Q2:C++部分有没有其他方法转小写字母?
  • A2:std::fransform , std::ranges::to_lower(std=-stdc++20 及以上)也都可以。
  • Q3:为什么你能知道这道题要转小写再去寻找rioi?
  • A3:因为题目里说了, Rioi ,rIOI, RiOi 等都是可以匹配的,所以大小写要统一,其实转大写也不是不行。读者可以自行实现。