Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Букви
- Дадена е низа од големи букви, во која буквата S се појавува парен број пати. После секоја буква S буквата Т се појавува еднаш или повеќе пати.Користејќи стек да се одреди дали после секоја буква S (до следната буква S), буквата Т се појавува ист број на пати. На првиот ред од влезот се чита низа од карактери (стринг), на излез се печати 1 доколку буквата Т се појавува ист број на пати после секоја S, и нула доколку овој услов не е исполнет.
- Име на класата: StackBukvi
- Sample input Sample output
- ASGTBST 1
- */
- // CODE
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- interface Stack<E> {
- public boolean isEmpty();
- public E peek();
- public void clear();
- public void push(E x);
- public E pop();
- }
- class ArrayStack<E> implements Stack<E> {
- private E[] elems;
- private int depth;
- @SuppressWarnings("unchecked")
- public ArrayStack(int maxDepth) {
- elems = (E[]) new Object[maxDepth];
- depth = 0;
- }
- public boolean isEmpty() {
- return (depth == 0);
- }
- public E peek() {
- if (depth == 0)
- throw new NoSuchElementException();
- return elems[depth - 1];
- }
- public void clear() {
- for (int i = 0; i < depth; i++)
- elems[i] = null;
- depth = 0;
- }
- public void push(E x) {
- elems[depth++] = x;
- }
- public int getDepth() {
- return depth;
- }
- public E pop() {
- if (depth == 0)
- throw new NoSuchElementException();
- E topmost = elems[--depth];
- elems[depth] = null;
- return topmost;
- }
- }
- public class StackBukvi {
- static int proveri_t_posle_s(char[] st) {
- ArrayStack<Character> stack = new ArrayStack<>(st.length);
- int count = 0;
- for (int i = 0; i < st.length; i++) {
- if (st[i] == 'S') {
- stack.push(Character.toUpperCase(st[i]));
- count++;
- } else if (st[i] == 'T'&&!stack.isEmpty())
- stack.push(Character.toUpperCase(st[i]));
- }
- if (!check(stack, count))
- return 0;
- return 1;
- }
- private static boolean check(ArrayStack<Character> stack, int count) {
- int sum = 0, sumTotal = 0;
- boolean flag = true;
- while (!stack.isEmpty()) {
- char value = stack.pop();
- if (value == 'T')
- sum++;
- else if (value == 'S') {
- if(flag){
- sumTotal = sum;
- flag = false;
- sum = 0;
- } else if(sumTotal != sum)
- return false;
- sum = 0;
- }
- }
- return true;
- }
- static final private Scanner input = new Scanner(System.in);
- public static void main(String[] args) {
- char[] niza = new char[100];
- String st = input.nextLine();
- niza = st.toCharArray();
- System.out.println(proveri_t_posle_s(niza));
- input.close();
- }
- }
Add Comment
Please, Sign In to add comment