关于网友提出的“ 求一算法两字符串abcdef123与xbcyzf123最大相同字符串数”问题疑问,本网通过在网上对“ 求一算法两字符串abcdef123与xbcyzf123最大相同字符串数”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 求一算法两字符串abcdef123与xbcyzf123最大相同字符串数
描述: 字符串abcdef123与
xbcyzf123相同位置下相似的有bc和f123.我想让其返回的数据相同数为4而不是为2
解决方案1: var str, Ms, str1: string;
i, j, tmpmax: integer;
begin
i:=0;
str := 'abcdef123';
str1 := 'xbcyzf123';
for j := 0 to Min(length(str),length(str1))-1 do
begin
if str[j]=str1[j] then
begin
inc(i);
tmpMax:=i;
end
else if str[j]<>str1[j] then
begin
tmpMax:=Max(tmpMax,i);
i:=0;
end
end;
showmessage(inttostr(tmpmax));
end;
解决方案2: 写的有点烂,凑乎着用
var str, Ms, str1: string;
maxl, i, j, tmpmax: integer;
begin
str := 'abcdef123';
str1 := 'xbcyzf123';
for j := 1 to length(str) do
begin
for i := length(str) downto 1 do
begin
ms := copy(str, j, i);
if pos(ms, str1) > 0 then
begin
maxl := length(ms);
if tmpmax < MAXL then
tmpmax := maxl;
end;
end;
end;
showmessage(inttostr(tmpmax));
end;
解决方案3:
a b c d e f 1 2 3
x - - - - - - - - -
b - * - - - - - - -
c - - * - - - - - -
y - - - - - - - - -
z - - - - - - - - -
f - - - - - * - - -
1 - - - - - - * - -
2 - - - - - - - * -
3 - - - - - - - - *
斜着的 * 线最长的就是。
以上介绍了“ 求一算法两字符串abcdef123与xbcyzf123最大相同字符串数”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2323333.html