SlavkovB

Букви

Aug 16th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. /**Букви
  2. Дадена е низа од големи букви, во која буквата S се појавува парен број пати. После секоја буква S буквата Т се појавува еднаш или повеќе пати.Користејќи стек да се одреди дали после секоја буква S (до следната буква S), буквата Т се појавува ист број на пати. На првиот ред од влезот се чита низа од карактери (стринг), на излез се печати 1 доколку буквата Т се појавува ист број на пати после секоја S, и нула доколку овој услов не е исполнет.
  3.  
  4. Име на класата: StackBukvi
  5. Sample input                    Sample output
  6. ASGTBST                         1
  7. */
  8.  
  9.  
  10. //  CODE
  11.  
  12.  
  13. import java.util.NoSuchElementException;
  14. import java.util.Scanner;
  15.  
  16. interface Stack<E> {
  17.     public boolean isEmpty();
  18.     public E peek();
  19.     public void clear();
  20.     public void push(E x);
  21.     public E pop();
  22. }
  23.  
  24. class ArrayStack<E> implements Stack<E> {
  25.     private E[] elems;
  26.     private int depth;
  27.  
  28.     @SuppressWarnings("unchecked")
  29.     public ArrayStack(int maxDepth) {
  30.         elems = (E[]) new Object[maxDepth];
  31.         depth = 0;
  32.     }
  33.  
  34.     public boolean isEmpty() {
  35.         return (depth == 0);
  36.     }
  37.  
  38.     public E peek() {
  39.         if (depth == 0)
  40.             throw new NoSuchElementException();
  41.         return elems[depth - 1];
  42.     }
  43.  
  44.     public void clear() {
  45.         for (int i = 0; i < depth; i++)
  46.             elems[i] = null;
  47.         depth = 0;
  48.     }
  49.  
  50.     public void push(E x) {
  51.         elems[depth++] = x;
  52.     }
  53.  
  54.     public int getDepth() {
  55.         return depth;
  56.     }
  57.  
  58.     public E pop() {
  59.         if (depth == 0)
  60.             throw new NoSuchElementException();
  61.         E topmost = elems[--depth];
  62.         elems[depth] = null;
  63.         return topmost;
  64.     }
  65. }
  66.  
  67. public class StackBukvi {
  68.  
  69.     static int proveri_t_posle_s(char[] st) {
  70.  
  71.         ArrayStack<Character> stack = new ArrayStack<>(st.length);
  72.         int count = 0;
  73.  
  74.         for (int i = 0; i < st.length; i++) {
  75.             if (st[i] == 'S') {
  76.                 stack.push(Character.toUpperCase(st[i]));
  77.                 count++;
  78.             } else if (st[i] == 'T'&&!stack.isEmpty())
  79.                 stack.push(Character.toUpperCase(st[i]));
  80.         }
  81.  
  82.         if (!check(stack, count))
  83.             return 0;
  84.         return 1;
  85.     }
  86.  
  87.     private static boolean check(ArrayStack<Character> stack, int count) {
  88.         int sum = 0, sumTotal = 0;
  89.         boolean flag = true;
  90.        
  91.         while (!stack.isEmpty()) {
  92.             char value = stack.pop();
  93.            
  94.             if (value == 'T')
  95.                 sum++;
  96.             else if (value == 'S') {
  97.                 if(flag){
  98.                     sumTotal = sum;
  99.                     flag = false;
  100.                     sum = 0;
  101.                 } else if(sumTotal != sum)
  102.                     return false;
  103.                   sum = 0;
  104.             }
  105.         }
  106.  
  107.         return true;
  108.     }
  109.  
  110.     static final private Scanner input = new Scanner(System.in);
  111.  
  112.     public static void main(String[] args) {
  113.         char[] niza = new char[100];
  114.  
  115.         String st = input.nextLine();
  116.         niza = st.toCharArray();
  117.  
  118.         System.out.println(proveri_t_posle_s(niza));
  119.        
  120.         input.close();
  121.     }
  122.  
  123. }
Add Comment
Please, Sign In to add comment