using System;
public class HelloWorld
{
static int BruteForceStringMatch(string text, string pattern)
{
int n = text.Length;
int m = pattern.Length;
for (int i = 0; i <= n - m; i++)
{
int j = 0;
while (j < m && text[i + j] == pattern[j])
{
j++;
}
if (j == m)
{
return i;
}
}
return -1;
}
static void Judge(string text, string pattern)
{
int result = BruteForceStringMatch(text, pattern);
if (result != -1)
{
Console.WriteLine($"字符串匹配成功,起始位置:{result}");
}
else
{
Console.WriteLine("未找到匹配");
}
}
static void Main()
{
string text = "abcddeffgabccbacome";
string pattern = "abccba";
Judge(text, pattern);
text = "iloveyou";
pattern = "loveu";
Judge(text, pattern);
text = " ";
pattern = "";
Judge(text, pattern);
}
}