1084 Broken Keyboard(20 分)(C++)

本文介绍了一种通过输入字符串对比来检测键盘上哪些按键可能已损坏的算法。该算法使用双指针法,并结合哈希映射确保每个损坏的按键只被报告一次。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.

Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.

Input Specification:

Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or _ (representing the space). It is guaranteed that both strings are non-empty.

Output Specification:

For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.

Sample Input:

7_This_is_a_test
_hs_s_a_es

Sample Output:

7TI

解题思路:双指针法,当两字符串对应字符相同,则两指针同时加一,若不同则s1的指针加一;当不同时,用visit进行hash映射判断是否已经判断过。另外在开始处理是s2多加了一个特殊字符,是为了s1后面存在s2没有的字符。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
    string s1,s2;
    cin>>s1>>s2;
    transform(s1.begin(), s1.end(),s1.begin(),::toupper);
    transform(s2.begin(), s2.end(),s2.begin(),::toupper);
    int i = 0, j = 0;
    std::vector<bool> visit(256,false);
    s2 +='#';
    while(i<s1.length()){
        if(s1[i] != s2[j]){
            if(visit[s1[i]] == false)
                printf("%c",s1[i]);
            visit[s1[i]] = true;
        }
        else
            j++;
        i++;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值