Advertisement
Mitko_jos

asp so nure1

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