首页 > 试题广场 >

Broken Keyboard (20)

[编程题]Broken Keyboard (20)
  • 热度指数:2614 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

输入描述:
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.


输出描述:
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.
示例1

输入

7_This_is_a_test<br/>_hs_s_a_es

输出

7TI
import java.util.*;
public class Main{
    public static void main(String[] args){
       Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str1 = sc.nextLine().toUpperCase();
            String str2 = sc.nextLine().toUpperCase();
            LinkedHashSet<Character> set = new LinkedHashSet<>();
            for (int i = 0; i <str1.length() ; i++) {
                if (!str2.contains(str1.charAt(i)+"")){
                    set.add(str1.charAt(i));
                }
            }
            Iterator i = set.iterator();
            while (i.hasNext()){
                System.out.print(i.next());
            }
        }
    }
}

发表于 2021-01-22 22:37:16 回复(0)
import java.util.*;
 
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String first=sc.next();
        String second=sc.next();
        first=first.toUpperCase();
        second=second.toUpperCase();
        for(int i=0;i<second.length();i++){
            first=first.replace(second.charAt(i)+"","");
        }
        String coutStr="";
        for(int i=0;i<first.length();i++){
            if(coutStr.contains(first.charAt(i)+"")){
                continue;
            }else{
                System.out.print(first.charAt(i));
                coutStr+=first.charAt(i);
            }
        }
        System.out.println();
    }
}

发表于 2018-10-05 20:52:21 回复(0)
s1, s2 = input().upper(), input().upper()
ans = ''
st = set(s1)-set(s2)
st1 = set()
for x in s1:
    if x in st:
        if x not in st1:
            ans += x
            st1.add(x)

print(ans)

发表于 2018-05-02 16:40:31 回复(0)

public class Main {

    public static void main(String[] args) {

        java.util.Scanner input = new java.util.Scanner(System.in);

        String originalString = input.nextLine();

        String typedOutString = input.nextLine();

        

        int[] keyboard = new int[128];

        // 扫描已键入的字符串,将小写字母转换成大写字母,使用 ASCII 码与 keyboard 对应,将成功键入的字符位置为 1

        for (int i = 0; i < typedOutString.length(); i++) {

            char ch = Character.toUpperCase(typedOutString.charAt(i));

            keyboard[ch] = 1;

        }

        

        // 扫描原始字符串,将小写字母转换成大小字母,去 keyboard 中检查是否已被成功键入,如果未成功键入,则打印对应的大写字母,

        // 并将 keyboard 中的对应的字符位置为 -1,表明已被打印

        for (int i = 0; i < originalString.length(); i++) {

            char ch = Character.toUpperCase(originalString.charAt(i));

            if (keyboard[ch] == 0) {

                System.out.print(ch);

                keyboard[ch] = -1;

            }

        }

        

        input.close();

    }

}


发表于 2018-02-10 17:47:28 回复(0)
#include <string>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 26 + 10 + 1 + 5;
int IF_Exit[maxn] = {0};

int Char2Id(char c){
    if(c >= '0' && c <= '9')
        return (c - '0');
    else if(c >= 'A' && c <= 'Z'){
        return (c - 'A' + 10);
    }
    else if(c >= 'a' && c <= 'z'){
        return (c - 'a' + 10);
    }
    else{
        return 36;
    }
}

int main(){
    string line1, line2;
    cin >> line1 >> line2;
    for(unsigned int i = 0; i<line2.size(); i++){
        int id = Char2Id(line2[i]);
        if(!IF_Exit[id]) IF_Exit[id] = 1;
    }
    for(unsigned int i = 0; i<line1.size(); i++){
        int id = Char2Id(line1[i]);
        if(!IF_Exit[id]){
            IF_Exit[id] ++;
            if(id >= 0 && id <= 9){
                printf("%d", id);
            }
            else if(id == 36){
                printf("_");
            }
            else if(id >= 10 && id <= 35){
                char c = id - 10 + 'A';
                printf("%c", c);
            }
        }
    }
    cout << endl;
    return 0;
}

发表于 2017-09-06 14:32:52 回复(0)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;

int main(){
    string input,output;
    getline(cin,input);
    getline(cin,output);
    for(auto &c : input){
        c = toupper(c);
    }
    for(auto &c : output){
        c = toupper(c);
    }
    int b=0;
    string temp;
    for(int a=0;a<input.size();a++){
        if(input[a]==output[b]){
            b++;
        }
        else{
            string::size_type pos = temp.find(input[a]);
            if(pos != temp.npos) continue;                                                                                                                     
            else {
                temp += input[a];
            }
        }
    }
    for(auto c : temp){
        cout << c;
    }
    return 0;
}

