File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed
Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+ public class Main {
5+ private static int count = 0 ;
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
9+ int N = Integer . parseInt(br. readLine());
10+
11+ String [] arr = new String [N ];
12+ for (int i = 0 ; i < N ; i++ ) {
13+ arr[i] = br. readLine();
14+ }
15+
16+ int a = 0 ;
17+ int b = N - 1 ;
18+ while (a <= b) {
19+ if (arr[a]. compareTo(arr[b]) > 0 ) {
20+ write(bw, arr[b]);
21+ b-- ;
22+ } else if (arr[a]. compareTo(arr[b]) == 0 ) {
23+ int innerA = a;
24+ int innerB = b;
25+ boolean go = false ;
26+ while (innerA < innerB) {
27+ int innerCompare = arr[innerA]. compareTo(arr[innerB]);
28+ if (innerCompare > 0 ) {
29+ write(bw, arr[b]);
30+ b-- ;
31+ go = true ;
32+ break ;
33+ } else if (innerCompare < 0 ) {
34+ write(bw, arr[a]);
35+ a++ ;
36+ go = true ;
37+ break ;
38+ }
39+ innerA++ ;
40+ innerB-- ;
41+ }
42+ if (! go) {
43+ write(bw, arr[a]);
44+ a++ ;
45+ }
46+ } else {
47+ write(bw, arr[a]);
48+ a++ ;
49+ }
50+ }
51+
52+ bw. flush();
53+ bw. close();
54+ }
55+
56+ private static void write (BufferedWriter bw , String target ) throws IOException {
57+ if (count % 80 == 0 ) {
58+ if (count!= 0 ) {
59+ bw. write(" \n " );
60+ }
61+ }
62+ bw. write(target);
63+ count++ ;
64+ }
65+ }
66+ ```
You can’t perform that action at this time.
0 commit comments