在数组c#中查找重复的单词

时间:2015-05-06 07:15:22

标签: c#

我有一系列子部门。我想要带有子部门和部门组合的返回字符串。

public static string[] TestDepartments
        {
            get
            {
                return new string[]
                {
                    "Radio-Electronic Systems and Devices in Radioelectronics and laser technologies department",
                    "Laser and Optic-Electronic Systems in Radioelectronics and laser technologies department",
                    "Optic-Electronic Devices for Scientific Research in Radioelectronics and laser technologies department",
                    "Theoretical Bases of Electrotechnology in Radioelectronics and laser technologies department",
                    "Technologies of Instrument Making in Radioelectronics and laser technologies department"
                };
            }
        }

我需要结果字符串:

string result = "Radio-Electronic Systems and Devices, Laser and Optic-Electronic Systems, Optic-Electronic Devices for Scientific Research, Theoretical Bases of Electrotechnology, echnologies of Instrument Making in Radioelectronics and laser technologies department";

您可以推荐什么算法?或者可能存在在字符串数组中查找重复单词组合的示例?在我的例子中,我需要找到部门重复。我需要针对不同部门的通用方法。

1 个答案:

答案 0 :(得分:1)

您可以从:

开始
string[] testDepartments = new string[]
{
    "Radio-Electronic Systems and Devices in Radioelectronics and laser technologies department",
    "Laser and Optic-Electronic Systems in Radioelectronics and laser technologies department",
    "Optic-Electronic Devices for Scientific Research in Radioelectronics and laser technologies department",
    "Theoretical Bases of Electrotechnology in Radioelectronics and laser technologies department",
    "Technologies of Instrument Making in Radioelectronics and laser technologies department"
};

int longestCommonPostfixIndex = 0;
string firstString = testDepartments.First();

// TODO: change to binary searching
// TODO: check every string's length
while(testDepartments.All(td => td.Substring(td.Length - longestCommonPostfixIndex) == firstString.Substring(firstString.Length - longestCommonPostfixIndex)))
     longestCommonPostfixIndex++;

string res = string.Join(", ", testDepartments.Select(d => d.Substring(0, d.Length - longestCommonPostfixIndex + 1))) + firstString.Substring(firstString.Length - longestCommonPostfixIndex);

然后尝试优化它。

res ="无线电电子系统和设备,激光和光电子系统,光学电子科学研究设备,电子技术的理论基础,无线电电子仪器制造技术和激光技术部门&# 34;

你也可以看看这个:https://stackoverflow.com/a/2070434/3901618来学习更奇特的方法来找到最长的公共后缀字符串。