Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.Scanner;
- interface Stack<E> {
- // Elementi na stekot se objekti od proizvolen tip.
- // Metodi za pristap:
- public boolean isEmpty ();
- // Vrakja true ako i samo ako stekot e prazen.
- public E peek () throws NoSuchElementException ;
- // Go vrakja elementot na vrvot od stekot.
- // Metodi za transformacija:
- public void clear ();
- // Go prazni stekot.
- public void push (E x) ;
- // Go dodava x na vrvot na stekot.
- public E pop () throws NoSuchElementException ;
- // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
- }
- class ArrayStack<E> implements Stack<E> {
- private E[] elems;
- private int depth;
- @SuppressWarnings("unchecked")
- public ArrayStack (int maxDepth) {
- // Konstrukcija na nov, prazen stek.
- elems = (E[]) new Object[maxDepth];
- depth = 0;
- }
- public boolean isEmpty () {
- // Vrakja true ako i samo ako stekot e prazen.
- return (depth == 0);
- }
- public E peek () throws NoSuchElementException {
- // Go vrakja elementot na vrvot od stekot.
- if (depth == 0)
- throw new NoSuchElementException();
- return elems[depth-1];
- }
- public void clear () {
- // Go prazni stekot.
- for (int i = 0; i < depth; i++) elems[i] = null;
- depth = 0;
- }
- public void push (E x) {
- // Go dodava x na vrvot na stekot.
- elems[depth++] = x;
- }
- public E pop () throws NoSuchElementException {
- // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
- if (depth == 0)
- throw new NoSuchElementException();
- E topmost = elems[--depth];
- elems[depth] = null;
- return topmost;
- }
- public int getDepth() {
- return this.depth;
- }
- }
- public class StackBukvi {
- static int proveri_t_posle_s(char [] St)
- {
- int br = 0;
- ArrayStack<Character> trska = new ArrayStack<Character>(St.length);
- for(int i = 0; i < St.length; i++){
- if(St[i]=='S')
- br++;
- if(br%2!=0&&St[i]=='T')
- trska.push(St[i]);
- else if(br%2==0&&St[i]=='T')
- if(!trska.isEmpty())
- try{
- trska.pop();
- }
- catch(Exception e){}
- }
- if(trska.isEmpty())
- return 1;
- else
- return 0;
- }
- public static void main(String[] args) throws IOException {
- char [] niza=new char[100];
- Scanner f=new Scanner(System.in);
- String st=f.next();
- niza=st.toCharArray();
- int rez= proveri_t_posle_s(niza);
- System.out.print(rez);
- }
- }
- class NoSuchElementException extends Exception {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement