Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package stack_parantheses_check;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.NoSuchElementException;
  7.  
  8. interface Stack<E> {
  9.  
  10.     // Elementi na stekot se objekti od proizvolen tip.
  11.  
  12.     // Metodi za pristap:
  13.  
  14.     public boolean isEmpty();
  15.  
  16.     // Vrakja true ako i samo ako stekot e prazen.
  17.  
  18.     public E peek();
  19.  
  20.     // Go vrakja elementot na vrvot od stekot.
  21.  
  22.     // Metodi za transformacija:
  23.  
  24.     public void clear();
  25.  
  26.     // Go prazni stekot.
  27.  
  28.     public void push(E x);
  29.  
  30.     // Go dodava x na vrvot na stekot.
  31.  
  32.     public E pop();
  33.     // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  34. }
  35.  
  36. class ArrayStack<E> implements Stack<E> {
  37.     private E[] elems;
  38.     private int depth;
  39.  
  40.     @SuppressWarnings("unchecked")
  41.     public ArrayStack(int maxDepth) {
  42.         // Konstrukcija na nov, prazen stek.
  43.         elems = (E[]) new Object[maxDepth];
  44.         depth = 0;
  45.     }
  46.  
  47.     public boolean isEmpty() {
  48.         // Vrakja true ako i samo ako stekot e prazen.
  49.         return (depth == 0);
  50.     }
  51.  
  52.     public E peek() {
  53.         // Go vrakja elementot na vrvot od stekot.
  54.         if (depth == 0)
  55.             throw new NoSuchElementException();
  56.         return elems[depth - 1];
  57.     }
  58.  
  59.     public void clear() {
  60.         // Go prazni stekot.
  61.         for (int i = 0; i < depth; i++)
  62.             elems[i] = null;
  63.         depth = 0;
  64.     }
  65.  
  66.     public void push(E x) {
  67.         // Go dodava x na vrvot na stekot.
  68.         elems[depth++] = x;
  69.     }
  70.  
  71.     public E pop() {
  72.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  73.         if (depth == 0)
  74.             throw new NoSuchElementException();
  75.         E topmost = elems[--depth];
  76.         elems[depth] = null;
  77.         return topmost;
  78.     }
  79. }
  80.  
  81. public class Parantheses {
  82.  
  83.     public static boolean isValid(String sequence) {
  84.         ArrayStack<Character> stack = new ArrayStack<Character>(50);
  85.  
  86.         for (int i = 0; i < sequence.length(); i++) {
  87.             if (sequence.charAt(i) == '{') {
  88.                 stack.push(sequence.charAt(i));
  89.             } else if (sequence.charAt(i) == '[') {
  90.                 stack.push(sequence.charAt(i));
  91.             } else if (sequence.charAt(i) == '(') {
  92.                 stack.push(sequence.charAt(i));
  93.  
  94.             } else if (sequence.charAt(i) == ')') {
  95.                 if (stack.isEmpty()) {
  96.                     return false;
  97.                 }
  98.                 if (stack.pop() != '(') {
  99.                     return false;
  100.                 }
  101.  
  102.             } else if (sequence.charAt(i) == ']') {
  103.                 if (stack.isEmpty()) {
  104.                     return false;
  105.                 }
  106.                 if (stack.pop() != '[') {
  107.                     return false;
  108.                 }
  109.             } else if (sequence.charAt(i) == '}') {
  110.                 if (stack.isEmpty()) {
  111.                     return false;
  112.                 }
  113.                 if (stack.pop() != '{') {
  114.                     return false;
  115.                 }
  116.             }
  117.         }
  118.  
  119.         return stack.isEmpty();
  120.     }
  121.  
  122.     public static void main(String[] args) throws IOException {
  123.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  124.                 System.in));
  125.         String sequence = stdin.readLine().trim();
  126.  
  127.         System.out.println(isValid(sequence));
  128.  
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement