cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <head>
 <link rel="stylesheet" href="https://foundation.zurb.com/docs/assets/css/docs.css"> 
 <style>

 </style>
</head>

<body>
<div class="row">
  <div class="small-8 small-centered columns" id="app"></div>
</div>


  



<script id="template" type="text/regular" name="template">


 <label>From string</label>
<input type="text" r-model='from' placeholder="from" />
 <label>dest String</label>
<input type="text" r-model='to' placeholder="to" />


<table class="small-12">
  <tbody>
    <tr>
      <td></td>
      <td></td>
    {{#list from.split("") as item}}
      <td>{{item}}</td>
    {{/list}}
    </tr>
    <tr> 
      <td></td>
      {{#list matrix && matrix[0] as item}}
      <td class={{this.inRoad(0 , item_index)? 'active': ''}}>{{item}}</td>
      {{/list}}
    </tr>
    {{#list to.split("") as m}}
      <tr>
        <td>{{to[m_index]}}</td>
        {{#list matrix && matrix[m_index+1] as item}}
        <td  class={{this.inRoad(m_index + 1, item_index)? 'active': ''}}>{{item}}</td>
        {{/list}}
      </tr>
    {{/list}}
  </tbody>
  
  
</table>
  
</script>

<!-- 引入regular.js -->
<script src="https://rawgit.com/regularjs/regular/master/dist/regular.js"></script>



  
</body>

            
          
!
            
              var ld = (function(){
  function equals(a,b){
    return a === b ;
  }
  function ld(array1, array2){
    var n = array1.length;
    var m = array2.length;
    var matrix = [];
    for(var i = 0; i <= n; i++){
      matrix.push([i]);
    }
    for(var j=1;j<=m;j++){
      matrix[0][j]=j;
    }
    for(var i = 1; i <= n; i++){
      for(var j = 1; j <= m; j++){
        if(equals(array1[i-1], array2[j-1])){
          matrix[i][j] = matrix[i-1][j-1];
        }else{
          matrix[i][j] = Math.min(
            matrix[i-1][j]+1, //delete
            matrix[i][j-1]+1//add
            )
        }
      }
    }
    return matrix;
  }
  function whole(arr2, arr1) {
      var matrix = ld(arr1, arr2)
      var n = arr1.length;
      var i = n;
      var m = arr2.length;
      var j = m;
      var edits = [];
      var roadMap = {};
      var current = matrix[i][j];
      roadMap[i+":"+j]= true;
      while(i>0 || j>0){
      // the last line
        if (i === 0) {

          edits.unshift(3);
          j--;
          roadMap[i+":"+j]= true;
          continue;
        }
        // the last col
        if (j === 0) {
          edits.unshift(2);
          i--;
          roadMap[i+":"+j]= true;
          continue;
        }
        var northWest = matrix[i - 1][j - 1];
        var west = matrix[i - 1][j];
        var north = matrix[i][j - 1];

        var min = Math.min(north, west, northWest);

        if (min === west) {
          edits.unshift(2); //delete
          i--;
          current = west;

        } else if (min === northWest ) {
          if (northWest === current) {
            edits.unshift(0); //no change
          } else {
            edits.unshift(1); //update
            current = northWest;
          }
          i--;
          j--;
        } else {
          edits.unshift(3); //add
          j--;
          current = north;
        }
        roadMap[i+":"+j]= true;

      }
      
      var LEAVE = 0;
      var ADD = 3;
      var DELELE = 2;
      var UPDATE = 1;
      var n = 0;m=0;
      var steps = [];
      var step = {index: null, add:0, removed:[]};

      for(var i=0;i<edits.length;i++){
        if(edits[i] > 0 ){ // NOT LEAVE
          if(step.index === null){
            step.index = m;
          }
        } else { //LEAVE
          if(step.index != null){
            steps.push(step)
            step = {index: null, add:0, removed:[]};
          }
        }
        switch(edits[i]){
          case LEAVE:
            n++;
            m++;
            break;
          case ADD:
            step.add++;
            m++;
            break;
          case DELELE:
            step.removed.push(arr1[n])
            n++;
            break;
          case UPDATE:
            step.add++;
            step.removed.push(arr1[n])
            n++;
            m++;
            break;
        }
      }
      if(step.index != null){
        steps.push(step)
      }
      steps.matrix = matrix;
      steps.roadMap = roadMap;
      return steps;
    }
    return whole;
  })();

Regular.config({BEGIN:'{{', END:'}}'})


//利用Regular构建你的app吧
var EditDistanceDemo = Regular.extend({
  template: '#template',
  config: function(data){
    this.$watch(["from", "to"], function(from, to){
      var res = ld(from,to);
      data.matrix = res.matrix;
      data.roadMap = res.roadMap;

    })
  }
});

// initialize component then $inject to #app's  bottom
var component = new EditDistanceDemo({
  data: {
    from: 'abc',
    to: 'abcd'
  },
  inRoad: function(i,j){
    return this.data.roadMap[i+":"+j] === true;
  }
});
component.$inject('#app'); 
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console