3.18携程2道ac
import java.util.HashMap; import java.util.Scanner; /** */ //Zhang San //Zhan Ai public class Main { /*请完成下面这个函数,实现题目要求的功能 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ ******************************开始写代码******************************/ static int calcSimilarity(String name1, String name2) { String[] n1 = name1.split(" "); String[] n2 = name2.split(" "); int j = 0; if (n1.length == n2.length) { for (int i = 0; i < n1.length; i++) { char[] chars1 = n1[i].toCharArray(); char[] chars2 = n2[i].toCharArray(); HashMap<String, String> hashMap1 = new HashMap<>(); HashMap<String, String> hashMap2 = new HashMap<>(); for (int i1 = 0; i1 < chars1.length; i1++) { hashMap1.put(String.valueOf(chars1[i1]), ""); } for (int i1 = 0; i1 < chars2.length; i1++) { hashMap2.put(String.valueOf(chars2[i1]), ""); } for (int i1 = 0; i1 < chars1.length; i1++) { if (!hashMap2.containsKey(String.valueOf(chars1[i1]))) { j += chars1[i1]; } } for (int i1 = 0; i1 < chars2.length; i1++) { if (!hashMap1.containsKey(String.valueOf(chars2[i1]))) { j += chars2[i1]; } } } } else if (n1.length > n2.length) { for (int i = 0; i < n2.length; i++) { HashMap<String, String> hashMap1 = new HashMap<>(); HashMap<String, String> hashMap2 = new HashMap<>(); char[] chars1 = n1[i].toCharArray(); char[] chars2 = n2[i].toCharArray(); for (int i1 = 0; i1 < chars1.length; i1++) { hashMap1.put(String.valueOf(chars1[i1]), ""); } for (int i1 = 0; i1 < chars2.length; i1++) { hashMap2.put(String.valueOf(chars2[i1]), ""); } for (int i1 = 0; i1 < chars1.length; i1++) { if (!hashMap2.containsKey(String.valueOf(chars1[i1]))) { j += chars1[i1]; } } for (int i1 = 0; i1 < chars2.length; i1++) { if (!hashMap1.containsKey(String.valueOf(chars2[i1]))) { j += chars2[i1]; } } } int k = n1.length - n2.length; for (int g = n1.length - k; g < n1.length; g++) { for (int i = 0; i < n1[g].toCharArray().length; i++) { j += n1[g].toCharArray()[i]; } } //Ali Ba Ba //Xie Cheng } else { for (int i = 0; i < n1.length; i++) { char[] chars1 = n1[i].toCharArray(); char[] chars2 = n2[i].toCharArray(); HashMap<String, String> hashMap1 = new HashMap<>(); HashMap<String, String> hashMap2 = new HashMap<>(); for (int i1 = 0; i1 < chars1.length; i1++) { hashMap1.put(String.valueOf(chars1[i1]), ""); } for (int i1 = 0; i1 < chars2.length; i1++) { hashMap2.put(String.valueOf(chars2[i1]), ""); } for (int i1 = 0; i1 < chars1.length; i1++) { if (!hashMap2.containsKey(String.valueOf(chars1[i1]))) { j += chars1[i1]; } } for (int i1 = 0; i1 < chars2.length; i1++) { if (!hashMap1.containsKey(String.valueOf(chars2[i1]))) { j += chars2[i1]; } } } int k = n2.length - n1.length; for (int g = n2.length - k; g < n2.length; g++) { for (int i = 0; i < n2[g].toCharArray().length; i++) { j += n2[g].toCharArray()[i]; } } } return j; } /******************************结束写代码******************************/ public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String name1 = in.nextLine(); String name2 = in.nextLine(); int sum = calcSimilarity(name1, name2); System.out.println(sum); } } }
全部评论
(1) 回帖