Advertisement
coder0687

Best Enemies

Jun 27th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import static java.lang.System.*;
  4. import static java.lang.Math.*;
  5. class Code
  6. {
  7.    
  8.     void run() {
  9.         int tc=ni();
  10.         StringBuilder sb=new StringBuilder();
  11.         while(tc-->0) {
  12.             int n=ni();
  13.             String s=ns(); ni();
  14.             int fr[] = new int[10];
  15.             for(int i=0;i<n;i++) fr[s.charAt(i)-'0']++;
  16.             int j=0;
  17.             xD:
  18.             for(int i=9;i>0;i++) {
  19.                 while(fr[i]>0) {
  20.                     int x=s.charAt(j)-'0';
  21.                     if(x==i) fr[i]--;
  22.                     else {
  23.                         sb.append(i); j++;
  24.                         while(j<n) {
  25.                             if(s.charAt(j)-'0'==x) { sb.append(x); x=-1; }
  26.                             else sb.append(s.charAt(j));
  27.                             j++;
  28.                         }
  29.                         break xD;
  30.                     }
  31.                     sb.append(x);
  32.                     j++;
  33.                 }
  34.             }
  35.             sb.append("\n");
  36.         }
  37.         out.println(sb);
  38.         out.flush();
  39.         out.close();
  40.     }
  41.    
  42.     public static void main(String[] args)throws IOException {
  43.         try {
  44.             new Code().run();
  45.         } catch(Exception e) {}
  46.     }
  47.    
  48.     FastReader sc = new FastReader();
  49.     PrintWriter out = new PrintWriter(System.out);
  50.    
  51.     String ns() { return sc.next(); }
  52.     int ni() { return sc.nextInt(); }
  53.     long nl() { return sc.nextLong(); }
  54.     int[] ni(int n) {
  55.         int a[]=new int[n];
  56.         for(int i=0;i<n;a[i++]=ni());
  57.         return a;
  58.     }
  59.     long[] nl(int n) {
  60.         long a[]=new long[n];
  61.         for(int i=0;i<n;a[i++]=nl());
  62.         return a;
  63.     }
  64.    
  65.     int[][] ni(int n,int m) {
  66.         int a[][]=new int[n][m];
  67.         for(int i=0;i<n;i++)
  68.             for(int j=0;j<m;j++)
  69.                 a[i][j]=ni();
  70.         return a;
  71.     }
  72.     long[][] nl(int n,int m) {
  73.         long a[][]=new long[n][m];
  74.         for(int i=0;i<n;i++)
  75.             for(int j=0;j<m;j++)
  76.                 a[i][j]=nl();
  77.         return a;
  78.     }
  79.     int gcd(int a, int b) {
  80.         return b==0?a:gcd(b,a%b);
  81.     }
  82.     static class FastReader {
  83.         private InputStream stream;
  84.         private byte[] buf = new byte[1024];
  85.         private int curChar;
  86.         private int numChars;
  87.         private FastReader.SpaceCharFilter filter;
  88.         FastReader(){ this(System.in); }
  89.         public FastReader(InputStream stream) {
  90.             this.stream = stream;
  91.         }
  92.  
  93.         public int read() {
  94.             if (numChars == -1) throw new InputMismatchException();
  95.             if (curChar >= numChars) {
  96.                 curChar = 0;
  97.                 try {
  98.                     numChars = stream.read(buf);
  99.                 } catch (IOException e) {
  100.                     throw new InputMismatchException();
  101.                 }
  102.                 if (numChars <= 0) return -1;
  103.             }
  104.             return buf[curChar++];
  105.         }
  106.  
  107.         public int nextInt() {
  108.             int c = read();
  109.             while (isSpaceChar(c)) c = read();
  110.             int sgn = 1;
  111.             if (c == '-') {
  112.                 sgn = -1;
  113.                 c = read();
  114.             }
  115.             int res = 0;
  116.             do {
  117.                 if (c < '0' || c > '9') throw new InputMismatchException();
  118.                 res *= 10;
  119.                 res += c - '0';
  120.                 c = read();
  121.             }
  122.             while (!isSpaceChar(c));
  123.             return res * sgn;
  124.         }
  125.        
  126.         public long nextLong() {
  127.             int c = read();
  128.             while (isSpaceChar(c)) c = read();
  129.             int sgn = 1;
  130.             if (c == '-') {
  131.                 sgn = -1;
  132.                 c = read();
  133.             }
  134.             long res = 0;
  135.             do {
  136.                 if (c < '0' || c > '9') throw new InputMismatchException();
  137.                 res = res*1L*10;
  138.                 res += c - '0';
  139.                 c = read();
  140.             }
  141.             while (!isSpaceChar(c));
  142.             return res *1L* sgn;
  143.         }
  144.        
  145.         public String next() {
  146.             int c = read();
  147.             while (isSpaceChar(c)) c = read();
  148.             StringBuilder res = new StringBuilder();
  149.             do {
  150.                 res.appendCodePoint(c);
  151.                 c = read();
  152.             } while (!isSpaceChar(c));
  153.             return res.toString();
  154.         }
  155.  
  156.         public boolean isSpaceChar(int c) {
  157.             if (filter != null) return filter.isSpaceChar(c);
  158.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  159.         }
  160.  
  161.         public interface SpaceCharFilter {
  162.             public boolean isSpaceChar(int ch);
  163.  
  164.         }
  165.  
  166.     }
  167.    
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement