110,545
社区成员
发帖
与我相关
我的任务
分享
foreach( RecordIndex X in this.RecordIndexs)
{
if( X.Match(Records,Index + 1 , PathStrings))
return true;
}
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
// //Console.WriteLine( Marshal.SizeOf( typeof(AsMonitorNumAtActivity )));
//
// XmlDocument doc = new XmlDocument();
// doc.Load(@"C:\111.xml");
// XmlNodeList list = doc.SelectNodes(@"NewDataSet/CalendarNumStat");
// Console.WriteLine( list.Count );
string[] recoders = { "WWW.XX.COM.CN", "WWW.XX.COM.CN ABC",
"WWW.XX.COM.CN ABC BCD", "WWW.XX.COM.CN ABC BCDEF" };
RecordIndex Index = new RecordIndex(recoders);
Console.WriteLine(Index.Match("WWW.XX.COM.CN ABC"));
Console.ReadLine();
//
// TODO: 在此处添加代码以启动应用程序
//
}
}
public class RecordIndex
{
//有幻型的话可以使用List<RecordIndex>
private class _RecordIndexs:ArrayList
{
public override int IndexOf(object value)
{
if( value is RecordIndex )
return base.IndexOf (value);
else if( value is string )
{
for( int i =0 ;i< Count ;i++)
{
if( (this[i] as RecordIndex).Subsection.Equals( value))
return i;
}
return -1;
}
return -1;
}
}
/// <summary>
/// 本级索引位置的关键字 例如 WWW.XX.COM.CN
/// </summary>
private string Subsection;
private _RecordIndexs RecordIndexs = new _RecordIndexs();
/// <summary>
/// 索引本身是否也是一个值
/// </summary>
private bool IndexIsValue=false;
private RecordIndex ( string Subsection )
{
this.Subsection = Subsection;
}
/// <summary>
/// 根据数据库中的数据记录构建索引记录 假设Records已经有序
/// </summary>
public RecordIndex(string[] Records)
{
foreach( string s in Records )
{
this.AddRecord(s);
}
}
/// <summary>
/// 向索引结构添加一条记录 用空格分割
/// </summary>
public void AddRecord(string Record)
{
string[] subRecords = Record.Split(' ');//用空格分割
AddRecord( subRecords , 0);
}
private void AddRecord( string[] subRecords , int Index)
{
if( Index >= subRecords.Length ) return;
string section = subRecords[Index];
if(section.Trim().Length == 0)
{
this.AddRecord(subRecords ,Index + section.Length );
}
int I = RecordIndexs.IndexOf(section);
RecordIndex subIndex;
if( I>=0 )
{
subIndex = RecordIndexs[I] as RecordIndex;
}
else
{
subIndex = new RecordIndex(section);
this.RecordIndexs.Add(subIndex);
}
if( subRecords.Length == Index + 1)
subIndex.IndexIsValue = true;
else
subIndex.AddRecord( subRecords , Index + 1);
}
public string Match( string Record)
{
return Match (Record.Trim().Split(' '));
}
public string Match( string[] Records)
{
ArrayList PathStrings = new ArrayList();
foreach( RecordIndex X in this.RecordIndexs)
{
if( X.Match(Records,0 , PathStrings))
break;
}
string ret = string.Empty;
foreach( string s in PathStrings)
{
ret += s+" ";
}
return ret;
}
private bool Match( string[] Records ,int Index , ArrayList PathStrings)
{
if( Records.Length <= Index) return false;
string Section = Records[Index];
if( this.Subsection.Equals( Section ))
{
if( Records.Length == Index + 1)
{
if( IndexIsValue )
{
PathStrings.Add( this.Subsection );
return true;
}
return false; //这里意思是数据库中没有完全是查找串的子串的记录 如果这种情况也可以匹配 返回True即可
}
else
{
PathStrings.Add( this.Subsection );
foreach( RecordIndex X in this.RecordIndexs)
{
if( X.Match(Records,Index + 1 , PathStrings))
return true;
}
}
}
return false;
}
}
static void Main(string[] args)
{
//你的URL
string site = "HTTP://WWW.XX.COM.CN/ABC/BCD/HA/AA.HTML";
Regex r1 = new Regex(@"(?<=/)[^/].+?(?=/)");
MatchCollection mc1 = r1.Matches(site);
string[] sc = new string[mc1.Count];
for (int i = 0; i < mc1.Count; i++)
{
sc[i] = mc1[i].Value;
}
string pattern = string.Join("|", sc);
Regex r2 = new Regex(pattern);
//你的记录
string[] recoders = { "WWW.XX.COM.CN", "WWW.XX.COM.CN ABC", "WWW.XX.COM.CN ABC BCD", "WWW.XX.COM.CN ABC BCDEF" };
int[] maxMatch = new int[recoders.Length];
for (int i = 0; i < recoders.Length; i++)
{
MatchCollection mc2 = r2.Matches(recoders[i]);
maxMatch[i] = mc2.Count;
}
int maxIndex = 0;
for (int i = 1; i < maxMatch.Length-1; i++)
{
if (maxMatch[i] > maxMatch[i - 1])
maxIndex = i;
}
Console.WriteLine(recoders[maxIndex]);
}