SashkoKlincharov

[Java][АПС] - Проверка на спелување

Feb 6th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. import java.util.*;
  2. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  3.  
  4. // Each MapEntry object is a pair consisting of a key (a Comparable
  5. // object) and a value (an arbitrary object).
  6. K key;
  7. E value;
  8.  
  9. public MapEntry (K key, E val) {
  10. this.key = key;
  11. this.value = val;
  12. }
  13.  
  14. public int compareTo (K that) {
  15. // Compare this map entry to that map entry.
  16. @SuppressWarnings("unchecked")
  17. MapEntry<K,E> other = (MapEntry<K,E>) that;
  18. return this.key.compareTo(other.key);
  19. }
  20.  
  21. public String toString () {
  22. return "<" + key + "," + value + ">";
  23. }
  24. }
  25.  
  26. class SLLNode<E> {
  27. protected E element;
  28. protected SLLNode<E> succ;
  29.  
  30. public SLLNode(E elem, SLLNode<E> succ) {
  31. this.element = elem;
  32. this.succ = succ;
  33. }
  34.  
  35. @Override
  36. public String toString() {
  37. return element.toString();
  38. }
  39. }
  40. class CBHT<K extends Comparable<K>, E> {
  41.  
  42. // An object of class CBHT is a closed-bucket hash table, containing
  43. // entries of class MapEntry.
  44. private SLLNode<MapEntry<K,E>>[] buckets;
  45.  
  46. @SuppressWarnings("unchecked")
  47. public CBHT(int m) {
  48. // Construct an empty CBHT with m buckets.
  49. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  50. }
  51.  
  52. private int hash(K key) {
  53. // Translate key to an index of the array buckets.
  54. return Math.abs(key.hashCode()) % buckets.length;
  55. }
  56.  
  57. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  58. // Find which if any node of this CBHT contains an entry whose key is
  59. // equal
  60. // to targetKey. Return a link to that node (or null if there is none).
  61. int b = hash(targetKey);
  62. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  63. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  64. return curr;
  65. }
  66. return null;
  67. }
  68.  
  69. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  70. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  71. int b = hash(key);
  72. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  73. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  74. // Make newEntry replace the existing entry ...
  75. curr.element = newEntry;
  76. return;
  77. }
  78. }
  79. // Insert newEntry at the front of the 1WLL in bucket b ...
  80. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  81. }
  82.  
  83. public void delete(K key) {
  84. // Delete the entry (if any) whose key is equal to key from this CBHT.
  85. int b = hash(key);
  86. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  87. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  88. if (pred == null)
  89. buckets[b] = curr.succ;
  90. else
  91. pred.succ = curr.succ;
  92. return;
  93. }
  94. }
  95. }
  96.  
  97. public String toString() {
  98. String temp = "";
  99. for (int i = 0; i < buckets.length; i++) {
  100. temp += i + ":";
  101. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  102. temp += curr.element.toString() + " ";
  103. }
  104. temp += "\n";
  105. }
  106. return temp;
  107. }
  108.  
  109. }
  110.  
  111. public class Speluvanje {
  112.  
  113. public static void main(String[] args) {
  114. Scanner input = new Scanner(System.in);
  115. int n = input.nextInt();
  116. input.nextLine();
  117.  
  118. CBHT<String,Integer> hashtable = new CBHT<String,Integer>(n*2);
  119. for(int i=0;i<n;i++) {
  120. String name = input.next();
  121. input.nextLine();
  122. hashtable.insert(name, 1);
  123. }
  124.  
  125. int correct = 0;
  126. String [] tekst = input.nextLine().split(" ");
  127. if(tekst.length<=1) {
  128. System.out.println("Bravo");
  129. }
  130. for(int i=0;i<tekst.length;i++) {
  131. tekst[i] = tekst[i].toLowerCase();
  132. if(tekst[i].contains(".")||tekst[i].contains(",")||tekst[i].contains("!")||tekst[i].contains("?")) {
  133. tekst[i] = tekst[i].substring(0,tekst[i].length()-1);
  134. }
  135. SLLNode<MapEntry<String,Integer>> node = hashtable.search(tekst[i]);
  136. if(node!=null) {
  137. //System.out.println(node);
  138. correct++;
  139. }
  140. else {
  141. System.out.println(tekst[i]);
  142. }
  143. if(correct==tekst.length) {
  144. System.out.println("Bravo");
  145. }
  146. }
  147. }
  148.  
  149. }
Add Comment
Please, Sign In to add comment