Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 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. public int size() {
  47. return depth;
  48. }
  49. public E peek () {
  50. // Go vrakja elementot na vrvot od stekot.
  51. if (depth == 0)
  52. throw new NoSuchElementException();
  53. return elems[depth-1];
  54. }
  55.  
  56.  
  57. public void clear () {
  58. // Go prazni stekot.
  59. for (int i = 0; i < depth; i++) elems[i] = null;
  60. depth = 0;
  61. }
  62.  
  63.  
  64. public void push (E x) {
  65. // Go dodava x na vrvot na stekot.
  66. elems[depth++] = x;
  67. }
  68.  
  69.  
  70. public E pop () {
  71. // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  72. if (depth == 0)
  73. throw new NoSuchElementException();
  74. E topmost = elems[--depth];
  75. elems[depth] = null;
  76. return topmost;
  77. }
  78. }
  79.  
  80. public class StackBukvi {
  81. static int proveri_t_posle_s(char [] St) {
  82. ArrayStack stek=new ArrayStack(St.length);
  83. Integer brS=0;
  84. for(int i=0; i<St.length; i++) {
  85. if(St[i]=='S') {
  86. brS++;
  87. }
  88. }
  89. Integer brojac=0;
  90. Integer temp2=0;
  91. Integer temp=0;
  92. boolean flag=false;
  93. for(int i=0; i<St.length; i++) {
  94. if(flag==false) {
  95. if(St[i]=='S') {
  96. flag=true;
  97. temp2++;
  98. }
  99. } else if(flag==true) {
  100. if(St[i]=='S') {
  101. if(stek.size()!=temp&&temp2!=1&&temp2!=3) {
  102. return 0;
  103. } else {
  104. temp2++;
  105. temp=stek.size();
  106. stek.clear();
  107. }
  108. } else if(St[i]=='T') {
  109. stek.push(St[i]);
  110. }
  111. }
  112. }
  113. if(stek.size()!=0) {
  114. if(stek.size()!=temp) {
  115. return 0;
  116. } else {
  117. return 1;
  118. }
  119. } else if(stek.size()==0) {
  120. if(brS==temp2 && temp2!=stek.size()) {
  121. return 0;
  122. }
  123. }
  124. return 1;
  125. }
  126.  
  127. public static void main(String[] args) throws IOException {
  128. char [] niza=new char[100];
  129.  
  130. Scanner f=new Scanner(System.in);
  131. String st=f.next();
  132. niza=st.toCharArray();
  133.  
  134. int rez= proveri_t_posle_s(niza);
  135. System.out.println(rez);
  136. }
  137.  
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement