import java.util.*;class Main{public static void main(String[]g)throws Exception{byte[]u=new byte[32767];String x,y,c="";Object[]w;StringBuffer b=new StringBuffer();Map h=new HashMap();int d,n=0;g=new String(u,0,System.in.read(u)).split("\n");while(g.length>++n){x=g[n];y=b.replace(0,x.length(),x).reverse().toString();d=x.compareTo(y);if(d==0)c+=x;else if(h.get(x)!=null)h.put(x,d<0?x:y);else h.put(y,"");}b.delete(0,b.length());for(Object z:new TreeSet(h.values()))b.append(z);System.out.print((b+c)+b.reverse().toString());}}

@@ -128,42 +128,8 @@
 %5B%5Dw;
-StringBuffer b=new StringBuffer();
 Map 
@@ -235,31 +235,25 @@
 %5D;y=
-b.replace(0,x.length(),
+new StringBuffer(
 x).r
@@ -364,30 +364,12 @@
 %22);%7D
-b.delete(0,b.length())
+x=%22%22
 ;for
@@ -406,19 +406,12 @@
 ()))
-b.append(z)
+x+=z
 ;Sys
@@ -429,14 +429,33 @@
 nt((
-b
+x
 +c)+
-b
+(new StringBuffer(x)
 .rev
@@ -472,12 +472,13 @@
 tring())
+)
 ;%7D%7D%0A

Update test5.java