风也温柔

计算机科学知识库

java字符串匹配度算法 字符串平铺算法

  我认为这应该适用于两个字符串的平铺,并且比使用和的当前实现更有效。从概念上讲,我遍历'left'字符串中的字符java字符串匹配度算法 字符串平铺算法,并将它们与'right'字符串中的字符进行比较。如果两个字符匹配,我将移动到右侧字符串中的下一个字符。根据首先到达末尾的字符串,以及最后比较的字符是否匹配,可以识别其中一个可能的平铺情况。我没有想过要改善拼贴两个以上字符串的时间复杂度。作为多个字符串的一个小注释,下面的算法很容易扩展到一次检查单个“左”字符串的平铺java字符串匹配度算法,多个“正确”字符串java字符串匹配度算法,这可能会阻止在字符串上多余的循环,如果你试图通过尝试所有可能性,找出是做(“ABC”,“BCX”,“XYZ”)还是(“ABC”,“XYZ”,BCX“)。

  <pre>string Tile(string a, string b)
{

// Try both orderings of a and b,
// since TileLeftToRight is not commutative.
string ab = TileLeftToRight(a, b);
if (ab != "")
    return ab;
return TileLeftToRight(b, a);
// Alternatively you could return whichever
// of the two results is longest, for cases
// like ("ABC" "BCABC").

}
string TileLeftToRight(string left, string right)
{

int i = 0;
int j = 0;
while (true)
{
    if (left[i] != right[j])
    {
        i++;
        if (i >= left.Length)
            return "";
    }
    else
    {
        i++;
        j++;
        if (i >= left.Length)
            return left + right.Substring(j);
        if (j >= right.Length)
            return left;
    }
}

}
</pre>

  文章来源:https://www.orcode.com/question/319385_kfd776.html