/// Call Method
/// JFrameWork. DataBase. SQL. MatchString sp = new MatchString ();
/// Sp. parseStr ("(1 + 8 * (9-1) + (1-9) + (2*5) (z) z (55 )) (666) (59 )","(",")");
//// // Implementation code
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Collections;
Namespace JFrameWork. DataBase. SQL
{
/// <Summary>
/// Matched structure storage body
/// </Summary>
Struct MS
{
Public int left;
Public int right;
Public MS (int left, int right)
{
This. left = left;
This. right = right;
}
Public MS (MS)
{
This. left = ms. left;
This. right = ms. right;
}
}
Public class MatchString
{
/// <Summary>
/// Left queue
/// </Summary>
List <int> llist = new List <int> ();
/// <Summary>
/// Right-value queue
/// </Summary>
List <int> rlist = new List <int> ();
/// <Summary>
/// Mixed queue of left and right values
/// </Summary>
List <int> alllist = new List <int> ();
/// <Summary>
/// Match the storage queue
/// </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>
/// Obtain the Left and Right queue numbers of the string, which are stored in llist and rlist respectively.
/// </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>
/// Set the content of index numbers in llist and rlist to be imported to ALLList and sorted by serial number.
/// </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>
/// Store the nearest matching pair in the alllist in 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]);
}
}
}
}
// Although the final result in this section is correct, I think this is only a result that happens to be correct, and the algorithm seems to have some problems.
// Hope someone can solve the problem and get better algorithms
/// <Summary>
/// Search and filter the matchmap pair to obtain the final root-level matching pair and return
/// </Summary>
/// <Returns> </returns>
List <MS> getrootmatch ()
{
List <MS> TMP = new list <MS> ();
For (INT I = 0; I <matchmap. Count; I ++)
{
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)
{
A 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 in matchmap)
{
System. Console. writeline ("matchmap: [l] {0} \ t [R] {1}", ms. Left, ms. Right );
}
}
}
}