ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1220. Stacks

Java solution
Posted by Mortrus 31 Oct 2016 23:22
Didn't notice that problem is not for Java, so I solved it. Maybe someone will need the solution. Solution without checking of stacks for emptiness.


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;

public class HelloWorld {

    public static void main (String[] args)     {
        HashMap<Integer,  ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
        Scanner reader = new Scanner(System.in);
        int n = reader.nextInt();
        reader.nextLine();
        for (int i = 0; i < n; i++) {
            String command = reader.nextLine();
            StringTokenizer tokenizer = new StringTokenizer(command);
            command = tokenizer.nextToken();
            if ("PUSH".equals(command)) {
                int key = Integer.parseInt(tokenizer.nextToken());
                int value = Integer.parseInt(tokenizer.nextToken());
                if (map.containsKey(key)) {
                    map.get(key).add(0, value);
                }
                else {
                    map.put(key, new ArrayList<Integer>());
                    map.get(key).add(0, value);
                }
            }
            else if ("POP".equals(command)) {
                int key = Integer.parseInt(tokenizer.nextToken());
                if (map.containsKey(key)) {
                    System.out.println(map.get(key).get(0));
                    map.get(key).remove(0);
                }
            }

        }
    }
}

Edited by author 31.10.2016 23:24

Edited by author 31.10.2016 23:26