我猜你说的是模式发现。看看一些基本方法(来源)
private static Dictionary<string, int> FindPatterns(string value) {
List<string> patternToSearchList = new List<string>();
for (int i = 0; i < value.Length; i++) {
for (int j = 2; j <= value.Length / 2; j++) {
if (i + j <= value.Length) {
patternToSearchList.Add(value.Substring(i, j));
}
}
}
// pattern matching
Dictionary<string, int> results = new Dictionary<string, int>();
foreach (string pattern in patternToSearchList) {
int occurence = Regex.Matches(value, pattern, RegexOptions.IgnoreCase).Count;
if (occurence > 1) {
results[pattern] = occurence;
}
}
return results;
}
static void Main(string[] args) {
Dictionary<string, int> result = FindPatterns("asdxgkeopgkajdflkjbpoijadadafhjkafikeoadkjhadfkjhocihakeo");
foreach (KeyValuePair<string, int> res in result.OrderByDescending(r => r.Value)) {
Console.WriteLine("Pattern:" + res.Key + " occurence:" + res.Value.ToString());
}
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
该算法由 2 个阶段组成。
它使用正则表达式进行模式匹配。还有其他更高级的算法。这些算法列于地址http://www-igm.univ-mlv.fr/~lecroq/string/ 但是,代码示例是用 C 编写的。此外,您还可以查看用于模式匹配的Boyer-Moore 算法,用 C# 编写
| 归档时间: |
|
| 查看次数: |
7298 次 |
| 最近记录: |