2

start と end を除く s 文字列のすべての可能な位置に char を挿入したい。例えば

abc
I want to have 
a-bc
ab-c
a-b-c

以下は私のテストですが、正しくありません:

procedure TForm1.Button2Click(Sender: TObject);
var
start, i,j,k,position,loop: integer;
laststart,lastend:integer;
c,item,stem:string;
str, prefix:string;
begin
str:='abcd';
memo1.clear;
memo1.Lines.Add(str);
laststart:=0;
lastend:=memo1.lines.count-1;
position:=0;
prefix:='';
loop:=0;
while loop<=length(str)-1 do
  begin

    for j:= laststart to lastend do
    begin
    item:=memo1.lines[j];
        for k:=length(item) downto 1 do
        begin
            if item[k]='-' then
            begin
            position:=j;
            break;
            end;
         end;  //for k
   prefix:=copy(item,1,position);
   stem:=copy(item,position+1, length(item));

        for start:=1 to length(stem)-1 do
        begin
        c:=prefix+copy(stem,1,start)+'-'+
        copy(stem, start+1,length(stem));
        memo1.lines.add(c);
        end;
     end; //for j
    laststart:=lastend+1;
    lastend:=memo1.Lines.Count-1;
inc(loop);
end; //end while

end;

それは出力します:

abcd
a-bcd
ab-cd
abc-d
a--bcd // not correct
a-b-cd
a-bc-d
ab--cd //incorrect
ab-c-d
abc--d  //incorrect
a--bc-d //incorrect

可能性のある最大の休憩はlenth(str)-1、abc->最も可能性のあるものはinsert 2 '-'(2回)だと思います。これは正しいです?

それを行うための他のより速い方法はありますか?

どうもありがとう。

4

4 に答える 4

9

再帰バージョン。

  procedure InsertSymbols(s: string; c: Char; Position: Integer = 1);
  var
    i: Integer;
  begin
    Memo1.Lines.Add(s);
    for i := Position to Length(s) - 1 do
      InsertSymbols(Copy(s, 1, i) + c + Copy(s, i + 1, Length(s) - i), c, i + 2);
  end;

begin
  InsertSymbols('Test', '-');
end;
于 2012-05-17T17:21:19.317 に答える
7

これは機能します:

procedure TForm4.Button1Click(Sender: TObject);
var
  S: string;
  N: integer;
  Marker: cardinal;
  MaxMarker: cardinal;
  Str: string;
  i: Integer;
begin
  S := Edit1.Text;
  N := length(S);
  Marker := 0;
  MaxMarker := 1 shl (N - 1) - 1;

  Memo1.Clear;
  Memo1.Lines.BeginUpdate;

  for Marker := 0 to MaxMarker do
  begin
    Str := S[1];
    for i := 2 to N do
    begin
      if (Marker shr (N-i)) and 1 <> 0 then
        Str := Str + '-';
      Str := Str + S[i];
    end;
    Memo1.Lines.Add(Str);
  end;

  Memo1.Lines.EndUpdate;
end;

スクリーンショット

ご覧のとおり、数値のバイナリ表現を使用して機能します。

t e s t
 0 0 0
 0 0 1
 0 1 0
 0 1 1
 1 0 0
 1 0 1
 1 1 0
 1 1 1
于 2012-05-17T12:20:06.893 に答える
1

なぜすべての難しい解決策があるのでしょうか? 文字列を 1 文字ずつ新しい文字列にコピーし、最後のハイフンを除いて間にハイフンを追加します。

于 2012-05-21T14:12:12.263 に答える
1

シリアル番号として使用する文字列を区切る必要がありました。コードは次のとおりです。

Function GetDashedKey(Key: string): string
const
  PartSize = 7;
var
  Indx: Integer;
  dashedKey : string;
begin

  repeat
    if Trim(dashedKey)<>'' then
      dashedKey := dashedKey + ' - ';

    if Length(Key) < PartSize then
    begin
      dashedKey := dashedKey + Key;
      Key := '';
    end
    else
    begin
      dashedKey := dashedKey + Copy(Key, 1, PartSize);
      Key := Copy(Key, PartSize + 1, Length(Key)-1);
    end;
  until Trim(Key) = '';

  Result := dashedKey;
end;
于 2012-10-24T06:39:54.993 に答える