本文发表在 rolia.net 枫下论坛程序里有2个重要变量
string array = "ACDWFGAB"; 为被测试字符串
string[] pattern = { "*CD*", "*-GA*","--D-F*","--D-G*" }; 为Pattern数组,用来找出匹配的
兄弟们只要修改这2个变量就可以测试我的程序了,用递归的好处就是以后再有什么新花花肠子可以相对容易的把算法加进去
我也不知道这是不是你们说的那个NFL,AFL.....呵呵
Rolia排版混乱,没办法,如果愿意可以留下邮箱,我把源码发给你们
同样的,在VS2005中建立一个Console Project,把我的Codes拷贝进去就可运行
奥,对了,本人还在找工作欧,还希望大家能帮忙,多谢多谢
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string array = "ACDWFGAB";
string[] pattern = { "*CD*", "*-GA*","--D-F*","--D-G*" };
Console.WriteLine("I am still seeking job now....");
foreach (string s in pattern)
{
Pattern p = new Pattern(s, array);
if (p.Result())
Console.WriteLine("Pattern ( " + s + " ) match " + array);
}
Console.ReadLine();
}
}
public class Pattern
{
List<string> realArray = new List<string>();
List<string> pattern = new List<string>();
public Pattern(string pString, string rString)
{
foreach (char c in pString.ToCharArray())
pattern.Add(c.ToString());
foreach (char c in rString.ToCharArray())
realArray.Add(c.ToString());
}
public bool Result()
{
return Process(pattern, realArray);
}
public bool Process(List<string> currentPattern, List<string> currentArray)
{
List<string> tmpPattern = new List<string>(currentPattern);
List<string> tmpArray = new List<string>(currentArray);
if (currentPattern[0] == "*" && currentPattern.Count == 1) //边界
{
return true;
}
else if (currentPattern[0] == "-" && currentPattern.Count == 1 && currentArray.Count == 1) //边界
{
return true;
}
else if (currentPattern[0] == "*" && currentPattern.Count > 1 && currentPattern[1] == "-" && currentArray.Count > 0) // *--- AA
{
tmpPattern.RemoveAt(1);
tmpArray.RemoveAt(0);
return Process(tmpPattern, tmpArray);
}
else if (currentPattern[0] == "*" && currentPattern.Count > 1 && currentPattern[1] != "-")
{
for (int i = 0; i < currentArray.Count; i++)
{
if (currentArray[i] == currentPattern[1])
{
if (i == currentArray.Count - 1)
return true;
else
{
tmpArray.RemoveRange(0, i + 1);
tmpPattern.RemoveRange(0, 2);
if (tmpPattern.Count == 0 && tmpArray.Count > 0)
return false;
else
return Process(tmpPattern, tmpArray);
}
}
}
}
else if (currentPattern[0] == "-" && currentPattern.Count > 1)
{
if (currentArray.Count == 1)
{
return false;
}
else
{
tmpPattern.RemoveAt(0);
tmpArray.RemoveAt(0);
return Process(tmpPattern, tmpArray);
}
}
else if (currentPattern[0] == currentArray[0])
{
if (currentPattern.Count == 1 && currentArray.Count == 1)
{
return true;
}
else if (currentArray.Count > 1 && currentPattern.Count > 1)
{
tmpPattern.RemoveAt(0);
tmpArray.RemoveAt(0);
return Process(tmpPattern, tmpArray);
}
}
else
return false;
return false;
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
string array = "ACDWFGAB"; 为被测试字符串
string[] pattern = { "*CD*", "*-GA*","--D-F*","--D-G*" }; 为Pattern数组,用来找出匹配的
兄弟们只要修改这2个变量就可以测试我的程序了,用递归的好处就是以后再有什么新花花肠子可以相对容易的把算法加进去
我也不知道这是不是你们说的那个NFL,AFL.....呵呵
Rolia排版混乱,没办法,如果愿意可以留下邮箱,我把源码发给你们
同样的,在VS2005中建立一个Console Project,把我的Codes拷贝进去就可运行
奥,对了,本人还在找工作欧,还希望大家能帮忙,多谢多谢
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string array = "ACDWFGAB";
string[] pattern = { "*CD*", "*-GA*","--D-F*","--D-G*" };
Console.WriteLine("I am still seeking job now....");
foreach (string s in pattern)
{
Pattern p = new Pattern(s, array);
if (p.Result())
Console.WriteLine("Pattern ( " + s + " ) match " + array);
}
Console.ReadLine();
}
}
public class Pattern
{
List<string> realArray = new List<string>();
List<string> pattern = new List<string>();
public Pattern(string pString, string rString)
{
foreach (char c in pString.ToCharArray())
pattern.Add(c.ToString());
foreach (char c in rString.ToCharArray())
realArray.Add(c.ToString());
}
public bool Result()
{
return Process(pattern, realArray);
}
public bool Process(List<string> currentPattern, List<string> currentArray)
{
List<string> tmpPattern = new List<string>(currentPattern);
List<string> tmpArray = new List<string>(currentArray);
if (currentPattern[0] == "*" && currentPattern.Count == 1) //边界
{
return true;
}
else if (currentPattern[0] == "-" && currentPattern.Count == 1 && currentArray.Count == 1) //边界
{
return true;
}
else if (currentPattern[0] == "*" && currentPattern.Count > 1 && currentPattern[1] == "-" && currentArray.Count > 0) // *--- AA
{
tmpPattern.RemoveAt(1);
tmpArray.RemoveAt(0);
return Process(tmpPattern, tmpArray);
}
else if (currentPattern[0] == "*" && currentPattern.Count > 1 && currentPattern[1] != "-")
{
for (int i = 0; i < currentArray.Count; i++)
{
if (currentArray[i] == currentPattern[1])
{
if (i == currentArray.Count - 1)
return true;
else
{
tmpArray.RemoveRange(0, i + 1);
tmpPattern.RemoveRange(0, 2);
if (tmpPattern.Count == 0 && tmpArray.Count > 0)
return false;
else
return Process(tmpPattern, tmpArray);
}
}
}
}
else if (currentPattern[0] == "-" && currentPattern.Count > 1)
{
if (currentArray.Count == 1)
{
return false;
}
else
{
tmpPattern.RemoveAt(0);
tmpArray.RemoveAt(0);
return Process(tmpPattern, tmpArray);
}
}
else if (currentPattern[0] == currentArray[0])
{
if (currentPattern.Count == 1 && currentArray.Count == 1)
{
return true;
}
else if (currentArray.Count > 1 && currentPattern.Count > 1)
{
tmpPattern.RemoveAt(0);
tmpArray.RemoveAt(0);
return Process(tmpPattern, tmpArray);
}
}
else
return false;
return false;
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net