![:)](./images/smilies/icon_smile.gif)
Code: Select all
Thanks, brianfry. :)
Moderator: Board moderators
Code: Select all
Thanks, brianfry. :)
Code: Select all
3
1 1
2 1
2 1
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class I_Can_Guess_the_Data_Structure {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean is_queue, is_stack, is_p_queue;
Queue<Integer> queue = new LinkedList<Integer>();
PriorityQueue<Integer> priority = new PriorityQueue<Integer>();
Stack<Integer> stack = new Stack<Integer>();
StringBuilder out = new StringBuilder();
int n = 0;
do {
try{
n = Integer.parseInt(in.readLine());
} catch (IOException | NumberFormatException e) {
return;
}
is_queue = is_stack = is_p_queue = true;
stack.clear();
queue.clear();
priority.clear();
for (int i = 0; i < n; i++) {
int com = sc.nextInt();
int val = sc.nextInt();
if (com == 1) {
stack.push(val);
queue.add(val);
priority.add(-val);
} else {
if (is_stack) {
if (stack.isEmpty()) is_stack = false;
else {
if (stack.pop() != val) is_stack = false;
}
}
if (is_queue) {
if (queue.isEmpty()) is_queue = false;
else {
if (queue.poll() != val) is_queue = false;
}
}
if (is_p_queue) {
if (priority.isEmpty()) is_p_queue = false;
else {
if (-1 * priority.poll() != val) is_p_queue = false;
}
}
}
}
if (!is_queue && !is_stack && !is_p_queue)
out.append("impossible\n");
else if (is_queue && !(is_stack || is_p_queue))
out.append("queue\n");
else if (is_stack && !(is_queue || is_p_queue))
out.append("stack\n");
else if (is_p_queue && !(is_stack || is_queue))
out.append("priority queue\n");
else
out.append("not sure\n");
} while (n > -1);
System.out.print(out);
}
}
Code: Select all
Removed After AC
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Queue<Integer> v1 = new LinkedList<Integer>(); // N Dentro
Queue<Integer> v2 = new LinkedList<Integer>(); // N Fuera
Queue<Integer> v3 = new LinkedList<Integer>(); // N Luego de llenar los otros
StringBuilder sb = new StringBuilder();
int i=0;
String line;
Set<Integer> t1 = new HashSet<Integer>(); //Conjunto dentro
Set<Integer> t2= new HashSet <Integer>();//Conjunto fuera
Set<Integer> result;
while ((line=bf.readLine())!=null&& !line.trim().equals("")){
i=Integer.parseInt(line);
String[] lista= new String [2];
Stack<Integer> s = new Stack<Integer>(); // Primero en entrar, ultimo en salir
Queue<Integer> q = new LinkedList<Integer>(); // Primero en entrar, primero en salir
Queue<Integer> pp = new PriorityQueue<Integer>(i, Collections.reverseOrder()); //Sale el mayor
int [] lista1=new int[2];
int[] lista2=new int[i]; // Dentro o Fuera
for (int j = 0; j < i; j++) {
line=bf.readLine();
lista=line.split(" ");
lista1[0]=Integer.parseInt(lista[0]);
lista1[1]=Integer.parseInt(lista[1]);
lista2[j]=Integer.parseInt(lista[0]);
if (lista1[0]==1){ //Llenado dentro
v1.add(lista1[1]);
t1.add(lista1[1]);
}
if (lista1[0]==2){ //Llenado fuera
v2.add(lista1[1]);
t2.add(lista1[1]);
}
}
int z1=0;
int z2=0;
int z3=0;
boolean t=true; // stack
boolean f=true; //queue
boolean w=true; // pp
for (int j= 0; j< lista2.length; j++) {
if(lista2[j]==1 & v1.size()>0){
s.push(v1.peek());
q.add(v1.peek());
pp.add(v1.peek());
v3.add(v1.peek());
v1.poll();
}
if(lista2[j]==2){
z1=s.pop();
z2=q.poll();
z3=pp.poll();
if (z1!=v2.peek()){
t=false;
}
if (z2!=v2.peek()){
f=false;
}
if (z3!=v2.peek()){
w=false;
}
v2.poll();
}
}
result = new HashSet<Integer>();
result.addAll(t1);
result.retainAll(t2); // t1 INTERSECCION t2
if (result.size()==0) {
sb.append("impossible" +"\n");
}
else if (t==true & f==false & w==false){
sb.append("stack"+"\n");
}
else if (t==false & f==true & w==false){
sb.append("queue"+"\n");
}
else if (t==false & f==false & w==true){
sb.append("priority queue"+"\n");
//Dobles true
}
else if (t==true & f==true & w==false){
sb.append("not sure"+"\n");
}
else if (t==true & f==false & w==true){
sb.append("not sure"+"\n");
}
else if (t==true & f==true & w==true){
sb.append("not sure"+"\n");
}
t1.clear();
t2.clear();
result.clear();
}
System.out.println(sb);
}
}
Code: Select all
if (i==1){
System.out.println("not sure");
break;
Post your updated code.brianfry713 wrote:Try using BufferedWriter
Code: Select all
if (i==1){
if (Integer.parseInt(lista[0])==1){
System.out.println("not sure");
break;
}
else{
System.out.println("impossible");
break;
}
}
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class pptesting {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Queue<Integer> v1 = new LinkedList<Integer>(); // N Dentro
Queue<Integer> v2 = new LinkedList<Integer>(); // N Fuera
Queue<Integer> v3 = new LinkedList<Integer>(); // N Luego de llenar los otros
StringBuilder sb = new StringBuilder();
int i=0;
String line;
Set<Integer> t1 = new HashSet<Integer>(); //Conjunto dentro
Set<Integer> t2= new HashSet <Integer>();//Conjunto fuera
Set<Integer> result;
while ((line=bf.readLine())!=null&& !line.trim().equals("")){
i=Integer.parseInt(line);
String[] lista= new String [2];
Stack<Integer> s = new Stack<Integer>(); // Primero en entrar, ultimo en salir
Queue<Integer> q = new LinkedList<Integer>(); // Primero en entrar, primero en salir
Queue<Integer> pp = new PriorityQueue<Integer>(i, Collections.reverseOrder()); //Sale el mayor
int [] lista1=new int[2];
int[] lista2=new int[i]; // Dentro o Fuera
for (int j = 0; j < i; j++) {
line=bf.readLine();
lista=line.split(" ");
lista1[0]=Integer.parseInt(lista[0]);
lista1[1]=Integer.parseInt(lista[1]);
lista2[j]=Integer.parseInt(lista[0]);
System.out.println(lista[0]);
if (i==1){
if (Integer.parseInt(lista[0])==1){
System.out.println("not sure");
break;
}
else{
System.out.println("impossible");
break;
}
}
if (lista1[0]==1){ //Llenado dentro
v1.add(lista1[1]);
t1.add(lista1[1]);
}
if (lista1[0]==2){ //Llenado fuera
v2.add(lista1[1]);
t2.add(lista1[1]);
}
}
int z1=0;
int z2=0;
int z3=0;
boolean t=true; // stack
boolean f=true; //queue
boolean w=true; // pp
for (int j= 0; j< lista2.length; j++) {
if(lista2[j]==1 & v1.size()>0){
s.push(v1.peek());
q.add(v1.peek());
pp.add(v1.peek());
v3.add(v1.peek());
v1.poll();
}
if(lista2[j]==2){
z1=s.pop();
z2=q.poll();
z3=pp.poll();
if (z1!=v2.peek()){
t=false;
}
if (z2!=v2.peek()){
f=false;
}
if (z3!=v2.peek()){
w=false;
}
v2.poll();
}
}
result = new HashSet<Integer>();
result.addAll(t1);
result.retainAll(t2); // t1 INTERSECCION t2
if (result.size()==0) {
sb.append("impossible" +"\n");
}
else if (t==true & f==false & w==false){
sb.append("stack"+"\n");
}
else if (t==false & f==true & w==false){
sb.append("queue"+"\n");
}
else if (t==false & f==false & w==true){
sb.append("priority queue"+"\n");
//Dobles true
}
else if (t==true & f==true & w==false){
sb.append("not sure"+"\n");
}
else if (t==true & f==false & w==true){
sb.append("not sure"+"\n");
}
else if (t==true & f==true & w==true){
sb.append("not sure"+"\n");
}
t1.clear();
t2.clear();
result.clear();
}
System.out.println(sb);
}
}
Sorry, i just forgot to change the class here, but i tried with class Main and i got WA. Do you see any mistake on my code besides that class thing?. Thanksbrianfry713 wrote:use class Main
brianfry713 wrote:Post the code you'd submit. On the sample input you're printing 1's and 2's.
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Queue<Integer> v1 = new LinkedList<Integer>(); // N Dentro
Queue<Integer> v2 = new LinkedList<Integer>(); // N Fuera
Queue<Integer> v3 = new LinkedList<Integer>(); // N Luego de llenar los otros
StringBuilder sb = new StringBuilder();
int i=0;
String line;
Set<Integer> t1 = new HashSet<Integer>(); //Conjunto dentro
Set<Integer> t2= new HashSet <Integer>();//Conjunto fuera
Set<Integer> result;
while ((line=bf.readLine())!=null&& !line.trim().equals("")){
i=Integer.parseInt(line);
String[] lista= new String [2];
Stack<Integer> s = new Stack<Integer>(); // Primero en entrar, ultimo en salir
Queue<Integer> q = new LinkedList<Integer>(); // Primero en entrar, primero en salir
Queue<Integer> pp = new PriorityQueue<Integer>(i, Collections.reverseOrder()); //Sale el mayor
int [] lista1=new int[2];
int[] lista2=new int[i]; // Dentro o Fuera
for (int j = 0; j < i; j++) {
line=bf.readLine();
lista=line.split(" ");
lista1[0]=Integer.parseInt(lista[0]);
lista1[1]=Integer.parseInt(lista[1]);
lista2[j]=Integer.parseInt(lista[0]);
if (i==1){
if (Integer.parseInt(lista[0])==1){
sb.append("not sure"+"\n");
break;
}
else{
sb.append("impossible"+"\n");
break;
}
}
if (lista1[0]==1){ //Llenado dentro
v1.add(lista1[1]);
t1.add(lista1[1]);
}
if (lista1[0]==2){ //Llenado fuera
v2.add(lista1[1]);
t2.add(lista1[1]);
}
}
int z1=0;
int z2=0;
int z3=0;
boolean t=true; // stack
boolean f=true; //queue
boolean w=true; // pp
for (int j= 0; j< lista2.length; j++) {
if(lista2[j]==1 & v1.size()>0){
s.push(v1.peek());
q.add(v1.peek());
pp.add(v1.peek());
v3.add(v1.peek());
v1.poll();
}
if(lista2[j]==2){
z1=s.pop();
z2=q.poll();
z3=pp.poll();
if (z1!=v2.peek()){
t=false;
}
if (z2!=v2.peek()){
f=false;
}
if (z3!=v2.peek()){
w=false;
}
v2.poll();
}
}
result = new HashSet<Integer>();
result.addAll(t1);
result.retainAll(t2); // t1 INTERSECCION t2
if (result.size()==0) {
sb.append("impossible" +"\n");
}
else if (t==true & f==false & w==false){
sb.append("stack"+"\n");
}
else if (t==false & f==true & w==false){
sb.append("queue"+"\n");
}
else if (t==false & f==false & w==true){
sb.append("priority queue"+"\n");
//Dobles true
}
else if (t==true & f==true & w==false){
sb.append("not sure"+"\n");
}
else if (t==true & f==false & w==true){
sb.append("not sure"+"\n");
}
else if (t==true & f==true & w==true){
sb.append("not sure"+"\n");
}
t1.clear();
t2.clear();
result.clear();
}
System.out.println(sb);
}
}