括弧匹配演算法

來源:互聯網
上載者:User

///調用方法
///JFrameWork.DataBase.SQL.MatchString sp = new MatchString();
///sp.ParseStr("(1+8*(9-1)+((1-9)+(2*5)))((z)(z)z(55))((666))(59)", "(", ")");
/////////////////實現代碼

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace JFrameWork.DataBase.SQL
{
    /// <summary>
    /// 匹配對的結構儲存體
    /// </summary>
    struct MS
    {
        public int left;
        public int right;
        public MS(int left, int right)
        {
            this.left = left;
            this.right = right;
        }

        public MS(MS ms)
        {
            this.left = ms.left;
            this.right = ms.right;
        }
    }

    public class MatchString
    {
        /// <summary>
        /// 左值隊列
        /// </summary>
        List<int> llist = new List<int>();
        /// <summary>
        /// 右值隊列
        /// </summary>
        List<int> rlist = new List<int>();
        /// <summary>
        /// 左右值混合隊列
        /// </summary>
        List<int> alllist = new List<int>();
        /// <summary>
        /// 匹配對儲存隊列
        /// </summary>
        List<MS> matchmap = new List<MS>();

        public void ParseStr(string str, string lvalue, string rvalue)
        {
            System.Console.WriteLine("str : {0}", "01234567890123456789012345678901234567890123456789");
            System.Console.WriteLine("str : {0}", str);
            GetIndex(str, lvalue, rvalue);
            SetAllList();
            GetNearMatch();
            GetRootMatch();
            DisplayMatchMap();
        }

        /// <summary>
        /// 得到串的左值以及右值隊列序號分別存放在llist以及rlist中
        /// </summary>
        /// <param name="str"></param>
        /// <param name="lvalue"></param>
        /// <param name="rvalue"></param>
        void GetIndex(string str, string lvalue, string rvalue)
        {
            int lstartIndex = 0;
            int rstartIndex = 0;
            bool lb = true;
            bool rb = true;
            while (lb || rb)
            {
                if (lb)
                {
                    lstartIndex = str.IndexOf(lvalue, lstartIndex);
                    if (lstartIndex >= 0)
                    {
                        llist.Add(lstartIndex);
                        System.Console.WriteLine("LValue Index: {0}", lstartIndex);
                        lstartIndex++;
                    }
                    else
                    {
                        lb = false;
                    }
                }
                if (rb)
                {
                    rstartIndex = str.IndexOf(rvalue, rstartIndex);
                    if (rstartIndex >= 0)
                    {
                        rlist.Add(rstartIndex);
                        System.Console.WriteLine("RValue Index: {0}", rstartIndex);
                        rstartIndex++;
                    }
                    else
                    {
                        rb = false;
                    }
                }
            }

        }

        /// <summary>
        /// 設定llist以及rlist中所有的索引序號內容匯入到ALLList,並按序號進行排序
        /// </summary>
        void SetAllList()
        {
            for (int i = 0; i < llist.Count; i++)
            {
                alllist.Add(llist[i]);
            }
            for (int i = 0; i < rlist.Count; i++)
            {
                alllist.Add(rlist[i]);
            }
            alllist.Sort();
        }

        /// <summary>
        /// 將alllist中找到最相近的匹配對到matchmap中儲存
        /// </summary>
        void GetNearMatch()
        {
            Stack<int> stack = new Stack<int>();
            for (int i = 0; i < alllist.Count; i++)
            {
                if (llist.Contains(alllist[i]))
                {
                    stack.Push(alllist[i]);
                }
                else if (rlist.Contains(alllist[i]))
                {
                    if (stack.Peek() < alllist[i])
                    {
                        int tmp = stack.Pop();
                        matchmap.Add(new MS(tmp, alllist[i]));
                        System.Console.WriteLine("NeerMatch : {0}\t{1}", tmp, alllist[i]);
                    }
                }
            }
        }

 

//本處最終結果儘管是對的 但是我認為這隻是一個碰巧得到了正確的結果,而且演算法好像也有些問題
//希望有人能解決得到更好的演算法
        /// <summary>
        /// 對matchmap對進行搜尋過濾,得到最終的根級匹配對,並返回
        /// </summary>
        /// <returns></returns>
        List<MS> GetRootMatch()
        {
            List<MS> tmp = new List<MS>();

            for (int i = 0; i < matchmap.Count; i++)
            {
                MS ms = new MS(matchmap[i]);
                for (int j = i + 1; j < matchmap.Count; j++)
                {
                    if (matchmap[j].left < matchmap[i].left && matchmap[j].right > matchmap[i].right)
                    {
                        ms = new MS(matchmap[j]);
                        if (!tmp.Contains(ms))
                        {
                            tmp.Add(ms);
                            //System.Console.WriteLine("RootMatch : {0}\t{1}", ms.left, ms.right);
                        }

                    xxx:
                        for (int k = 0; k < matchmap.Count; k++)
                        {
                            for (int l = 0; l < tmp.Count; l++)
                            {
                                if (matchmap[k].left > tmp[l].left && matchmap[k].right < tmp[l].right)
                                {
                                    matchmap.RemoveAt(k);
                                    goto xxx;
                                }
                            }
                        }
                    }

                }

            }
            return matchmap;
        }

        void DisplayMatchMap()
        {
            foreach (MS ms in matchmap)
            {
                System.Console.WriteLine("MatchMap: [L] {0}\t [R] {1}",ms.left, ms.right);
            }
        }

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.