旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出
肯定坏掉的那些键。
输入在2行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过80个字符的串,由字母A-Z(包括大、小写)、数字0-9、
以及下划线“_”(代表空格)组成。题目保证2个字符串均非空。
按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有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 in = new Scanner(System.in); while (in.hasNextLine()) { String str1 = in.nextLine(); String str2 = in.nextLine(); func(str1,str2); } } private static void func(String str1, String str2){ HashSet<Character> set = new HashSet<>(); for(char ch: str2.toUpperCase().toCharArray()){ set.add(ch); } HashSet<Character> set2 = new HashSet<>(); for(char ch: str1.toUpperCase().toCharArray()){ if(!set.contains(ch) && !set2.contains(ch)){ System.out.print(ch); set2.add(ch); } } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String str1 = in.nextLine();//从键盘敲得键 String str2 = in.nextLine(); func(str1, str2); } } private static void func(String str1, String str2) { HashSet<Character> set = new HashSet<>(); HashSet<Character> set1 = new HashSet<>(); for (char ch : str2.toUpperCase().toCharArray()) { set.add(ch); } for (char ch : str1.toUpperCase().toCharArray()) { if (!set.contains(ch) && !set1.contains(ch)) { System.out.print(ch); set1.add(ch); } } } }
import java.util.*;
public class Main{
public static void func(String want,String real){ Set set = new HashSet(); for(Character ch : real.toUpperCase().toCharArray()){ set.add(ch); } Set setbroken = new HashSet(); for(char str : want.toUpperCase().toCharArray()){ if(!set.contains(str) && !setbroken.contains(str)){ System.out.print(str); setbroken.add(str); } } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); String want = sc.nextLine(); String real = sc.nextLine(); func(want,real); }
}
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Set<Character> set1 = new HashSet<>(); //存储实际输入的所有字符 List<Character> list = new ArrayList<>(); //存储坏掉的按键字符 String words; String virtualWords; words = scanner.nextLine(); virtualWords = scanner.nextLine(); //把实际输入的句子的字符入Set1 for (int i = 0; i < virtualWords.length(); i++) { set1.add(virtualWords.charAt(i)); } //遍历输入的句子的字符,把没输入的字符放入set2 for (int i = 0; i < words.length(); i++) { //获取当前字符的大写形式 char c = (words.charAt(i) + "").toUpperCase().charAt(0); //如果set1与set2都没有输入的这个字符,说明这个键坏了并且还没有入set2,然后set2加入这个字符 if(!set1.contains(words.charAt(i)) && !list.contains(c) && !list.contains(c + 32)){ //String c = words.charAt(i) + ""; list.add(c); } } Object[] objects = list.toArray(); for (int i = 0; i < objects.length; i++) { System.out.print(objects[i]); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str1 = sc.nextLine(); String str2 = sc.nextLine(); badKeyBoard(str1,str2); } public static void badKeyBoard(String str1, String str2) { StringBuilder sb = new StringBuilder(); Set<Character> set = new HashSet<>(); String str3 = str1.toUpperCase(); String str4 = str2.toUpperCase(); HashMap<Character,Integer> map = new HashMap<>(); for(int i = 0; i < str4.length(); i++) { char ch = str4.charAt(i); map.put(ch,map.getOrDefault(ch,0)+1); } for(int i = 0; i < str3.length(); i++) { char ch = str3.charAt(i); if(!map.containsKey(ch) && !set.contains(ch)) { sb.append(ch); set.add(ch); } } String ret = sb.toString(); System.out.println(ret); } }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String strExce = in.nextLine(); String strActual = in.nextLine(); funcPrint(strExce,strActual); } /** * 输出 * @param strExce 7_This_is_a_test * @param strActual _hs_s_a_es */ public static void funcPrint(String strExce,String strActual){ Set<Character> set = new HashSet<>(); //1、将坏键盘输出的数据放入set中 //因题例中输出结果均为大写,调用toUpperCase()存入 for (Character ch : strActual.toUpperCase().toCharArray()) { set.add(ch); } Set<Character> hasPrint = new HashSet<>(); //2、遍历好键盘输入的数据,得出有问题的键 for (Character ch : strExce.toUpperCase().toCharArray()) { //3、用hasPrint排除已经出现过的键 if (!set.contains(ch) && !hasPrint.contains(ch)){ System.out.print(ch); hasPrint.add(ch); } } } }
import java.util.*; public class Main{ public static void function(String str1,String str2){ HashSet<Character> set = new HashSet<>(); HashSet<Character> setapp = new HashSet<>(); for(char ch:str2.toUpperCase().toCharArray()){ set.add(ch); //存储了实际存储的数据; } for(char ch:str1.toUpperCase().toCharArray()){ if(!set.contains(ch) && !setapp.contains(ch)){ setapp.add(ch); System.out.print(ch); } } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str1 = sc.nextLine();//期望输入的 String str2 = sc.nextLine();//实际输入的 function(str1,str2); } }
import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { char[] should = sc.nextLine().toUpperCase().toCharArray();//自己以为键盘输入的 char[] actual = sc.nextLine().toUpperCase().toCharArray();//实际输出的 Set<Character> actualSet = new TreeSet<>(); for (char actCh : actual) { actualSet.add(actCh); } Set<Character> alreadySet = new TreeSet<>(); for (char shoCh : should) { if (!actualSet.contains(shoCh) && !alreadySet.contains(shoCh)) { System.out.print(shoCh); alreadySet.add(shoCh); } } } } }
import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()){ String s = sc.nextLine().toUpperCase(); String a = sc.nextLine().toUpperCase(); Set<Character> aset = new TreeSet<>(); for(char ch:a.toCharArray()){ aset.add(ch); } Set<Character> sset = new TreeSet<>(); for(char ch :s.toCharArray()){ if(!aset.contains(ch)){ if(!aset.contains(ch)){ System.out.print(ch); aset.add(ch); } } } } } }
//不是特别难想 利用contains查重的原理就行。 import java.util.Scanner; import java.util.Set; import java.util.HashSet; public class Main{ public static String find(String a,String b){ Set<Character> set = new HashSet<>(); StringBuilder sb = new StringBuilder(); for(char ch : b.toCharArray()){ set.add(ch); } for(char ch : a.toCharArray()){ if(!set.contains(ch)){ ch = Character.toUpperCase(ch); if(!sb.toString().contains(ch+"")){ sb.append(Character.toUpperCase(ch)); } } } return sb.toString(); } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String a = sc.next(); String b = sc.next(); System.out.println(find(a,b)); } } }
import java.util.*; public class Main{ public static void test(){ Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); String s2 = sc.nextLine(); Set st = new HashSet(); for (char ch : s2.toCharArray()){ st.add(ch); } Set st1 = new HashSet(); for (char ch : s1.toCharArray()){ if(!st.contains(ch) && !st1.contains(ch) && !st1.contains((char)(ch - 32))){ if(ch >= 'a' && ch <= 'z'){ ch = (char) (ch - 32); st1.add(ch); System.out.print(ch); }else{ st1.add(ch); System.out.print(ch); } } } } public static void main(String[] args) { test(); } }
import java.util.*; public class Main { public static void oldKey(String actural,String shortage){ //将实际输入与短缺的字符串全改为大写 String act=actural.toUpperCase(); String shorted=shortage.toUpperCase(); Set<Character> set1=new HashSet<>(); for(char ch:shorted.toCharArray()){//将短缺的字符串改为字符数组存入HashSet set1.add(ch); } Set<Character> set2=new HashSet<>(); for(char ch:act.toCharArray()){//将实际输入和短缺的进行比较 if(!set1.contains(ch)){//如果短缺中没有,则代表是该值坏掉了 if(!set2.contains(ch)){//再看是否之前已经遇到过 set2.add(ch);//如果没有,就将该值添加并打印 System.out.print(ch); } } } } public static void main(String[] args){ Main demo=new Main(); Scanner scanner=new Scanner(System.in); String s1=scanner.nextLine(); String s2=scanner.nextLine(); demo.oldKey(s1,s2); } }
import java.util.Scanner; public class Main{ public static String dech(String s) { String g="$"; for(int i=0;i<s.length();i++) { for(int j=0;j<g.length();j++) { if(s.charAt(i)==g.charAt(j)) { break; } if(j==g.length()-1) { g+=s.charAt(i); } } } return g.substring(1); } public static void main(String[] args) { Scanner in=new Scanner(System.in); String n=in.next(); String m=in.next(); String s="$"; for(int i=0;i<n.length();i++) { for(int j=0;j<m.length();j++) { if(n.charAt(i)==m.charAt(j)) { break; } if(j==m.length()-1) { for(int t=0;t<s.length();t++) { if(n.charAt(i)==s.charAt(t)) { break; } if(t==s.length()-1) { s+=n.charAt(i); } } } } } System.out.println(dech(s.toUpperCase())); } }
import java.util.Scanner; import java.util.ArrayList; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ char[] ch = sc.nextLine().toCharArray(); String str = sc.nextLine(); ArrayList<Character> lt = new ArrayList<>(); for(int i=0;i<ch.length;i++){ String c = String.valueOf(ch[i]); if(!str.contains(c)){ if(ch[i]>='a' && ch[i]<='z'){ ch[i]=(char)(ch[i]-32); } if(!lt.contains(ch[i])){ lt.add(ch[i]); } } } for(Character cc : lt){ System.out.print(cc); } } } }
import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String [] args) { Scanner sc=new Scanner(System.in); char [] c1=sc.next().toUpperCase().toCharArray(); char [] c2=sc.next().toUpperCase().toCharArray(); HashSet <String> hashset1=new HashSet<> (); HashSet <String> hashset2=new HashSet<> (); for(char c:c2) { hashset1.add(String.valueOf(c)); } String str=""; for(char c:c1) { if(!hashset1.contains(String.valueOf(c))) { if(!hashset2.contains(String.valueOf(c))){ str+=c; hashset2.add(String.valueOf(c)); } } } System.out.println(str); } }
importjava.util.Scanner;importjava.util.*;publicclassMain{publicstaticvoidmain(String[] args){Scanner scan = newScanner(System.in);String str1 = scan.nextLine();String str2 = scan.nextLine();String str3 = str1.toLowerCase();String str4 = str2.toLowerCase();String[] char1 = str3.split("");String[] char2 = str4.split("");LinkedHashSet<String> ts1 = newLinkedHashSet<String>();LinkedHashSet<String> ts2 = newLinkedHashSet<String>();StringBuilder sb = newStringBuilder();for(String c:char1)ts1.add(c);for(String c:char2)ts2.add(c);ts1.removeAll(ts2);for(String s:ts1)sb.append(s);String str5 = sb.toString().toUpperCase();System.out.println(str5);}}
算法思路:读取字符串的同时将其变为大写,然后剔除每个字符串重复的元素,然后进行比较。