Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在列表中查找字符串的公共部分<;字符串>;在C中#_C#_Linq - Fatal编程技术网

C# 如何在列表中查找字符串的公共部分<;字符串>;在C中#

C# 如何在列表中查找字符串的公共部分<;字符串>;在C中#,c#,linq,C#,Linq,我正在寻找一种在字符串列表中查找字符串最大公共部分的简洁方法。我想从一个列表中找到一个方法 {"1 Some Street, Some Town, XYZ" , "2 Some Street, Some Town, ABC" , "3 Some Street, Some Town, XYZ" , "4 Some Street, Some Town, ABC" } 要返回单个字符串“一些街道,一些城镇,”。我不知道字符串的公共部分是在输入列表中字符串的开头、结尾还是中间,我认为应该有一个简洁的方

我正在寻找一种在字符串列表中查找字符串最大公共部分的简洁方法。我想从一个列表中找到一个方法

{"1 Some Street, Some Town, XYZ" ,
"2 Some Street, Some Town, ABC" ,
"3 Some Street, Some Town, XYZ" ,
"4 Some Street, Some Town, ABC" }

要返回单个字符串
“一些街道,一些城镇,”
。我不知道字符串的公共部分是在输入列表中字符串的开头、结尾还是中间,我认为应该有一个简洁的方法来实现这一点,但我想不起来。

改编自《忧郁》。penguin的评论,使用

static void Main(字符串[]args)
{
var值=新列表
{“1某处街道,某处城镇,XYZ”,
“2一些街道,一些城镇,ABC”,
“3一些街道,一些城镇,XYZ”,
“4一些街道,一些城镇,ABC”};
Console.WriteLine(LongestCommonSubstring(values));
Console.ReadLine();
}
公共静态字符串LongestCommonSubstring(IList值)
{
字符串结果=string.Empty;
对于(int i=0;iresult.Length)
{
结果=tmp;
}
}
}
返回结果;
}
//资料来源:http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring
公共静态int LongestCommonSubstring(字符串str1、字符串str2、out字符串序列)
{
sequence=string.Empty;
if(String.IsNullOrEmpty(str1)| | String.IsNullOrEmpty(str2))
返回0;
int[,]num=新int[str1.Length,str2.Length];
int maxlen=0;
int=0;
StringBuilder sequenceBuilder=新的StringBuilder();
for(int i=0;imaxlen)
{
maxlen=num[i,j];
int=i-num[i,j]+1;
如果(lastSubsBegin==thisSubsBegin)
{//如果当前LCS与上次运行此块时相同
sequenceBuilder.Append(str1[i]);
}
else//如果找到不同的LCS,此块将重置字符串生成器
{
lastSubsBegin=此SUBSBEGIN;
sequenceBuilder.Length=0;//清除它
sequenceBuilder.Append(str1.Substring(lastsubsbeing,(i+1)-lastsubsbeing));
}
}
}
}
}
sequence=sequenceBuilder.ToString();
返回maxlen;
}

注意:如果打成平局,先到先得。

@downy.penguin这是钱!那是谷歌。。。。搜索“c#最长公共字符串”并单击第一个结果。我可以假设空格是单词分隔符,字符串是由单词组成的吗?或者它可以是任何字符序列?
static void Main(string[] args)
{
    var values = new List<string>
    {"1 Some Street, Some Town, XYZ" ,
    "2 Some Street, Some Town, ABC" ,
    "3 Some Street, Some Town, XYZ" ,
    "4 Some Street, Some Town, ABC" };

    Console.WriteLine(LongestCommonSubstring(values));

    Console.ReadLine();
}

public static string LongestCommonSubstring(IList<string> values)
{
    string result = string.Empty;

    for (int i = 0; i < values.Count - 1; i++)
    {
        for (int j = i + 1; j < values.Count; j++)
        {
            string tmp;
            if (LongestCommonSubstring(values[i], values[j], out tmp) > result.Length)
            {
                result = tmp;
            }
        }
    }

    return result;
}

// Source: http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring
public static int LongestCommonSubstring(string str1, string str2, out string sequence)
{
    sequence = string.Empty;
    if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2))
        return 0;

    int[,] num = new int[str1.Length, str2.Length];
    int maxlen = 0;
    int lastSubsBegin = 0;
    StringBuilder sequenceBuilder = new StringBuilder();

    for (int i = 0; i < str1.Length; i++)
    {
        for (int j = 0; j < str2.Length; j++)
        {
            if (str1[i] != str2[j])
                num[i, j] = 0;
            else
            {
                if ((i == 0) || (j == 0))
                    num[i, j] = 1;
                else
                    num[i, j] = 1 + num[i - 1, j - 1];

                if (num[i, j] > maxlen)
                {
                    maxlen = num[i, j];
                    int thisSubsBegin = i - num[i, j] + 1;
                    if (lastSubsBegin == thisSubsBegin)
                    {//if the current LCS is the same as the last time this block ran
                        sequenceBuilder.Append(str1[i]);
                    }
                    else //this block resets the string builder if a different LCS is found
                    {
                        lastSubsBegin = thisSubsBegin;
                        sequenceBuilder.Length = 0; //clear it
                        sequenceBuilder.Append(str1.Substring(lastSubsBegin, (i + 1) - lastSubsBegin));
                    }
                }
            }
        }
    }
    sequence = sequenceBuilder.ToString();
    return maxlen;
}