一组输入整数序列I和一组规则整数序列R,I和R序列的第一个整数为序列的个数(个数不包含第一个整数);整数范围为0~(2^31)-1,序列个数不限
输出描述:
从R依次中取出R<i>,对I进行处理,找到满足条件的I:
I整数对应的数字需要连续包含R<i>对应的数字。比如R<i>为23,I为231,那么I包含了R<i>,条件满足 。
按R<i>从小到大的顺序:
(1)先输出R<i>;
(2)再输出满足条件的I的个数;
(3)然后输出满足条件的I在I序列中的位置索引(从0开始);
(4)最后再输出I。
附加条件:
(1)R<i>需要从小到大排序。相同的R<i>只需要输出索引小的以及满足条件的I,索引大的需要过滤掉
(2)如果没有满足条件的I,对应的R<i>不用输出
(3)最后需要在输出序列的第一个整数位置记录后续整数序列的个数(不包含“个数”本身)
序列I:15,123,456,786,453,46,7,5,3,665,453456,745,456,786,453,123(第一个15表明后续有15个整数)
序列R:5,6,3,6,3,0(第一个5表明后续有5个整数)
输出:30, 3,6,0,123,3,453,7,3,9,453456,13,453,14,123,6,7,1,456,2,786,4,46,8,665,9,453456,11,456,12,786
说明:
30----后续有30个整数
3----从小到大排序,第一个R<i>为0,但没有满足条件的I,不输出0,而下一个R<i>是3
6--- 存在6个包含3的I
0--- 123所在的原序号为0
123--- 123包含3,满足条件
示例1
输入:
15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123 5 6 3 6 3 0输出:
30 3 6 0 123 3 453 7 3 9 453456 13 453 14 123 6 7 1 456 2 786 4 46 8 665 9 453456 11 456 12 786说明:
将序列R:5,6,3,6,3,0(第一个5表明后续有5个整数)排序去重后,可得0,3,6。 序列I没有包含0的元素。 序列I中包含3的元素有:I[0]的值为123、I[3]的值为453、I[7]的值为3、I[9]的值为453456、I[13]的值为453、I[14]的值为123。 序列I中包含6的元素有:I[1]的值为456、I[2]的值为786、I[4]的值为46、I[8]的值为665、I[9]的值为453456、I[11]的值为456、I[12]的值为786。 最后按题目要求的格式进行输出即可。
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
// 注意类名必须为 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
List<Integer> list = new ArrayList<>();
Set<Integer> params = new HashSet<>();
Map<Integer, List<NumObj>> map = new LinkedHashMap<>();
String a = in.nextLine();
String b = in.nextLine();
String[] l1 = a.split(" ");
String[] l2 = b.split(" ");
int[] a1 = new int[Integer.valueOf(l1[0])];
int[] a2 = new int[Integer.valueOf(l2[0])];
for (int i=1; i<l1.length; i++) {
a1[i-1] = Integer.valueOf(l1[i]);
}
for (int i=1; i<l2.length; i++) {
a2[i-1] = Integer.valueOf(l2[i]);
}
params = getParams(a2);
AtomicInteger count = new AtomicInteger(0);
for (int i=0; i<a1.length; i++) {
for (int tmp : params) {
if (isInclude(tmp, a1[i])) {
NumObj nb = new NumObj();
nb.index = i;
nb.val = a1[i];
if (map.containsKey(tmp)) {
map.get(tmp).add(nb);
}
else {
List<NumObj> listT = new ArrayList<>();
listT.add(nb);
map.put(tmp, listT);
}
}
}
}
Set<Integer> keys = map.keySet();
StringBuilder builder = new StringBuilder();
count.addAndGet(map.size() * 2);
Comparator<RealObj> comp = new Comparator<RealObj>() {
@Override
public int compare(RealObj o1, RealObj o2) {
return o1.num - o2.num;
}
};
TreeSet<RealObj> realSet = new TreeSet<RealObj>(comp);
// String s = setParam.stream().map(o -> String.valueOf(o)).collect(Collectors.joining(" "));
for (int tmp : params) {
if (map.containsKey(tmp)) {
int k = tmp;
List<NumObj> v = map.get(tmp);
StringBuilder tmpBuilder = new StringBuilder();
count.addAndGet(v.size()*2);
tmpBuilder.append(k+ " ");
tmpBuilder.append(v.size() + " ");
String tmpS = v.stream().map(o->o.toString()).collect(Collectors.joining(" "));
tmpBuilder.append(tmpS + " ");
RealObj obj = new RealObj();
obj.count = v.size();
obj.content = tmpBuilder.toString();
obj.num = k;
realSet.add(obj);
}
}
for (RealObj tmp : realSet) {
builder.append(tmp.content);
}
System.out.println(count.get() + " " + builder.toString());
}
}
public static class RealObj {
int num;
int count;
String content;
}
public static class NumObj {
int index;
int val;
public String toString() {
return index + " " + val;
}
}
public static boolean isInclude(int param, int sN) {
String pS = String.valueOf(param);
String s = String.valueOf(sN);
return s.contains(pS);
}
public static Set<Integer> getParams(int[] array) {
Set<Integer> set = new HashSet<>();
for (int tmp : array) {
set.add(tmp);
}
return set;
}
public static class Obj {
int num;
int count;
}
}