Discussione: Distanza di editing
View Single Post
Old 28-08-2017, 12:54   #1
stev809
Junior Member
 
Iscritto dal: Aug 2017
Messaggi: 20
Distanza di editing

Ciao a tutti ho aperto un nuovo tread per chiedere se c'è la possibilità che da questo codice
Codice:
	public static int distanzaEditing (CharSequence lhs, CharSequence rhs) {                          
		    int len0 = lhs.length() + 1;                                                     
		    int len1 = rhs.length() + 1;                                                     
		    int cost_replace =0;
		    int cost_insert =0;
            int cost_delete =0;
		    // the array of distances                                                       
		    int[] cost = new int[len0];                                                     
		    int[] newcost = new int[len0];                                                  
		                                                                                    
		    // initial cost of skipping prefix in String s0                                 
		    for (int i = 0; i < len0; i++) cost[i] = i;                                     
		                                                                                    
		    // dynamically computing the array of distances                                  
		                                                                                    
		    // transformation cost for each letter in s1                                    
		    for (int j = 1; j < len1; j++) {                                                
		        // initial cost of skipping prefix in String s1                             
		        newcost[0] = j;                                                             
		                                                                                    
		        // transformation cost for each letter in s0                                
		        for(int i = 1; i < len0; i++) {                                             
		            // matching current letters in both strings                             
		            int match = (lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1;             
		                                                                                    
		            // computing cost for each transformation                               
		             cost_replace = cost[i - 1] + match;                                 
		             cost_insert  = cost[i] + 1;                                         
		             cost_delete  = newcost[i - 1] + 1;                                  
		                                                                                    
		            // keep minimum cost                                                    
		            newcost[i] = Math.min(Math.min(cost_insert, cost_delete), cost_replace);
		        }                                                                           
		                                                                                    
		        // swap cost/newcost arrays                                                 
		        int[] swap = cost; 
		        cost = newcost; 
		        newcost = swap;                          
		    }
		    
		                                                                                    
		    // the distance is the cost for transforming all letters in both strings        
		    return cost[len0 - 1] ;                                                          
		}
io possa estrapolare gli indici e le lettere per stampare quali operazioni di modifica siano state fatte e in che parte della stringa

Grazie a tutti
stev809 è offline   Rispondi citando il messaggio o parte di esso