字符处排列组合 delphi

/// <summary>
/// 字符串是否存在于“指定升序数字数字组成的任意位数字中”
///  eg:
///  ASet=178 可以组成的任意位升序数字是:1、7、8、17、18、78、178
///  若Aval是其中一个则返回true,否则返回的是false
/// </summary>
/// <param name="AVal">带判断的值</param>
/// <param name="ASet">升序的数字字符串</param>
function Check_IsAllInAscNumberSetByAnyBit(const AVal, ASet: string): boolean;
  function Combin(mStrings: TStrings; mStr: string; mLen: Integer): Boolean;
    procedure fCombin(mSubStr: string; mPath: string);
    var
      I: Integer;
      S: string;
      b: Boolean;
      lst: TStringList;
    begin
      if Length(mPath) >= mLen then
      begin
        lst := TStringList.Create;
        for i := 1 to Length(mPath) do
        begin
          lst.Add(mPath[i]);
        end;
        lst.Sort;
        b := True;
        for i := 0 to lst.Count - 1 do
        begin
          if lst[i] <> mPath[i+1] then
          begin
            b := False;
            Break;
          end;
        end;
        lst.free;  
        if b then
          mStrings.Add(mPath);
      end
      else
      for I := 1 to Length(mSubStr) do
      begin
        S := mSubStr;
        Delete(S, I, 1);
        fCombin(S, mPath + mSubStr[I]);
      end;
    end;
  begin
    Result := False;
    if not Assigned(mStrings) then Exit;
    mStrings.BeginUpdate;
    try
      mStrings.Clear;
      fCombin(mStr, '');
    finally
      mStrings.EndUpdate;
    end;
    Result := True;
  end;
var
  i: Integer;
  lst: TStringList;
begin
  Result := False;
  lst := TStringList.Create;
  try
    Combin(lst, ASet, Length(AVal));
    for i := 0 to lst.Count - 1 do
    begin
      if AVal = lst[i] then
      begin
        Result := True;
        Exit;
      end;
    end;
  finally
    lst.Free;
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  if Check_IsAllInAscNumberSetByAnyBit(edt1.Text, '178') then
    Caption := 'OK'
  else
    Caption := 'Bad';
end;

转载于:https://my.oschina.net/u/582827/blog/322895

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值