mic_checker
Top Contributor
Der Rückgabewert - also die Levenshtein Distance (LD) - gibt die Anzahl der Löschungen/Einfügungen/Substitutionen etc. an, die man benötigt um einen String s in einen anderen String t zu transformieren.
Freu dich nicht zu frühHobbit_Im_Blutrausch hat gesagt.:Es scheint zu funktionieren!
public static int getLevenshteinDistance (String s, String t) {
if (s == null || t == null) {
throw new IllegalArgumentException("Strings must not be null");
}
/*
The difference between this impl. and the previous is that, rather
than creating and retaining a matrix of size s.length()+1 by t.length()+1,
we maintain two single-dimensional arrays of length s.length()+1. The first, d,
is the 'current working' distance array that maintains the newest distance cost
counts as we iterate through the characters of String s. Each time we increment
the index of String t we are comparing, d is copied to p, the second int[]. Doing so
allows us to retain the previous cost counts as required by the algorithm (taking
the minimum of the cost count to the left, up one, and diagonally up and to the left
of the current cost count being calculated). (Note that the arrays aren't really
copied anymore, just switched...this is clearly much better than cloning an array
or doing a System.arraycopy() each time through the outer loop.)
Effectively, the difference between the two implementations is this one does not
cause an out of memory condition when calculating the LD over two very large strings.
*/
int n = s.length(); // length of s
int m = t.length(); // length of t
if (n == 0) {
return m;
} else if (m == 0) {
return n;
}
int p[] = new int[n+1]; //'previous' cost array, horizontally
int d[] = new int[n+1]; // cost array, horizontally
int _d[]; //placeholder to assist in swapping p and d
// indexes into strings s and t
int i; // iterates through s
int j; // iterates through t
char t_j; // jth character of t
int cost; // cost
for (i = 0; i<=n; i++) {
p[i] = i;
}
for (j = 1; j<=m; j++) {
t_j = t.charAt(j-1);
d[0] = j;
for (i=1; i<=n; i++) {
cost = s.charAt(i-1)==t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left and up +cost
d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
/* Wenn Zeichen übereinstimmen -> Keine Substitution etc. notwendig -> 0 "Kosten" */
if(chs == cht)
cost = 0;
else
cost = 1;
/* Berechnung des Minimums */
minimum = matrix[i-1][j]+1;
if((matrix[i][j-1]+1) < minimum)
minimum = matrix[i][j-1]+1;
if((matrix[i-1][j-1] + cost) < minimum)
minimum = matrix[i-1][j-1] + cost;
matrix[i][j] = minimum;
import java.io.*;
public class diffTest {
int diff(String str1, String str2) {
str1 = " " + str1 + " ";
str2 = " " + str2 + " ";
int fehler = 0;
int[][] check = new int[str1.length()][str2.length()];
for (int a = 0; a < str1.length(); a++) {
for (int b = 0; b < str2.length(); b++) {
if (str1.charAt(a) == str2.charAt(b)) {
check[a][b] = 1;
}
}
}
if (str1.length() < str2.length()) {
fehler = str2.length() - str1.length();
}
for (int a = str1.length() - 1, b = str2.length() - 1; a >= 0 && b >= 0;) {
if (check[a][b] == 1) {
a--;
b--;
}
else {
for (int c = 0; c <= a; c++) {
fehler++;
for (int d = 0; d <= b; d++) {
if (check[a - c][b - d] == 1) {
if (c != 0) {
fehler--;
}
a = a - c;
b = b - d;
d = b + 1;
c = a + 1;
}
}
}
}
}
return fehler;
}
public static void main(String[] args) {
String str1 = null;
String str2 = null;
int fehler1 = 0;
int fehler2 = 0;
diffTest test = new diffTest();
while(true) {
try {
str1 = new BufferedReader(new InputStreamReader(System.in)).readLine();
str2 = new BufferedReader(new InputStreamReader(System.in)).readLine();
}
catch (Exception e) {
System.out.println(e.toString());
}
fehler1 = test.diff(str1, str2);
fehler2 = test.diff(str2, str1);
if (fehler1 > fehler2) {
System.out.println("Fehler: " + fehler2);
}
else {
System.out.println("Fehler: " + fehler1);
}
}
}
}
mic_checker hat gesagt.:...
... sind Änderungen nötig um den neuen Buchstaben zu erhalten,...
mic_checker hat gesagt.:...
...du erhälst also auf jeden Fall in der Hauptdiagonalen einen Wert der um (mind.) eins größer ist als einer der drei. ...
Der Levenshtein stellt dann nur fest: An dieser Stelle müsste eine Änderung vorgenommen werden, sei es nun eine Löschung,Änderung oder Substitution.Hobbit_Im_Blutrausch hat gesagt.:Hä? Steh hier komplett auf dem Schlau. Wird da was geändert oder ist das einfach nur die Feststellung, dass was nicht überein stimmt.
Das mit dem mindestens war unnötig. Wenn ich das richtig erfasst habe kann der Wert nämlich nur nur 1 zunehmen, also mindestens war überflüssig.mic_checker hat gesagt.:...
...du erhälst also auf jeden Fall in der Hauptdiagonalen einen Wert der um (mind.) eins größer ist als einer der drei. ...
Woher weiß ich um wieviel er größer ist und von welchem Punkt der Matrix ich das berechne?
Wenn der jeweilige Buchstabe an einer best. Stelle mit dem Original übereinstimmt (cost = 0) , dann soll natürlich zum linksdiagonalen Element nichts mehr hinzuaddiert werden, da man das Ergebnis natürlich nicht verfälschen will.[edit] und warum wird bei der Berechnung des Minimums zweimal der Wert "1" als Zusatz verwendet und einmal der Wert von "cost"? Das ist mir auch noch nicht ganz klar
int n = s.length();
int m = t.length();
...
return matrix[n][m];
for(int i = 1;i < hoehe;i++) {
...
for(int j = 1;j < breite;j++) {
...
}
}
for (int a = 0; a < matrix.length; a++) {
for (int b = 0; b < matrix[0].length; b++) {
System.out.print(matrix[a][b]);
}
System.out.println();
}
for (int a = matrix.length - 1; a >= 0; a--) {
for (int b = 0; b < matrix[0].length; b++) {
System.out.print(matrix[a][b]);
}
System.out.println();
}
Da hast du recht - doch was heisst es wenn i maximal groß ist ? Dann steht es in der letzten Zeile ganz rechts.Du gibst den Wert in der Matrix zurück, der am höchsten und am weitesten Rechts liegt
System.out.print(" ");
for(int i = 0;i < m;i++)
System.out.print(t.charAt(i)+" ");
System.out.println();
/*
* Ausgabe der Matrix
*/
for (int a = 0; a < matrix.length; a++) {
if(a >= 1)
System.out.print(s.charAt(a-1)+" ");
else
System.out.print(" ");
for (int b = 0; b < matrix[0].length; b++) {
System.out.print(matrix[a][b]+" ");
}
System.out.println();
}
Der Algorithmus müsste doch eigentlich für dich in die richtige Richtung gehen oder?Wildcard hat gesagt.:Womöglich schon, mich interessiert eher das algemeinere Problem von minimal möglich Transformationen, und ob's
da einen Algo gibt der nachweislich exakt ist.