CCC2024青少年组真题+解析(下)

Problem J4: Troublesome Keys

Problem Description

As Alex is typing, their keyboard is acting strangely. Two letter keys are causing trouble:

• One letter key displays the same wrong letter each time it is pressed. Alex calls this key the silly key. Oddly, Alex never actually tries to type the wrong letter displayed by the silly key.

• Another letter key doesn’t display anything when it is pressed. Alex calls this key the quiet key.

Alex presses the silly key at least once but they don’t necessarily press the quiet key. Your job is to determine the troublesome keys and the wrong letter that is displayed. Luckily, this is possible because Alex never presses the silly key immediately after pressing the quiet key and Alex never presses the quiet key immediately after pressing the silly key. 

Input Specification 

There will be two lines of input. The first line of input represents the N keys Alex presses on the keyboard. The second line of input represents the letters displayed on the screen. Both lines of input will only contain lowercase letters of the alphabet.

Marks100% N ≤ 500 000

Output Specification

There will be two lines of output. On the first line, output the letter corresponding to the silly key and the wrong letter displayed on the screen when it is pressed, separated by a single space. On the second line, output the letter corresponding to the quiet key if it is pressed. Output the dash character (-) if the quiet key is not pressed.

Sample Input 1

forloops

fxrlxxps

Output for Sample Input 1

o x
-

Explanation of Sample Output 1

The letter corresponding to the silly key was the letter o. Each time it was pressed, the wrong letter x was displayed. The quiet key was not pressed.

Sample Input 2

forloops

fxrlxxp

Output for Sample Input 2

o x

s

Explanation of Sample Output 2

The letter corresponding to the silly key was the letter o. Each time it was pressed, the wrong letter x was displayed. The quiet key corresponds to the letter s which was not displayed.

Sample Input 3

forloops

frlpz

Output for Sample Input 3

s z

o

Explanation of Sample Output 3

The letter corresponding to the silly key was the letter s. Each time it was pressed, the wrong letter z was displayed. The quiet key corresponds to the letter o which was not displayed

贴心的翻译

​问题描述

亚历克斯打字时,他们的键盘动作很奇怪。两个字母键出现问题:

•每次按下一个字母键都会显示相同的错误字母。亚历克斯称这把钥匙为愚蠢的钥匙。奇怪的是,亚历克斯实际上从来没有试图键入愚蠢的键显示的错误字母。 •按下另一个字母键时不会显示任何内容。亚历克斯称这把钥匙为静音键。 亚历克斯至少按了一次愚蠢的键,但他们不一定要按安静的键。您的工作是确定出现问题的按键和显示的错误字母。幸运的是,这是可能的,因为亚历克斯从不在按下愚蠢的键后立即按下静音键,亚历克斯也从不在按下无声键后立即按愚蠢的键。

输入规范

将有两行输入。第一行输入表示Alex在键盘上按下的N个键。第二行输入表示屏幕上显示的字母。两行输入都只包含字母表中的小写字母。 标记100%N≤500000

输出规格

将有两行输出。在第一行,输出与按下时屏幕上显示的愚蠢键和错误字母相对应的字母,用一个空格分隔。在第二行,如果按下静音键,则输出对应的字母。如果未按下安静键,则输出短划线字符(-)。

样本输入1

forloops
fxrlxxps

样本输入1的输出

o x
-

示例输出1的说明

与愚蠢的键对应的字母是字母o。每次按下它,都会显示错误的字母x。没有按下静音键。

样本输入2

forloops
fxrlxxp

样本输入2的输出

o x
s

示例输出2的说明

与愚蠢的键对应的字母是字母o。每次按下它,都会显示错误的字母x。安静键对应于未显示的字母s。

样本输入3

forloops
frlpz

样本输入3的输出

s z
o

示例输出3的说明

与愚蠢的键对应的字母是字母s。每次按下它,都会显示错误的字母z。安静键对应于未显示的字母o

解析:

其实作者认为这道题比第五道题还要难,这个题很考验思路,作者刚开始的想法是找到不一样的然后从这个点开始向后寻找,直到找到一个与当前值不一样的,然后基于让个值来判断是静音键还是愚蠢的键,但是很可惜,超时了。所以这道题的正解应该是统计字母出现的次数,基于次数来进行判断。明白了吗,现在我们来一步步的分析:

