SHARE
TWEET

Query Mess

Ivelin_1936 Jun 18th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2. // 100/100 in Judge
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.LinkedHashMap;
  8. import java.util.Map;
  9.  
  10. public class QueryMess {
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  13.  
  14.         String line = reader.readLine();
  15.  
  16.         while (!"end".equalsIgnoreCase(line)) {
  17.             Map<String, ArrayList<String>> keyMap = new LinkedHashMap<>();
  18.  
  19.             line = line.replaceAll(".+\\?", "");
  20.             line = line.replaceAll("\\+|%20", " ");
  21.             line = line.replaceAll("\\s{2,}", " ").trim();
  22.  
  23.             String[] keyValues = line.split("&");
  24.  
  25.             for (int i = 0; i < keyValues.length; i++) {
  26.                 String[] pairs = keyValues[i].split("=");
  27.                 String key = pairs[0].trim();
  28.                 String value = pairs[1].trim();
  29.  
  30.                 if (!keyMap.containsKey(key)) {
  31.                     keyMap.put(key, new ArrayList<>());
  32.                 }
  33.                 keyMap.get(key).add(value);
  34.             }
  35.  
  36.             for (String key : keyMap.keySet()) {
  37.                 System.out.print(key + "=");
  38.                 System.out.print(keyMap.get(key));
  39.             }
  40.             System.out.println();
  41.  
  42.             line = reader.readLine();
  43.         }
  44.     }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top