Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225358
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-08-02 10:16:25

  1. /**
  2.  * The Longest common subsequence algorithm
  3.  * created on Aug 2, 2011
  4.  */

  5. package org.mht.strings;

  6. import java.util.ArrayList;
  7. import java.util.Iterator;



  8. public class LCS {
  9.     
  10.     private int[][] _length;
  11.     private String _x;
  12.     private String _y;
  13.     private ArrayList<Character> _lcs;
  14.     
  15.     public LCS(String x, String y) {
  16.         _x = x;
  17.         _y = y;
  18.         _length = new int[x.length()+1][y.length()+1];
  19.         }
  20.     
  21.     public int LCSLength() {
  22.         
  23.         for (int i = 0; i <= _x.length(); i++)
  24.             _length[i][0] = 0;
  25.         for (int j = 0; j <= _y.length(); j++)
  26.             _length[0][j] = 0;
  27.         
  28.         for (int i = 1; i <= _x.length(); i ++) {
  29.             for (int j = 1; j <= _y.length(); j++) {
  30.                 if (_x.charAt(i-1) == _y.charAt(j-1))            
  31.                     _length[i][j] = _length[i-1][j-1]+1;
  32.                 else
  33.                     _length[i][j] = maximum(_length[i][j-1], _length[i-1][j]);
  34.                 }
  35.             }
  36.         return _length[_x.length()][_y.length()];
  37.     }
  38.     
  39.     public static int maximum(int a, int b) {
  40.         return a >= b ? a : b;
  41.     }
  42.     
  43.     
  44.     public ArrayList backTrack() {
  45.         LCSLength();
  46.         if(_lcs == null) {
  47.             _lcs = new ArrayList<Character>();
  48.             backTrack(_x.length(), _y.length());
  49.         }
  50.         
  51.         return _lcs;
  52.     }
  53.     
  54.     public void backTrack(int i, int j) {
  55.         if ((i == 0) || (j == 0))
  56.             return;
  57.         else if (isEqual(i,j)) {
  58.             backTrack( i-1, j-1);
  59.             _lcs.add(valueOfX(i));            
  60.         }
  61.         else {
  62.             if (_length[i-1][j] > _length[i][j-1]) {
  63.                 backTrack(i-1,j);
  64.             }
  65.             else
  66.                 backTrack(i, j-1);
  67.         }
  68.                 
  69.                 
  70.     }
  71.     
  72.     public boolean isEqual(int i, int j) {
  73.         return (valueOfX(i).equals(valueOfY(j)));
  74.     }
  75.     
  76.     public Character valueOfX(int i) {
  77.         return _x.charAt(i-1);
  78.     }
  79.     
  80.     public Character valueOfY(int j) {
  81.         return _y.charAt(j-1);
  82.     }
  83.     
  84.     public static void main(String[] args) {
  85.         LCS lcs = new LCS("adfabfdc", "adfadfadabyz");
  86.         
  87.         ArrayList<Character> result = new ArrayList<Character>();
  88.         result = lcs.backTrack();
  89.         System.out.println("The LCS length is " + lcs.LCSLength());
  90.         System.out.println("The length of result is " + result.size());
  91.         Iterator e = result.iterator();
  92.         System.out.print("The LCS is :");
  93.         while(e.hasNext())
  94.             System.out.print(e.next().toString());
  95.         
  96.     }
  97.     
  98. }///:~

  99. /** Algorithm calculating LCS length:
  100.  * function LCSLength(X[1..m], Y[1..n])
  101.     C = array(0..m, 0..n)
  102.     for i := 0..m
  103.        C[i,0] = 0
  104.     for j := 0..n
  105.        C[0,j] = 0
  106.     for i := 1..m
  107.         for j := 1..n
  108.             if X[i-1] = Y[j-1]
  109.                 C[i,j] := C[i-1,j-1] + 1
  110.             else:
  111.                 C[i,j] := max(C[i,j-1], C[i-1,j])
  112.     return C[m,n]
  113.     
  114.     */

  115. /** Algorithm reading out of an LCS:
  116.  * function backTrack(C[0..m,0..n], X[1..m], Y[1..n], i, j)
  117.     if i = 0 or j = 0
  118.         return ""
  119.     else if X[i] = Y[j]
  120.         return backTrack(C, X, Y, i-1, j-1) + X[i]
  121.     else
  122.         if C[i,j-1] > C[i-1,j]
  123.             return backTrack(C, X, Y, i, j-1)
  124.         else
  125.             return backTrack(C, X, Y, i-1, j)

  126.  */

  127. /** Related problems:
  128.  * For two strings X_{1 \dots m} and Y_{1 \dots n}, the length of the shortest common supersequence is related to the length of the LCS by[2]

  129.     \left|SCS(X,Y)\right| = n + m - \left|LCS(X,Y)\right|.

  130. * The edit distance when only insertion and deletion is allowed (no substitution), or when the cost of the substitution is the double of the cost of an insertion or deletion, is:

  131.     d'(X,Y) = n + m - 2 \cdot \left|LCS(X,Y)\right|.
  132.     */
阅读(387) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~