Assume that someone dictates you a sequence of numbers and you need to write it down. For brevity, he dictates it as follows: first says the number of consecutive identical numbers and then says the number itself. E.g. The sequence 1 1 3 3 3 2 2 2 2 14 14 14 11 11 11 2 will be dictated as "Two ones, three threes, four twos, three fourteens, three elevens, one two", so you will write down the sequence 2 1 3 3 4 2 3 14 3 11 1 2. The challenge is to write the program which compresses the given sequence using this approach.
Input:
Your program should read lines from standard input. Each line is a sequence of L integers, where each integer is N, separated by a whitespace. N is in range [0, 99]. L is in range [1, 400].
Output:
For each test case, produce a single line of output containing a compressed sequence of numbers separated by a single space char.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.io.File; import java.util.Scanner; import java.util.LinkedHashMap; public class Main { /** * Iterate through each line of input. */ public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(reader); int number = 0; int numberOfParagraph = 0; int numberOfSequence = 1; char ch; HashMap<Integer, Integer> map = new HashMap<>(); String line; while ((line = in.readLine()) != null) { if(line.equals('\n')) { numberOfParagraph++; } if(numberOfParagraph < 1 || numberOfParagraph > 400) { break; } for (int index = 0; index < line.length(); index++) { //number = line.parseInt(line); ch = line.charAt(index); number = ch - '0'; if(number < 0 || number > 99) { continue; } if(!map.containsKey(number)) { map.put(number, 1); } else { numberOfSequence++; map.put(number, numberOfSequence); } } //System.out.println(line); } for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + " ocurrs " + entry.getValue()+ " times"); } } }