发表于 2020-10-08 23:07:47 回复(0)
//先把两个句子里面的所有小写的英文字母全部变成大写的
//然后以第一个句子从左向右遍历,第二个里面没有的都输出
//这里面有一个标记,可以去除重复的遍历,提升速度
//ctype.h头文件里包含着许多判断字符的函数,可以直接拿来用
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

void sen_to_upper(char s[])
{
    int len = strlen(s), i = 0, j = 0;
    for (i = 0; i < len; i++)
    {
        if (islower(s[i]))
        {
            s[i] = toupper(s[i]);
        }
    }
}

int main()
{
    int ans[128] = {0}, i = 0, j = 0, k = 0;
    char real[83], bre_out[83];
    cin >> real;
    cin >> bre_out;
    sen_to_upper(real);
    sen_to_upper(bre_out);
    k = strlen(real);
    for (i = 0; i < k; i++)
    {
        if (ans[int(real[i])] == 1)
        continue;
        else
        {
            if (strchr(bre_out, real[i]) == NULL)
            {
                cout << real[i];
            }
            ans[int(real[i])] = 1;
        }
    }
    return 0;
} 

编辑于 2018-08-24 15:34:13 回复(0)
a = input()
res = ''
if '<' in a:
    start = a.index('<')
    end = a.index('>')
    s1 = a[:start].upper()
    s2 = a[end+1:].upper()
else:
    b = input()
    s1 = a.upper()
    s2 = b.upper()
for c in s1:
        if c not in s2:
            if c not in res:
                res+=c
print(res)

发表于 2021-07-30 20:06:46 回复(0)
// 题目大概意思,键盘上有几个键坏了,为了找是哪几个,输入一串字符穿,
// 如果输出结果中没有那个字符,就是那个坏了。
//输出要求:输出坏的键位,用大写表示
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String output = scanner.nextLine();
        input = input.toUpperCase();
        output = output.toUpperCase();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            String a = input.substring(i,i+1);
            if (!output.contains(a) && !sb.toString().contains(a)){
                sb.append(a);
            }
        }
        String res = sb.toString();
        System.out.println(res);
    }
}

发表于 2021-02-06 14:30:40 回复(0)
#include<iostream>
(720)#include<vector>
#include<string>
using namespace std;
int main() {
	string s1, s2;
	cin >> s1 >> s2;
	int isOk[128] = { 0 };
	int isBroken[128] = { 0 };
	for (int i = 0; i < s1.size(); i++) {
		if (isOk[s1[i]] || isBroken[s1[i]]) {
			continue;
		}
		bool flag = false;
		for (int j = 0; j <= i&&j<s2.size(); j++) {
			if ((s1[i] >= 'a' && s1[i] <= 'z') && (s1[i] == s2[j] || s1[i] == s2[j] - 'A' + 'a')) {
				isOk[s1[i]] = 1;
				isOk[s1[i] - 'a' + 'A'] = 1;
				flag = true;
				break;
			}
			else if ((s1[i] >= 'A' && s1[i] <= 'Z') && (s1[i] == s2[j] || s1[i] - 'A' + 'a' == s2[j])) {
				isOk[s1[i]] = 1;
				isOk[s1[i] - 'A' + 'a'] = 1;
				flag = true;
				break;
			}
			else if (s1[i] == s2[j]) {//空格和数字
				isOk[s1[i]] = 1;
				flag = true;
				break;
			}
		}
		if (!flag) {
			isBroken[s1[i]] = 1;
			if (s1[i] >= 'a' && s1[i] <= 'z') {
				isBroken[s1[i] - 'a' + 'A'] = 1;
				printf("%c", s1[i] - 'a' + 'A');
			}
			else if (s1[i] >= 'A' && s1[i] <= 'Z') {
				isBroken[s1[i] - 'A' + 'a'] = 1;
				printf("%c", s1[i]);
			}
			else {
				printf("%c", s1[i]);
			}
		}
	}
}

发表于 2020-04-01 10:44:58 回复(0)
import java.util.*;

public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String a = sc.nextLine();
        String b = sc.nextLine();
        LinkedHashSet<Character> set = new LinkedHashSet<>();
        for (int i = 0;i<a.length();i++){
            if(!b.contains(String.valueOf(a.charAt(i)))){
                if(a.charAt(i)>='a'&&a.charAt(i)<='z'){
                    char x = Character.toUpperCase(a.charAt(i));
                    set.add(x);
                }else {
                    set.add(a.charAt(i));
                }
            }
        }
        for(Character character: set){
            System.out.print(character);
        }
    }
}

发表于 2020-03-05 17:03:32 回复(0)
填楼😀
#include<iostream>
(720)#include<string>
#include<set>
using namespace std;
set<char> outputSet;

char toUpper(char c){
	if (c >= 'a' && c <= 'z') return c + ('A' - 'a');
	return c;
}

int main(){
	string inStr, outStr;
	cin >> inStr >> outStr;
	for (int i = 0; i < outStr.size(); i++){
		outputSet.insert(toUpper(outStr[i]));
	}
	
	for (int i = 0; i < inStr.size(); i++){
		char tmp = toupper(inStr[i]);
		if (outputSet.find(tmp) == outputSet.end()){
			outputSet.insert(tmp);
			cout << tmp;
		}
	}
	cout << endl;
	return 0;
}
发表于 2020-03-01 17:58:53 回复(0)
import java.util.HashSet;
import java.util.LinkedHashSet;

import java.util.Scanner;

/**
 * @auther zh
 * @data 2019/4/10 19:49
  * 借鉴了其他人的明白题的意思,就是把两个字符串转大写,
  *然后不重复输出Str1中比Str2中的字符
 */
public class Main {
    public static void main(String[] args) {
            //HashSet是为了去重
        HashSet<Character> hashSet = new HashSet<>();
           // LinkedHashSet是有序的set集合,按照添加进去的顺序输出  
            LinkedHashSet<Character> linkedHashSet = new LinkedHashSet<>();
        StringBuilder sb =new StringBuilder();
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();
        String res1 = str1.toUpperCase();
        String res2 = str2.toUpperCase();
        for (int i = 0; i < res2.length(); i++) {
            char c = res2.charAt(i);
            hashSet.add(c);
        }
        for (int i = 0; i < res1.length(); i++) {
            if (!hashSet.contains(res1.charAt(i))){
                linkedHashSet.add(res1.charAt(i));
            }
        }
        for (Character c : linkedHashSet){
            System.out.print(c);
        }
    }
}

发表于 2019-04-10 20:05:08 回复(0)
//欢迎参观鄙人的博客https://blog.csdn.net/qq_33375598
#include<cstdio>
#include<cstring>
bool hashTable[128] = {false};//用来标记字符是否已被输出
const int maxn = 100;
char str1[maxn], str2[maxn];
int main(int argc, char const *argv[])
{
  fgets(str1, maxn + 1, stdin);
  fgets(str2, maxn + 1, stdin);
  int len1 = strlen(str1) - 1;//获取长度
  int len2 = strlen(str2) - 1;

for (int i = 0; i < len1; ++i)//枚举第一个字符串每个字符
{
  int j;
  char c1, c2;
  for (j = 0; j < len2; ++j)//枚举第二个字符串每个字符
  {
    c1 = str1[i];
    c2 = str2[j];
    if(c1 >= 'a' && c1 <= 'z') c1 -= 32;//小写字母转化为大写
    if(c2 >= 'a' && c2 <= 'z') c2 -= 32;
    if(c1 == c2) break;
  }
  if(j == len2 && hashTable[c1] == false){//第二个字符串中未出现c1,且c1位被输出过
    printf("%c", c1); 
    hashTable[c1] = true;
  }
}
printf("\n");
  return 0;
}

发表于 2019-03-01 09:27:55 回复(0)
如下代码在牛客能AC但是在PAT有两个测试用例过不了,有劳大神看下
#include <iostream>
#include <set>
using namespace std;

int main() {     set<char> output;
    const int MAX_N = 200;
    const int MAX_INPUT = 80;
    bool alphabet[MAX_N] = {false};
    char target[MAX_INPUT] = {'*'};
    char real[MAX_INPUT] = {'*'};
    cin.getline(target, 80);
    cin.getline(real, 80);
    
    for(int i = 0; i < 80; i++) {
        if(target[i] >= 'a' && target[i] <= 'z') {
            target[i] = target[i] - 32;
        }
        if(real[i] >= 'a' && real[i] <= 'z') {
            real[i] = real[i] - 32;
        }
    }
    for(int i = 0; i < 80; i++) {
        if(real[i] != '*') {
            int index = (int)real[i];
            alphabet[index] = true; 
        }
    }
    
    for(int k = 0; k < 80; k++) {
        if(target[k] != '*') {
            int index = (int)target[k];
            if(!alphabet[index]) {
                if(output.find(target[k]) == output.end()) {
                    cout<<target[k];
                }
                output.insert(target[k]);
            }
        }
    }
    cout<<endl;     return 0;
}

发表于 2019-01-13 19:22:20 回复(0)

//先自己去重然后在第一行去重第二行
//最后结果再次去重两次
有没有大神解释一下为什么最后不去重不对