定义s1是输入的,s2是屏幕上显示的

首先如果是正常的键,那么在s1和s2所统计的个数应该是相同的,如果是愚蠢的键,那么愚蠢的键对应的字母(a1)就会在s1中比在s2中多,而它显示错误的字母(a2)就会比s2少,基于此,我们就可以快速的找到a2,并且知道按下了多少个愚蠢的键,如果是静音键,那么静音键对应的字母也是要在s1中比在s2中多,基于此,我们也可以找到静音键。

那么事情到这里结束了吗?并没有,我们要遍历两次,因为按下静音键和按下愚蠢的键的次数是可能相同的,所以我们要检验

#include<bits/stdc++.h>
using namespace std;

char s1,s2,q1='-';

int qa[150],qb[150],k;
int main(){
    string a,b;
    cin>>a>>b;
    for (int i=0;i<a.length();i++){
        qa[a[i]]++;
    }
    for (int i=0;i<b.length();i++){
        qb[b[i]]++;
    }
    for (int i=90;i<150;i++){
        if (qb[i]>qa[i]){
            s2=(char)i;
            k=qb[i]-qa[i];
            break;
        }
    }
    //情况1(从前往后) 
    for (int i=90;i<150;i++){
        if (qa[i]-qb[i]==k&&qb[i]==0){
            s1=(char)i;
            break;
        }
    }
    
    for (int i=90;i<150;i++){
        if (qa[i]>qb[i]&&qb[i]==0&&i!=s1){
            q1=(char)i;
            break;
        }
    }
    
    string t;
    for (int i=0;i<a.length();i++){
        if (a[i]==s1)t+=s2;
        else if (a[i]==q1)continue;
        else t+=a[i];
    }
    if (t==b){
        cout<<s1<<" "<<s2<<endl;
        cout<<q1;
        return 0;
    }
    //情况2(从后往前) 
    for (int i=149;i>90;i--){
        if (qa[i]-qb[i]==k&&qb[i]==0){
            s1=(char)i;
            break;
        }
    }
    
    for (int i=149;i>90;i--){
        if (qa[i]>qb[i]&&qb[i]==0&&i!=s1){
            q1=(char)i;
            break;
        }
    }
    
    string q;
    for (int i=0;i<a.length();i++){
        if (a[i]==s1)q+=s2;
        else if (a[i]==q1)continue;
        else q+=a[i];
    }
    if (q==b){
        cout<<s1<<" "<<s2<<endl;
        cout<<q1;
        return 0;
    }
    return 0;
}

Problem J5: Harvest Waterloo

题目描述

有一款广受欢迎的新收割模拟游戏叫做收割滑铁卢。游戏在一块长方形的南瓜地上进行,南瓜地里有成捆的干草和不同大小的南瓜。为了开始游戏,一个农民被安置在南瓜的位置。农民通过在整片土地上向左、向右、向上和向下移动来收获他们能够到的所有南瓜。农民不能斜向移动。农民也不能穿过一捆干草,也不能离开田地。你的工作是确定农民收获的所有南瓜的总价值。一个小南瓜值1美元,一个中等大小的南瓜值5美元,而一个大南瓜值10美元。

输入

第一行 n,o表示南瓜地的大小,接下来就是输入南瓜地里的情况,*是干草,s是小南瓜,l是中等南瓜,m是大南瓜,然后就是农夫的所在地x,y

输出

收获的总价值

解析:

直接深搜就完事了,注意要开标记数组

#include<bits/stdc++.h>
using namespace std;

int dr[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,o,sx,sy;
int s,m,l;
char a[10000][10000],book[10000][10000];

void dfs(int x,int y){
    if (x<=0||y<=0||x>n||y>o||a[x][y]=='*'||book[x][y]==1)return;
    book[x][y]=1;
    if(a[x][y]=='S')s++;
    else if (a[x][y]=='L')l++;
    else if (a[x][y]=='M')m++;
    for(int i=0;i<4;i++){
        int cmpx=x+dr[i][0],cmpy=y+dr[i][1];
        dfs(cmpx,cmpy);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值