Advertisement
HEitor_Santos

Untitled

Apr 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.         String dic, ingles, foreign;
  8.         Dict dict;
  9.         String words[];
  10.         int hashed;
  11.         Lista dicts[] = new Lista[100000];
  12.         boolean flag =true;
  13.         while (flag){
  14.             dic = in.nextLine();
  15.             if(dic.equals("")){
  16.                 flag = false;
  17.             }
  18.             else{
  19.                 words = dic.split(" ");
  20.                 ingles=words[0];
  21.                 foreign = words[1];
  22.                 dict = new Dict(ingles,foreign);
  23.                 hashed = hash(foreign);
  24.                 if(dicts[hashed]==null){
  25.                     dicts[hashed] = new Lista();
  26.                 }
  27.                 dicts[hashed].inserir(dict);
  28.             }
  29.         }
  30.         flag = true;
  31.         while(flag){
  32.             foreign = in.nextLine();
  33.             if(foreign.equals("")){
  34.                 flag = false;
  35.             }
  36.             else {
  37.                 hashed = hash(foreign);
  38.                 if(dicts[hashed]!=null){
  39.                     System.out.println(dicts[hashed].find(foreign));
  40.                 }
  41.                 else{
  42.                     System.out.println("eh");
  43.                 }
  44.             }
  45.         }
  46.  
  47.     }
  48.     public static int hash(String key){
  49.         int tamString = key.length()/4;
  50.         int soma=0, mult, s;
  51.         String sub;
  52.         for(int i=0;i<tamString;i++){
  53.             sub = key.substring(i*4,(i*4)+4);
  54.             mult =1;
  55.             s = sub.length();
  56.             for(int j=0;j<s;j++){
  57.                 soma+= ((int) sub.charAt(j))*mult;
  58.                 mult*=256;
  59.             }
  60.         }
  61.         sub = key.substring(tamString*4);
  62.         mult =1;
  63.         s = sub.length();
  64.         for(int k=0;k<s;k++){
  65.             soma+=sub.charAt(k)*mult;
  66.             mult*=256;
  67.         }
  68.         return Math.abs(soma)%100000;
  69.     }
  70. }
  71. class Lista {
  72.     private Lista proximo;
  73.     private Dict dict;
  74.     Lista(){
  75.         proximo = null;
  76.         dict = null;
  77.     }
  78.     public void inserir(Dict _dict){
  79.         if(this.dict==null){
  80.             this.dict =_dict;
  81.             this.proximo = new Lista();
  82.         }
  83.         else {
  84.             this.proximo.inserir(_dict);
  85.         }
  86.     }
  87.     public String find(String _foreign){
  88.         if(this.dict!=null){
  89.             if(this.dict.getForeign().equals(_foreign)){
  90.                 return this.dict.getIngles();
  91.             }
  92.             else {
  93.                 if(this.proximo!=null){
  94.                     return this.proximo.find(_foreign);
  95.                 }
  96.                 else{
  97.                     return "eh";
  98.                 }
  99.             }
  100.         }
  101.         else {
  102.             return "eh";
  103.         }
  104.     }
  105. }
  106. class Dict {
  107.     private String ingles;
  108.     private String foreign;
  109.     Dict(String _ingles, String _foreign){
  110.         this.ingles = _ingles;
  111.         this.foreign = _foreign;
  112.     }
  113.     String getIngles(){
  114.         return this.ingles;
  115.     }
  116.     String getForeign(){
  117.         return this.foreign;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement