function StringMaxCntInAry1(str1:string;strSour:TStrings):integer;
var
    i,iMaxCnt,iCnt:Integer;
    bFind,bStart:boolean;
begin
    iCnt:=0;
    iMaxCnt:=0;
    bFind:=false;
    bStart:=false;
    for i:=strSour.Count-1 downto 0 do
    begin
        if Pos(str1,strSour.Strings[i])>0  then
        begin
            iMaxCnt:=Max(iMaxCnt,iCnt);
            iCnt:=0;
            bFind:=false;
            bStart:=true;
        end else
            if bStart then
                bFind:=true;
            
        if bFind then
            inc(iCnt);
    end;
    Result:=iMaxCnt;
end;
3
8
3
7
8
9
3
4
9
0
求各自的最大间隔,0为9,1、2为10,3为3,

解决方案 »

  1.   


    function StringMaxCntInAry1(str1:string;strSour:TStrings):integer;
    var 
        i,iMaxCnt,iCnt:Integer; 
        bFind,bStart:boolean; 
    begin 
        iCnt:=0; 
        iMaxCnt:=0;
        bFind:=false; 
        bStart:=false; 
        for i:=strSour.Count-1 downto 0 do
        begin 
            if Pos(str1,strSour.Strings[i])>0  then 
            begin
                iMaxCnt:=Max(iMaxCnt,iCnt);
                iCnt:=0;
                bFind:=false;
                bStart:=true; 
            end else 
                if bStart then
                    bFind:=true;
                
            if bFind then 
                inc(iCnt);
        end;
        if (iCnt=0) and (iMaxCnt=0) then
            Result:=10
        else if iMaxCnt=0 then
            Result:=iCnt
        else
            Result:=iMaxCnt;
    end;