Advertisement
since85stas

Untitled

May 25th, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class StringInsert {
  5.  
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
  8.         StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
  9.         StringBuilder s = new StringBuilder(tokenizer.nextToken());
  10.  
  11.         tokenizer = new StringTokenizer(reader.readLine());
  12.         int n = Integer.parseInt(tokenizer.nextToken());
  13.  
  14.         TreeSet<Insert> inserts = new TreeSet<>();
  15.  
  16.         for (int i = 0; i < n; i++) {
  17.             tokenizer = new StringTokenizer(reader.readLine());
  18.             String str = tokenizer.nextToken();
  19.             int pos = Integer.parseInt(tokenizer.nextToken());
  20.             inserts.add(new Insert(str, pos));
  21.         }
  22.    
  23.         int offs = 0;
  24.         for (int i = 0; i < n; i++) {
  25.             Insert insert = inserts.pollFirst();
  26.             s.insert(insert.start + offs, insert.str);
  27.             offs += insert.str.length;
  28.         }
  29.  
  30.         System.out.println(s.toString());
  31.     }
  32.  
  33.     private static class Insert implements Comparable<Insert>{
  34.         char[] str;
  35.         int start;
  36.  
  37.         public Insert(String str, int start) {
  38.             this.start = start;
  39.             this.str = str.toCharArray();
  40.         }
  41.  
  42.         @Override
  43.         public int compareTo(Insert insert) {
  44.             return Integer.compare( start, insert.start);
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement