fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6. private static char[] arr = new char[] { 'a', 'c', 'b', 'd' };
  7.  
  8. private static Dictionary<char, int> dic = new Dictionary<char, int>()
  9. {
  10. { 'a', 0 }, { 'c', 1 }, { 'b', 2 }, { 'd', 3 }
  11. };
  12.  
  13. static void Main(string[] args)
  14. {
  15. Console.WriteLine("Example no 1:");
  16. Display(Generate(1));
  17. Console.WriteLine();
  18.  
  19. Console.WriteLine("Example no 2:");
  20. Display(Generate(2));
  21. Console.WriteLine();
  22.  
  23. Console.WriteLine("Example no 3:");
  24. Display(Generate("ab", 5));
  25. Console.WriteLine();
  26.  
  27. Console.WriteLine("Example no 4:");
  28. Display(Generate("dc", 10));
  29. Console.WriteLine();
  30.  
  31. Console.WriteLine("Example no 5:");
  32. Display(Generate("ab", 0));
  33. Console.WriteLine();
  34.  
  35. Console.WriteLine("Example no 6:");
  36. Display(Generate(3));
  37. Console.WriteLine();
  38. }
  39.  
  40. public static void Display(List<string> list)
  41. {
  42. list.ForEach(i => Console.Write("{0} ", i));
  43. }
  44.  
  45. public static List<string> Generate(int length)
  46. {
  47. char[] seed = new char[length];
  48. for (int i = 0; i < length; i++)
  49. seed[i] = arr[0];
  50.  
  51. int count = -1;
  52. checked { count = (int)Math.Pow(arr.Length, length); }
  53.  
  54. return Generate(new string(seed), count, true);
  55. }
  56.  
  57. public static List<string> Generate(string seed, int count)
  58. {
  59. return Generate(seed, count, false);
  60. }
  61.  
  62. private static List<string> Generate(string seed, int count, bool includeSeed)
  63. {
  64. List<string> result = new List<string>();
  65. if (includeSeed)
  66. result.Add(seed);
  67.  
  68. if (result.Count < count)
  69. Generate(seed.ToCharArray(), result, count);
  70.  
  71. return result;
  72. }
  73.  
  74. private static void Generate(char[] previous, List<string> result, int count)
  75. {
  76. char[] current = new char[previous.Length + 1];
  77.  
  78. bool copy = false;
  79. for (int i = previous.Length - 1; i >= 0; i--)
  80. {
  81. if (copy)
  82. current[i + 1] = previous[i];
  83. else
  84. {
  85. int index = dic[previous[i]];
  86.  
  87. if (index < arr.Length - 1)
  88. {
  89. current[i + 1] = arr[index + 1];
  90. copy = true;
  91. }
  92. else
  93. current[i + 1] = arr[0];
  94. }
  95. }
  96.  
  97. if (copy)
  98. result.Add(new string(current, 1, current.Length - 1));
  99. else
  100. {
  101. current[0] = arr[0];
  102. result.Add(new string(current));
  103. }
  104.  
  105. if (result.Count < count)
  106. Generate(result[result.Count - 1].ToCharArray(), result, count);
  107. }
  108. }
  109.  
Success #stdin #stdout 0.04s 37128KB
stdin
Standard input is empty
stdout
Example no 1:
a c b d 
Example no 2:
aa ac ab ad ca cc cb cd ba bc bb bd da dc db dd 
Example no 3:
ad ca cc cb cd 
Example no 4:
db dd aaa aac aab aad aca acc acb acd 
Example no 5:

Example no 6:
aaa aac aab aad aca acc acb acd aba abc abb abd ada adc adb add caa cac cab cad cca ccc ccb ccd cba cbc cbb cbd cda cdc cdb cdd baa bac bab bad bca bcc bcb bcd bba bbc bbb bbd bda bdc bdb bdd daa dac dab dad dca dcc dcb dcd dba dbc dbb dbd dda ddc ddb ddd