Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.util.Scanner;
- class Map<K extends Comparable<K>,E> {
- public K key;
- public E value;
- public Map (K key, E val) {
- this.key = key;
- this.value = val;
- }
- }
- class SLLNode<E> {
- protected E info;
- protected SLLNode<E> next;
- public SLLNode(E info, SLLNode<E> next) {
- this.info = info;
- this.next = next;
- }
- }
- class SLLHT<K extends Comparable<K>, E> {
- private SLLNode<Map<K,E>>[] htable;
- public SLLHT(int m) {
- /*kreiranje na prazna hesh tabela*/
- htable = (SLLNode<Map<K,E>>[]) new SLLNode[m];
- for(int i=0;i<m;i++)
- htable[i]=null;
- }
- private int hash(K key) {
- // Ednostavna verzija, neefikasna!
- return (key.toString().charAt(0)-'a') % htable.length;
- }
- public SLLNode<Map<K,E>> find(K look) {
- int h = hash(look);
- for (SLLNode<Map<K,E>> node = htable[h]; node != null; node = node.next) {
- if (look.equals(node.info.key))
- return node;
- }
- /*ako vo tabelata ne se pronajde kluch ist so preneseniot
- * se vrakja null vrednost*/
- return null;
- }
- public void insert(K key, E val) {
- /* vo tabelata se vmetnuva jazol, chie info pole
- * go sodrzhi parot (key,val) */
- Map<K, E> entry = new Map(key, val);
- int h = hash(key);
- for (SLLNode<Map<K,E>> node = htable[h]; node != null; node = node.next) {
- if (key.equals(node.info.key)) {
- node.info = entry;
- /*ako se pronajde jazol so ist kluch,
- * se resetira vrednosta*/
- return;
- }
- }
- /* ako ne se pronajde jazolot vo soodvetnata kofichka
- * se dodava na pochetok vo nejzinata povrzana lista */
- htable[h] = new SLLNode<Map<K,E>>(entry, htable[h]);
- }
- public void delete(K key) {
- int h = hash(key);
- for (SLLNode<Map<K,E>> pred = null, node = htable[h]; node != null; pred = node, node = node.next) {
- if (key.equals(node.info.key)) {
- /*ako se najde zapisot i e na pochetok na listata*/
- if (pred == null)
- htable[h] = node.next;
- else
- /*ako se najde zapisot i ne e na pochetok na listata*/
- pred.next = node.next;
- return;
- }
- }
- }
- }
- public class SLLHashTables {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int N = input.nextInt();
- SLLHT<String, String> mapa = new SLLHT(2*N);
- for(int i = 0 ; i < N ; ++i)
- mapa.insert(input.next().toLowerCase(), "");
- input.nextLine();
- String[] recenica = input.nextLine().split(" ");
- for(int i = 0 ; i < recenica.length ; ++i)
- {
- if(!Character.isAlphabetic(recenica[i].charAt(recenica[i].length() - 1)))
- recenica[i] = recenica[i].substring(0, recenica[i].length()-1);
- SLLNode<Map<String, String>> entry = mapa.find(recenica[i].toLowerCase());
- for(;;)
- {
- if(entry == null)
- {
- System.out.println(recenica[i]);
- break;
- }
- else if(entry.info.key.equals(recenica[i].toLowerCase()))
- break;
- else
- entry = entry.next;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement