如题,C# codes 如下:
public static string[] GetBiggestString(string str1, string str2)
{
var max=0;
var count = 0;
ArrayList container = new ArrayList();
for (int i = 0; i < str2.Length; i++)
{
//Reset count
if (count != 0)
{
count = 0;
}
for (int j = 0; j < str1.Length; j++)
{
if (i + count < str2.Length)
{
if (str1[j] == str2[i + count])
{
count++;
if (count > max)
{
//Clear arrayList
container.Clear();
max = count;
//To judge if the string has already been in arraylist
if (!container.Contains(str2.Substring(i, count)))
{
container.Add(str2.Substring(i, count));
}
}
else if (count == max)
{
//To judge if the string has already been in arraylist
if (!container.Contains(str2.Substring(i, count)))
{
container.Add(str2.Substring(i, count));
}
}
}
else
{
//Go back
j -= count;
//Reset count
count = 0;
}
}
}
}
本文介绍了一个使用C#实现的寻找两个字符串之间的最长公共子串的算法。该算法通过双层循环遍历字符串,并利用ArrayList存储找到的最长公共子串。通过对字符串逐字符比较,实现了对最长公共子串的有效查找。
695

被折叠的 条评论
为什么被折叠?



