算法 二维数组

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

转载于:https://www.cnblogs.com/hongdada/archive/2013/01/24/2875398.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值