Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class SLLNode<E>{
- private E info;
- private SLLNode<E> link;
- public SLLNode(E info, SLLNode<E> link)
- {
- this.info=info;
- this.link=link;
- }
- public E getInfo()
- {
- return info;
- }
- public SLLNode<E> getLink()
- {
- return link;
- }
- public void setInfo(E info)
- {
- this.info=info;
- }
- public void setLink(SLLNode<E> link)
- {
- this.link=link;
- }
- }
- class MapEntry<K extends Comparable<K>, E> implements Comparable<K>{
- private K key;
- private E value;
- public MapEntry(K key, E value)
- {
- this.key=key;
- this.value=value;
- }
- public String toString()
- {
- return String.format("<%s, %s>", key, value);
- }
- @Override
- public int compareTo(K k) {
- MapEntry<K, E> me=(MapEntry<K, E>) k;
- return this.key.compareTo(me.key);
- }
- public K getKey()
- {
- return key;
- }
- public E getValue()
- {
- return value;
- }
- }
- class CBHT<K extends Comparable<K>, E>{
- private SLLNode<MapEntry<K, E>> []buckets;
- public CBHT(int m)
- {
- buckets=(SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
- }
- private int hash(K key)
- {
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K, E>> search(K targetKey)
- {
- int b=this.hash(targetKey);
- for(SLLNode<MapEntry<K, E>> curr=buckets[b]; curr!=null; curr=curr.getLink())
- if(targetKey.equals(((MapEntry<K, E>) curr.getInfo()).getKey()))
- return curr;
- return null;
- }
- public void insert(K key, E value)
- {
- MapEntry<K, E> ins=new MapEntry(key, value);
- int b=hash(key);
- for(SLLNode<MapEntry<K, E>> curr=buckets[b]; curr!=null; curr=curr.getLink())
- {
- if (key.equals(((MapEntry<K, E>) curr.getInfo()).getKey()))
- {
- curr.setInfo(ins);
- return;
- }
- }
- buckets[b]=new SLLNode<MapEntry<K,E>>(ins, buckets[b]);
- }
- public void delete(K key)
- {
- int b=hash(key);
- for(SLLNode<MapEntry<K, E>> curr=buckets[b], pred=null; curr!=null; pred=curr, curr=curr.getLink())
- if(key.equals(((MapEntry<K, E>) curr.getInfo()).getKey()))
- {
- if(pred==null)
- buckets[b]=curr.getLink();
- else
- pred.setLink(curr.getLink());
- return;
- }
- }
- public String toString()
- {
- StringBuilder sb=new StringBuilder();
- for(int i=0; i<buckets.length; i++)
- {
- sb.append(i +":");
- for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.getLink())
- sb.append(curr.getInfo().toString() +" ");
- sb.append("\n");
- }
- return sb.toString();
- }
- }
- public class Lozinki {
- public static void main (String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- CBHT<String, String> hash=new CBHT(N);
- String s;
- String []ss;
- for(int i=1;i<=N;i++){
- String imelozinka = br.readLine();
- String[] pom = imelozinka.split(" ");
- hash.insert(pom[1], pom[0]);
- }
- while(true)
- {
- s=br.readLine();
- if(s.equals("KRAJ"))
- break;
- ss=s.split(" ");
- if(hash.search(ss[1])==null)
- System.out.println("Nenajaven");
- else
- {
- if(ss[0].equals(hash.search(ss[1]).getInfo().getValue()))
- {
- System.out.println("Najaven");
- break;
- }
- else
- System.out.println("Nenajaven");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement