CCC 24 J4 Troublesome Keys

题目:
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.
The following table shows how the available 15 marks are distributed.
Marks
Description
Bound
3
The quiet key is not pressed. A small number of keys are pressed.
N 50
3
The first troublesome key pressed is the silly key. A small num
ber of keys are pressed.
N 50
5
The first troublesome key pressed may be the silly key or the
quiet key. A small number of keys are pressed.
N 50
4
The first troublesome key pressed may be the silly key or the
quiet key. A large number of keys are pressed.
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.
La version fran¸caise figure `a la suite de la version anglaise. 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.
思路:

这道题的解题思路主要是通过比较两行输入,找出它们之间的差异,从而确定“坏键”以及显示的错误字母。具体来说,我们可以按照以下步骤进行处理:

  1. 首先,我们将两行输入转换为集合,并求出它们的差集(即不同的部分)。
  2. 如果差集中只有一个元素,那么这个元素对应的就是“坏键”(silly key),然后在第一行中找到该字符的位置,再在第二行中找到相同位置的字符,就是错误的显示字母。如果不存在“安静键”(quiet key),则输出“-”。
  3. 如果差集中有两个元素,我们需要进一步判断哪个是“坏键”,哪个是“安静键”。我们可以统计它们在第一行中出现的次数,出现次数较多的就是“坏键”。然后根据这个判断,找到两个字符在第一行的位置,再在第二行中找到相同位置的字符,即可得到错误的显示字母。最后输出另一个字符作为“安静键”。
题解:
line1 = input()
line2 = input()

set1 = set(line1)
set2 = set(line2)

result = set1.difference(set2)
if len(result) == 1:
    for i in result:
        a1 = i
    a2 = line1.find(a1)
    print(f"{a1} {line2[a2]}")
    print("-")
else:
    result = list(result)
    a1 = result[0]
    a2 = result[1]
    a1_c = line1.count(a1)
    a2_c = line1.count(a2)
    if a1_c == len(line1) - len(line2):
        if line1.index(a1) > line1.index(a2):
            print(f"{a2} {line2[line1.index(a2)]}")
            print(a1)
        else:
            l1=list(line1)
            l2=list(line2)
            for i in range(len(line1)):
                if l1[i] == a1:
                    l2.insert(i,a1)
            print(f"{a2} {l2[line1.index(a2)]}")
            print(a1)
    else:
        if line1.index(a2) > line1.index(a1):
            print(f"{a1} {line2[line1.index(a1)]}")
            print(a2)
        else:
            l1 = list(line1)
            l2 = list(line2)
            for i in range(len(line1)):
                if l1[i] == a2:
                    l2.insert(i, a2)
            print(f"{a1} {l2[line1.index(a1)]}")
            print(a2)
 
  • 17
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值