ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
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();
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)) {
}
else {
map.put(key, new ArrayList<Integer>());
}
}
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