PDA

View Full Version : سوال: الگوريتم LongestCommonSubstring



armannnext
سه شنبه 31 خرداد 1390, 11:15 صبح
با سلام خدمت دوستان ميخواستم بدونم اين الگوريتم دقيقا چي كار ميكنه اگه روش كاركردش رو هم بگيد ممنون ميشم البته مربوط به طراحي الگوريتم ميشه ولي چون C# بود گفتم مطرح كنم



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace max_sub_strings
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static string LongestCommonSubstring(string S1, string S2)
{
int Start = 0;
int Max = 0;
for (int i = 0; i < S1.Length; i++)
{
for (int j = 0; j < S2.Length; j++)
{
int x = 0;
while (S1[i + x] == S2[j + x])
{
x++;
if ((i + x >= S1.Length) || (j + x >= S2.Length)) break;
}
if (x > Max)
{
Max = x;
Start = i;
}

}
}
return S1.Substring(Start, Max);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
textBox3.Text = LongestCommonSubstring(textBox1.Text, textBox2.Text);

}
}
}