Advertisement
Guest User

P03_Ascent

a guest
Jun 7th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class P03_Ascent {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         LinkedHashMap<String, String> foundPatterns = new LinkedHashMap<String, String>();
  14.         String inputLine = "";
  15.         Pattern cypherPattern = Pattern.compile("(\\,|\\_)([a-zA-Z]+)(\\d)");
  16.  
  17.         String line = reader.readLine();
  18.  
  19.         while (!"Ascend".equalsIgnoreCase(line)){
  20.  
  21.             for (Map.Entry<String, String> stringStringEntry : foundPatterns.entrySet()) {
  22.                 line = line.replaceAll(stringStringEntry.getKey(), stringStringEntry.getValue());
  23.             }
  24.  
  25.             Matcher matcher = cypherPattern.matcher(line);
  26.  
  27.             while (matcher.find()){
  28.              String decodedLine = decode(matcher.group(1),
  29.                      matcher.group(2), Integer.parseInt(matcher.group(3)));
  30.  
  31.              line = line.replaceAll(matcher.group(0), decodedLine);
  32.              foundPatterns.put(matcher.group(0), decodedLine);
  33.             }
  34.  
  35.             System.out.println(line);
  36.             line = reader.readLine();
  37.         }
  38.     }
  39.  
  40.     private static String decode(String sign, String encodedString, int digit) {
  41.         String decodedString = "";
  42.  
  43.         for (int i = 0; i < encodedString.length(); i++) {
  44.             char resultChar = '\0';
  45.  
  46.             if(sign.equalsIgnoreCase(",")){
  47.                 resultChar = (char)(encodedString.charAt(i) + (char)digit);
  48.             }else{
  49.                 resultChar = (char) (encodedString.charAt(i) - (char)digit);
  50.             }
  51.  
  52.             decodedString = decodedString + resultChar;
  53.         }
  54.  
  55.         return decodedString;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement