梦幻 | Reopening of Dream

· · 题解

\large\textbf{梦幻 | Reopening of Dream}

按题意模拟即可。

由于不知道每个 <title> 的长度为多少,以及为了避免忘记清空 char 数组,推荐使用 string 类型。

string title[4],read,write;

其中,\text{title} 是每道题目的名称,\text{read} 指选手的 freopen("<title>.in","r",stdin); 语句,\text{write} 指选手的 freopen("<title>.out","w",stdout); 语句。

我们将普通人作为 \text{type 1},将乐子人作为 \text{type 2},将见祖宗人作为 \text{type 3}。(原因会在下文中解释)

if ((read == "freopen(\"" + title[j] + ".in\",\"r\",stdin);") && (write == "freopen(\"" + title[j] + ".out\",\"w\",stdout);"))
    each_type = 1;

在讲解 \text{type 3} 之前,我们先补充一下 \text{substr()} 函数的用法:

假设 string s = "E_Space_is_amazing";

特别地:

else if ((read.substr(0, 10) == "//freopen(") && (read.substr(read.length()-2) == ");") || 
        (write.substr(0, 10) == "//freopen(") && (write.substr(write.length()-2) == ");"))
    each_type = 3;

值得一提的是,注意到在判断语句 (read.substr(read.length()-2) == ");") 中:当 read.length() < 2 时,会引起 Runtime Error;但是当 read.length() < 2 时,(read.substr(0, 10) == "//freopen(") 必然不成立,构成 && 短路,于是不会 Runtime Error。

最后,我们来解释一下本题的优先级,同时回答上文的问题。

综上,\text{type 1 < type 2 < type 3}。这样,代码的实现便容易了许多。

参考代码:

#include <bits/stdc++.h>
using namespace std;
int T,n,m;
string title[4],read,write;
inline int max(int a,int b)
{
    return (a>b) ? a : b;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    cin >> T >> n >> m;
    for (register int i=0; i<m; ++i)
    {
        cin >> title[i];
    }
    for (register int i=0; i<n; ++i)
    {
        int type=1;
        for (register int j=0; j<m; ++j)
        {
            int each_type=2;
            cin >> read >> write;
            if ((read == "freopen(\"" + title[j] + ".in\",\"r\",stdin);") && (write == "freopen(\"" + title[j] + ".out\",\"w\",stdout);"))
                each_type = 1;
            else if ((read.substr(0, 10) == "//freopen(") && (read.substr(read.length()-2) == ");") || (write.substr(0, 10) == "//freopen(") && (write.substr(write.length()-2) == ");"))
                each_type = 3;
            type=max(type,each_type);
        }
        if (type == 1)
            puts("PION2202 RP++.");
        else if (type == 3)
            puts("Wrong file operation takes you to your ancestors along with your 3 years' efforts on OI.");
        else
            puts("Good luck and have fun.");
    }
    return 0;
}

Ps. 感谢 Cloud_yx 对 \text{substr()} 函数的用法的补充。