测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出: c0 n0 c1 n1 c2 n2 ... 其中ci是第1行中第i个字符,ni是ci出现的次数。
I THIS IS A TEST i ng this is a long test string #
I 2 i 3 5 n 2 g 2
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str1 = in.nextLine();
HashMap<Character, Integer> hashMap = new HashMap<>();
ArrayList<Integer> arrayList = new ArrayList<>();
if (str1.equals("#")) {
break;
} else {
String str2 = in.nextLine();
for (int i = 0; i < str1.length(); i++) {
for (int j = 0; j < str2.length(); j++) {
if (str1.charAt(i) == str2.charAt(j))
if (hashMap.get(str1.charAt(i)) == null) {
hashMap.put(str1.charAt(i), 1);
} else {
hashMap.put(str1.charAt(i), hashMap.get(str1.charAt(i)) + 1);
}
}
arrayList.add(hashMap.get(str1.charAt(i)));
hashMap.put(str1.charAt(i), 0); //如果有相同的字符下一轮置为0
}
}
for (int i = 0; i < str1.length(); i++) {
if (arrayList.get(i) == null) {
System.out.println(str1.charAt(i) + " " + "0");
} else {
System.out.println(str1.charAt(i) + " " + arrayList.get(i));
}
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = br.readLine()) != null) {
if (s.equals("#")) break;
String str = br.readLine();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int count = 0;
for (int j = 0; j < str.length(); j++) {
char cc = str.charAt(j);
if (c == cc) count++;
}
System.out.println(c + " " + count);
}
}
}
}
package nowcoder02.demo47;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 终止条件一般不需要处理
while (scanner.hasNext()){
int[] record = new int[256];
char[] first = scanner.nextLine().toCharArray();
char[] second = scanner.nextLine().toCharArray();
for (char c : second) record[c]++;
for (char c : first) System.out.println(c+" "+record[c]);
}
}
}
//本代码思路:创建一个ascii整型数组,收集第二行字符串中每个字符出现的次数
//然后按第一行字符串的需要打印次数
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int[] ascii = new int[127];
char[] target = sc.nextLine().toCharArray();
char[] str = sc.nextLine().toCharArray();
for(int i=0;i<str.length;i++){
ascii[(int)(str[i])-1]++;
}
for(int i=0;i<target.length;i++){
char c = (char)(target[i]);
System.out.println(c+" "+ascii[(int)(target[i])-1]);
}
}
}
}