/
C305A.java
94 lines (80 loc) · 2.31 KB
/
C305A.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class C305A {
/***/
public class Group{
List<String> list=Collections.synchronizedList(new ArrayList<String>());
/***/
public void add(String a){
Iterator<String> i=this.list.iterator();
while(i.hasNext()){
if(!canSum(a,i.next()))
return;
}
this.list.add(a);
// System.err.println(this.list);
}
}
public Group newGroup(String a){
Group g=new Group();
g.list.add(a);
return g;
}
/***/
public static boolean canSum(String a,String b){
int m=Math.max(a.length(), b.length());
while(a.length()<m)
a="0"+a;
while(b.length()<m)
b="0"+b;
for(int i=0;i!=a.length();i++)
if((a.charAt(i)!='0')&&(b.charAt(i)!='0'))
return false;
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
C305A instance=new C305A();
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); // STDIN
try {
Integer n=Integer.valueOf(reader.readLine().trim());
String[] numbers=reader.readLine().trim().split(" ", n);
List<Group> listGroups=Collections.synchronizedList(new ArrayList<Group>());
for(int i=0;i!=numbers.length;i++){
String num=numbers[i];
if(!listGroups.isEmpty()){
Iterator<Group> iterGroup=listGroups.iterator();
while(iterGroup.hasNext())
iterGroup.next().add(num);
}
listGroups.add(instance.newGroup(num));
}
if(!listGroups.isEmpty()){
Group maxGroup=null;
Iterator<Group> iterGroup=listGroups.iterator();
while(iterGroup.hasNext()){
Group g=iterGroup.next();
if((maxGroup==null)||(g.list.size()>maxGroup.list.size()))
maxGroup=g;
}
if(maxGroup!=null){
System.out.println(maxGroup.list.size());
Iterator<String> iter=maxGroup.list.iterator();
while(iter.hasNext())
System.out.print(iter.next()+" ");
System.out.println();
}
}else{ System.out.println(0); }
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}