import java.util.*;
import java.lang.*;
import java.io.*;
class Alimentos {
// rosettacode.org/wiki/Levenshtein_distance#Java
a = a.toLowerCase();
b = b.toLowerCase();
int [] costs = new int [b.length() + 1];
for (int j = 0; j < costs.length; j++)
costs[j] = j;
for (int i = 1; i <= a.length(); i++) {
costs[0] = i;
int nw = i - 1;
for (int j = 1; j <= b.length(); j++) {
int cj
= Math.
min(1 + Math.
min(costs
[j
], costs
[j
- 1]), a.
charAt(i
- 1) == b.
charAt(j
- 1) ? nw
: nw
+ 1); nw = costs[j];
costs[j] = cj;
}
}
return costs[b.length()];
}
public static void main
(String args
[]){ alimentos[0] = "Vegetariano";
alimentos[1] = "Peixe";
alimentos[2] = "Frango";
alimentos[3] = "Carne";
int distancia;
int prevDistancia;
Scanner in
= new Scanner
(System.
in); System.
out.
println("Digite o Alimento (Vegetariano, Peixe, Frango ou Carne): "); while ( in.hasNext() ) {
alimento = in.nextLine();
prevDistancia = 1000;
resultado = "";
for ( String item
: alimentos
) { distancia = distance( item, alimento );
if ( prevDistancia > distancia ) {
prevDistancia = distancia;
resultado = item;
}
}
System.
out.
println("Alimento: "+resultado
); }
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBBbGltZW50b3MgewoKICAgIC8vIHJvc2V0dGFjb2RlLm9yZy93aWtpL0xldmVuc2h0ZWluX2Rpc3RhbmNlI0phdmEKICAgIHB1YmxpYyBzdGF0aWMgaW50IGRpc3RhbmNlKFN0cmluZyBhLCBTdHJpbmcgYikgewogICAgICAgIGEgPSBhLnRvTG93ZXJDYXNlKCk7CiAgICAgICAgYiA9IGIudG9Mb3dlckNhc2UoKTsKICAgICAgICBpbnQgW10gY29zdHMgPSBuZXcgaW50IFtiLmxlbmd0aCgpICsgMV07CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBjb3N0cy5sZW5ndGg7IGorKykKICAgICAgICAgICAgY29zdHNbal0gPSBqOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IGEubGVuZ3RoKCk7IGkrKykgewogICAgICAgICAgICBjb3N0c1swXSA9IGk7CiAgICAgICAgICAgIGludCBudyA9IGkgLSAxOwogICAgICAgICAgICBmb3IgKGludCBqID0gMTsgaiA8PSBiLmxlbmd0aCgpOyBqKyspIHsKICAgICAgICAgICAgICAgIGludCBjaiA9IE1hdGgubWluKDEgKyBNYXRoLm1pbihjb3N0c1tqXSwgY29zdHNbaiAtIDFdKSwgYS5jaGFyQXQoaSAtIDEpID09IGIuY2hhckF0KGogLSAxKSA/IG53IDogbncgKyAxKTsKICAgICAgICAgICAgICAgIG53ID0gY29zdHNbal07CiAgICAgICAgICAgICAgICBjb3N0c1tqXSA9IGNqOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBjb3N0c1tiLmxlbmd0aCgpXTsKICAgIH0KIAogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nIGFyZ3NbXSl7CiAgICAJU3RyaW5nW10gYWxpbWVudG9zID0gbmV3IFN0cmluZ1s0XTsKICAgIAlhbGltZW50b3NbMF0gPSAiVmVnZXRhcmlhbm8iOwogICAgCWFsaW1lbnRvc1sxXSA9ICJQZWl4ZSI7CiAgICAJYWxpbWVudG9zWzJdID0gIkZyYW5nbyI7CiAgICAJYWxpbWVudG9zWzNdID0gIkNhcm5lIjsKICAgICAgICBTdHJpbmcgYWxpbWVudG87CiAgICAgICAgU3RyaW5nIHJlc3VsdGFkbzsKICAgICAgICBpbnQgZGlzdGFuY2lhOwogICAgICAgIGludCBwcmV2RGlzdGFuY2lhOwoKICAgICAgICBTY2FubmVyIGluID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkRpZ2l0ZSBvIEFsaW1lbnRvIChWZWdldGFyaWFubywgUGVpeGUsIEZyYW5nbyBvdSBDYXJuZSk6ICIpOwogICAgICAgIHdoaWxlICggaW4uaGFzTmV4dCgpICkgewogICAgICAgIAlhbGltZW50byA9IGluLm5leHRMaW5lKCk7CiAgICAgICAgICAgIHByZXZEaXN0YW5jaWEgPSAxMDAwOwogICAgICAgICAgICByZXN1bHRhZG8gPSAiIjsKICAgICAgICAgICAgZm9yICggU3RyaW5nIGl0ZW0gOiBhbGltZW50b3MgKSB7CiAgICAgICAgICAgIAlkaXN0YW5jaWEgPSBkaXN0YW5jZSggaXRlbSwgYWxpbWVudG8gKTsKICAgICAgICAgICAgICAgIGlmICggcHJldkRpc3RhbmNpYSA+IGRpc3RhbmNpYSApIHsKICAgICAgICAgICAgICAgIAlwcmV2RGlzdGFuY2lhID0gZGlzdGFuY2lhOwogICAgICAgICAgICAgICAgCXJlc3VsdGFkbyA9IGl0ZW07CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJBbGltZW50bzogIityZXN1bHRhZG8gKTsKICAgICAgICB9CiAgICB9Cn0=