Advertisement
Guest User

Untitled

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