fork(3) download
  1. program ideone;
  2. Uses sysutils;
  3. const
  4. Samples: array [0 .. 9] of AnsiString = ('zero', 'one', 'two', 'three',
  5. 'four', 'five', 'six', 'seven', 'eight', 'nine');
  6. var
  7. BigCount, SamCount: array [Ord('a') .. Ord('z')] of Integer;
  8. Big, OutS: AnsiString;
  9. i, j, k, n, r: Integer;
  10. Res: Boolean;
  11. ch: AnsiChar;
  12. begin
  13. Randomize;
  14. for r := 1 to 4 do begin
  15. n := 2 + random(9);
  16. Big := '';
  17. OutS := '';
  18. for i := 0 to n - 1 do
  19. Big := Big + Samples[Random(10)];
  20. Writeln(Big);
  21. //Pascal strings numbered from 1
  22. for i := Length(Big) downto 2 do begin //shuffle chars
  23. j := 1 + random(i);
  24. ch := Big[i];
  25. Big[i] := Big[j];
  26. Big[j] := ch;
  27. end;
  28. Writeln(Big);
  29.  
  30. FillChar(BigCount, SizeOf(BigCount), 0); //string char counts
  31. for i := 1 to Length(Big) do
  32. Inc(BigCount[Ord(Big[i])]);
  33.  
  34. for k := 0 to 4 do begin //even numbers
  35. j := 2 * k;
  36. FillChar(SamCount, SizeOf(SamCount), 0); //sample[j] char counts
  37. for i := 1 to Length(Samples[j]) do
  38. Inc(SamCount[Ord(Samples[j][i])]);
  39. Res := True;
  40. while Res do begin
  41. for i := Ord('a') to Ord('z') do
  42. if SamCount[i] > BigCount[i] then begin
  43. Res := False;
  44. Break;
  45. end;
  46. if Res then begin //word found, just add index
  47. OutS := OutS + IntToStr(j);
  48. for i := Ord('a') to Ord('z') do
  49. BigCount[i] := BigCount[i] - SamCount[i];
  50. end;
  51. end;
  52. end;
  53.  
  54. for k := 0 to 4 do begin //odd numbers
  55. j := 2 * k + 1;
  56. FillChar(SamCount, SizeOf(SamCount), 0);
  57. for i := 1 to Length(Samples[j]) do
  58. Inc(SamCount[Ord(Samples[j][i])]);
  59. Res := True;
  60. while Res do begin
  61. for i := Ord('a') to Ord('z') do
  62. if SamCount[i] > BigCount[i] then begin
  63. Res := False;
  64. Break;
  65. end;
  66. if Res then begin //word found, insert index in the right place in sorted order
  67. n := Length(OutS);
  68. ch := AnsiChar(j + Ord('0'));
  69. while (n > 0) and (ch < OutS[n]) do
  70. Dec(n);
  71. Insert(ch, OutS, n + 1);
  72. for i := Ord('a') to Ord('z') do
  73. BigCount[i] := BigCount[i] - SamCount[i];
  74. end;
  75. end;
  76. end;
  77. Writeln(OutS);
  78. end;
  79. end.
Success #stdin #stdout 0s 4276KB
stdin
Standard input is empty
stdout
nineninefourseveneighttwo
reeniuietnnvngefeiwsthnoo
247899
nineninefoursix
reninieunnoxfsi
4699
threethreeonenine
eehneernitteorhne
1339
fivefivezeroninesixonesixzerofour
efvferfoiiuirxeiznnzrvooseexineos
001455669