算法 二维数组

1.最长公共子序列

 static int[,] martix;

    static string str1 = "cnblogs";
    static string str2 = "belong";

    protected void Page_Load(object sender, EventArgs e)
    {
        martix = new int[str1.Length + 1, str2.Length + 1];
        
        LCS(str1, str2);
        Response.Write("<br><br><br><br><br><br>");
        GetArr(martix);
        
        //只要拿出矩阵最后一个位置的数字即可
        //Response.Write(String.Format("当前最大公共子序列的长度为:{0}<br>", martix[str1.Length, str2.Length]));   
    }
    static void LCS(string str1, string str2)
    {
        //初始化边界,过滤掉0的情况
        for (int i = 0; i <= str1.Length; i++)
            martix[i, 0] = 0;

        for (int j = 0; j <= str2.Length; j++)
            martix[0, j] = 0;

        //填充矩阵
        for (int i = 1; i <= str1.Length; i++)
        {
            for (int j = 1; j <= str2.Length; j++)
            {
                //相等的情况
                if (str1[i - 1] == str2[j - 1])
                {
                    martix[i, j] = martix[i - 1, j - 1] + 1;
                }
                else
                {
                    //比较“左边”和“上边“,根据其max来填充
                    if (martix[i - 1, j] >= martix[i, j - 1])
                        martix[i, j] = martix[i - 1, j];
                    else
                        martix[i, j] = martix[i, j - 1];
                }
            }
        }  
    }
    public static void GetArr(int[,] arr)
    {
        int x = arr.GetUpperBound(0);
        int y = arr.GetUpperBound(1);
        for (int i = 0; i <= x; i++)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;");
            for (int j = 0; j <= y; j++)
            {
                strBuilder.Append(arr[i, j].ToString() + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ");
            }
            HttpContext.Current.Response.Write(strBuilder.ToString() + "<br><br><br>");
        }
    }
    

 

总结:就是在边上加一圈零,比较相对应的字符,相等,值就等于左上角的值,

不相等,值就去左边和上边的最大值。

 2.字符串相似度

  static int[,] martix;

    static string str1 = "dhongda";
    static string str2 = "fehongda";

    protected void Page_Load(object sender, EventArgs e)
    {
        martix = new int[str1.Length + 1, str2.Length + 1];
        
        LCS(str1, str2);
        Response.Write("<br><br><br><br><br><br>");
        GetArr(martix);
        
        //只要拿出矩阵最后一个位置的数字即可
        //Response.Write(String.Format("当前最大公共子序列的长度为:{0}<br>", martix[str1.Length, str2.Length]));   
    }
    static void LCS(string str1, string str2)
    {
        //初始化边界值(忽略计算时的边界情况)
        for (int i = 0; i <= str1.Length; i++)
        {
            martix[i, 0] = i;
        }

        for (int j = 0; j <= str2.Length; j++)
        {
            martix[0, j] = j;
        }

        //矩阵的 X 坐标
        for (int i = 1; i <= str1.Length; i++)
        {
            //矩阵的 Y 坐标
            for (int j = 1; j <= str2.Length; j++)
            {
                //相等情况
                if (str1[i - 1] == str2[j - 1])
                {
                    martix[i, j] = martix[i - 1, j - 1];
                }
                else
                {
                    //取“左前方”,“上方”,“左方“的最小值
                    var temp1 = Math.Min(martix[i - 1, j], martix[i, j - 1]);

                    //获取最小值
                    var min = Math.Min(temp1, martix[i - 1, j - 1]);

                    martix[i, j] = min + 1;
                }
            }
        }
    }
    public static void GetArr(int[,] arr)
    {
        int x = arr.GetUpperBound(0);
        int y = arr.GetUpperBound(1);
        for (int i = 0; i <= x; i++)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;");
            for (int j = 0; j <= y; j++)
            {
                strBuilder.Append(arr[i, j].ToString() + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ");
            }
            HttpContext.Current.Response.Write(strBuilder.ToString() + "<br><br><br>");
        }
    }
    

总结:就是在边上加一圈零,比较相对应的字符,相等,值就等于左上角的值,

不相等,值就去左边,左上角和上边的最小值,就是四角中最小的,再加1.

 

http://www.cnblogs.com/huangxincheng/category/401959.html

posted @ 2013-01-24 17:37  hongdada  阅读(404)  评论(0编辑  收藏  举报