110,538
社区成员
发帖
与我相关
我的任务
分享
string str1 = "abcdefdsafdsakl;jdsfkl;wjqr";
string str2 = "uiyoiweqrkjljdesakfvdefnxczv;ljsdaasd';oiew[r;slakjfa";
Sub Main()
Dim str1 As String = "abcdefdsafdsakl;jdsfkl;wjqr"
Dim str2 As String = "uiyoiweqrkjljdesakfvdefnxczv;ljsdaasd';oiew[r;slakjfa"
Dim strTemp1, strTemp2 As String
If Len(str1) > Len(str2) Then
strTemp1 = str2
strTemp2 = str1
Else
strTemp1 = str1
strTemp2 = str2
End If
Dim strPointer As String = ""
Dim arr As New ArrayList()
For i As Integer = 1 To strTemp1.Length
For j As Integer = 1 To strTemp1.Length - i + 1
strPointer = Mid(strTemp1, i, j)
If InStr(strTemp2, strPointer) <> 0 Then
arr.Add(strPointer)
End If
Next
Next
Dim MaxChildStr As String = ""
If arr.Count <> 0 Then
MaxChildStr = arr(0)
For Each strChild In arr
If strChild.Length > MaxChildStr.Length Then
MaxChildStr = strChild
End If
Next
End If
Console.WriteLine(MaxChildStr)
Console.ReadLine()
End Sub
public void Main()
{
string str1 = "abcdefdsafdsakl;jdsfkl;wjqr";
string str2 = "uiyoiweqrkjljdesakfvdefnxczv;ljsdaasd';oiew[r;slakjfa";
string strTemp1 = null;
string strTemp2 = null;
if (Strings.Len(str1) > Strings.Len(str2)) {
strTemp1 = str2;
strTemp2 = str1;
} else {
strTemp1 = str1;
strTemp2 = str2;
}
string strPointer = "";
ArrayList arr = new ArrayList();
for (int i = 1; i <= strTemp1.Length; i++) {
for (int j = 1; j <= strTemp1.Length - i + 1; j++) {
strPointer = Strings.Mid(strTemp1, i, j);
if (Strings.InStr(strTemp2, strPointer) != 0) {
arr.Add(strPointer);
}
}
}
string MaxChildStr = "";
if (arr.Count != 0) {
MaxChildStr = arr[0];
foreach (object strChild_loopVariable in arr) {
strChild = strChild_loopVariable;
if (strChild.Length > MaxChildStr.Length) {
MaxChildStr = strChild;
}
}
}
Console.WriteLine(MaxChildStr);
Console.ReadLine();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str1 = "abcdefdsafdsaklasdfasddfsgdsfas;ewr53253221nxczv;ljsdaaefcvxsakgbvnmmfvdefnxczv;ljsdaasdadw2sfadfadsoiew[r;ssdadw2s51jdsf23klwerqewrq;wjqr";
string str2 = "uiyoiweqrkjljdefcvxsakgbvnmmfvdeflasdfasnxczv;ljsdaasdadw2sfadfadsoiew[r;slakjfa";
/*
*
* 先找第一个相同的,如果第一个有相同的,就跟着第一个进去,看第二个是不是相同的
* 如果第二也相同,那就再跟进去看第三个是不是相同
* 如果不同,那就跳到第二。按第二个找到最长的字符串
*
*
*/
Console.WriteLine(new Program().Get_long_public_str(str1, str2));
Console.Read();
}
public string Get_long_public_str(string str1, string str2)
{
ArrayList al = new ArrayList();
string resultStr = "";
string tmpStr = "";
for (int i = 0; i < str1.Length; i++)
{
if (resultStr == "")
{
tmpStr = this.Get_Str2_Index(str1, str2, i, 1);
}
else
{
if (i + resultStr.Length + 1 > str1.Length)
{
tmpStr = "";
}
else
{
tmpStr = this.Get_Str2_Index(str1, str2, i, resultStr.Length + 1);
}
}
if (tmpStr.Length > resultStr.Length)
{
resultStr = tmpStr;
}
}
return resultStr;
}
public string Get_Str2_Index(string str1, string str2, int i, int charCount)
{
string tmpChar = str1.Substring(i,charCount);
if (str2.IndexOf(tmpChar) > 0)
{
charCount = charCount + 1;
if (i + charCount > str1.Length)
{
return str1.Substring(i, charCount - 1);
}
else
{
return Get_Str2_Index(str1, str2, i, charCount);
}
}
else
{
return str1.Substring(i, charCount-1);
}
}
}
}