import java.io.*;
public class Main {
public static void main(String[] args)throws IOException
{

    BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
    StringBuffer first_String=new StringBuffer(reader.readLine().toUpperCase());
    StringBuffer temp=new StringBuffer(reader.readLine().toUpperCase());
    cud(temp);
    cud(first_String);
    for(int j=0;j<first_String.length();j++)
    {
        for (int i = 0; i < temp.length(); i++)
        {
            if(temp.charAt(i)==first_String.charAt(j))
            {

                first_String.delete(j,j+1);

            }

        }
    }
    for(int i=0;i<5;i++)
    {
        cud(first_String);
    }
    System.out.println(first_String.toString().toUpperCase());

}

public static void cud(StringBuffer temp) {
    for(int j=0;j<temp.length();j++) {
        for (int i = j+1; i < temp.length(); i++) {
            if(temp.charAt(i)==temp.charAt(j))
            {
                temp.delete(i,i+1);
            }

        }

    }

}

}

发表于 2018-10-09 20:34:43 回复(0)
s=input().upper()
t=input().upper()+'#'
res,Set='',set([])
i,j=0,0
while i<len(s):
    if s[i]==t[j]:
        i,j=i+1,j+1
    else:
        if s[i] not in Set:
            Set.add(s[i])
            res+=s[i]
        i+=1
print(res)

发表于 2018-09-06 15:07:50 回复(0)
//思路:加加减减
//1.利用数字字母hash表,统计原始串中各个字符出现的次数,而残缺串有啥减啥。
//2.表中剩下大于零的,肯定就是缺失的。通过原始串控制输出顺序。
//#include "stdafx.h"
#include <cstdio>
#include <cstring>
int cnt[26 + 10 + 1] = {0};
int myhash(char c){
    int key = 36;
    if(c >= 'a' && c<= 'z') key =10 + c - 'a';
    else if(c >= 'A' && c <= 'Z')key =10 + c - 'A';
    else if(c >= '0' && c <= '9')key = c - '0';
    return key;
}
char up(char c){
    if(c >= 'a' && c<= 'z') c = c - 'a' + 'A';
    return c;
}
int main(){
    char s1[90], s2[90];
    scanf("%s %s", s1, s2);
    for(int i = 0; i < strlen(s1); ++i){
        cnt[myhash(s1[i])]++;
    }
    for(int i = 0; i < strlen(s2); ++i){
        cnt[myhash(s2[i])]--;
    }
    for(int i = 0; i < strlen(s1); ++i){
        if(cnt[myhash(s1[i])] > 0){
            printf("%c", up(s1[i]));
            cnt[myhash(s1[i])] = 0;
        }
    }
    return 0;
}
发表于 2018-07-27 15:20:25 回复(0)
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <vector>
using namespace std;

int main()
{
    string a, b;

    cin >> a >> b;
    int i = 0, j = 0; 
    vector<int> h(256, 0);
    //b += '!';
    while (i<a.length()) 
    {
        while ((i<a.length())&&(a[i] == b[j])) { i++;j++; }
        if (a[i] != b[j]) {
            //if (a[i] >= 'a' &&a[i] <= 'z') a[i] = a[i] + 'A' - 'a';
            a[i] = toupper(a[i]);
            if (h[a[i]]==0) {
                h[a[i]] = 1;
                printf("%c", a[i]);
            }
            i++;
        }
    }
    cout << endl;

    system("pause");
    return 0;
}
 
发表于 2018-07-25 14:32:26 回复(0)
#include<iostream>
#include<map>
#include<vector>
#include<string>
using namespace std; 
char getUpperLetter(char a){
    if( a >= 'a' && a <= 'z'){
          a += 'A'-'a';
    }
    return a;
}
char get(char a){
    if( a >= 'A' && a <= 'Z'){
          a -= 'A'-'a';
    }
    return a;
}
int main(){
    string a1,a2;
    cin>>a1;
    cin>>a2;
    char b;
    map<char,int> a;    
    for(int i=0;i<a1.length();i++){
        a[a1[i]]=-1;
    }
    for(int i=0;i<a2.length();i++){
        a[a2[i]]=1;
    }
    for(int i=0;i<a1.length();i++){
        if(a[a1[i]]==-1){
            if(a1[i]>='a'&&a1[i]<='z'){
                cout<<getUpperLetter(a1[i]);
            }else{
                cout<<a1[i];
            }
            if(a1[i]>='a'&&a1[i]<='z'||a1[i]>='a'&&a1[i]>='A'&&a1[i]<='Z'){
                   b=getUpperLetter(a1[i]);
                   a[b]=1;
                   b=get(b);
                a[b]=1;
            }
            a[a1[i]]=1;
            
        }
    }
    return 0;
}

发表于 2018-05-18 00:40:41 回复(0)

问题信息

难度:
35条回答 12475浏览

热门推荐

通过挑战的用户