题解 | #数据分类处理#
数据分类处理
http://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
暴力求解
import java.util.Scanner;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
// 输入I
int n = sc.nextInt();
Long[] I = new Long[n];
for(int i= 0; i< n; i++) {
I[i] = sc.nextLong();
}
// 输入R
int r = sc.nextInt();
Long[] R = new Long[r];
for(int i= 0; i< r; i++) {
R[i] = sc.nextLong();
}
// 对 R去重复 并排序
List<Long> rList = Arrays.stream(R).distinct().sorted()
.collect(Collectors.toList());
// 满足条件的结果
StringBuilder result = new StringBuilder();
int total = 0; // 总个数
for(Long rn : rList){
int count = 0; // 记录当前rn在I中满足条件的个数
// 存储满足条件的索引和值
StringBuilder b = new StringBuilder();
for(int j= 0; j < I.length; j++){
if(I[j].toString().contains(rn.toString())){
count++;
b.append(j).append(" ").append(I[j])
.append(" ");
}
}
if(count > 0){
total += count*2+2;
result.append(rn).append(" ").append(count)
.append(" ").append(b);
}
}
result.insert(0,total+" ");
System.out.println(result.toString().trim());
}
}
}