fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class PatternGenerator2Exception {};
  6.  
  7. class PatternGenerator2
  8. {
  9. private:
  10. int *_src;
  11. int *_flag;
  12. int *_indexes;
  13. int *_list;
  14. int _srcsize;
  15. int _len;
  16. int _count;
  17. PatternGenerator2(int srcsize, int len, int c)
  18. : _srcsize(srcsize), _len(len), _count(c) {
  19.  
  20. if (_srcsize < 2 || _len < 2 || _srcsize < _len) {
  21. throw PatternGenerator2Exception();
  22. }
  23. _src = new int[_srcsize];
  24. _flag = new int[_srcsize];
  25.  
  26. _list = new int[_len];
  27. _indexes = new int[len];
  28.  
  29. for (int i = 0; i < _srcsize; i++) {
  30. _flag[i] = 0;
  31. }
  32. }
  33. public:
  34.  
  35. PatternGenerator2(int srcsize, int len)
  36. : PatternGenerator2(srcsize, len, 0) {
  37.  
  38. for (int i = 0; i < _srcsize; i++) {
  39. _src[i] = i + 1;
  40. }
  41.  
  42. reset();
  43. }
  44.  
  45. PatternGenerator2(int size)
  46. : PatternGenerator2(size, size) {
  47. }
  48.  
  49. PatternGenerator2(const int *sortedsrc, int srcsize, int len)
  50. : PatternGenerator2(srcsize, len, 0) {
  51.  
  52. memcpy(_src, sortedsrc, sizeof(int) * _srcsize);
  53.  
  54. reset();
  55. }
  56.  
  57. ~PatternGenerator2() {
  58. delete [] _src;
  59. delete [] _flag;
  60. delete [] _list;
  61. delete [] _indexes;
  62. }
  63.  
  64. int length() const { return _len; }
  65. int sourcesize() const { return _srcsize; }
  66. int count() const { return _count; }
  67.  
  68. void reset() {
  69. for (int i = 0; i < _len; i++) {
  70. _flag[_indexes[i]] = 0;
  71. }
  72. for (int i = 0; i < _len; i++) {
  73. _indexes[i] = i;
  74. _list[i] = _src[_indexes[i]];
  75. _flag[_indexes[i]] = 1;
  76. }
  77. _count = 0;
  78. }
  79.  
  80. int hasNext() {
  81. if (_count == 0) {
  82. return 1;
  83. }
  84. for (int i = 0; i < _len; i++) {
  85. if (_src[_indexes[i]] != _src[_srcsize - i - 1]) {
  86. return 1;
  87. }
  88. }
  89. return 0;
  90. }
  91.  
  92. const int* next() {
  93.  
  94. if (_count == 0) {
  95. _count++;
  96. return _list;
  97. }
  98.  
  99. int i, j, b, f;
  100.  
  101. i = _len - 1;
  102. _flag[_indexes[i]] = 0;
  103.  
  104. f = 1;
  105.  
  106. for (;;) {
  107. if (f) {
  108. b = _src[_indexes[i]];
  109. for (j = _indexes[i] + 1; j < _srcsize; j++) {
  110. if (_flag[j] == 0 && _src[j] != b) {
  111. break;
  112. }
  113. }
  114. } else {
  115. for (j = 0; j < _srcsize; j++) {
  116. if (_flag[j] == 0) {
  117. break;
  118. }
  119. }
  120. f = 1;
  121. }
  122. if (j == _srcsize) {
  123. i--;
  124. if (i < 0) {
  125. reset();
  126. break;
  127. } else {
  128. _flag[_indexes[i]] = 0;
  129. }
  130. } else {
  131. _indexes[i] = j;
  132. _list[i] = _src[_indexes[i]];
  133. _flag[_indexes[i]] = 1;
  134. i++;
  135. if (i == _len) {
  136. // kakutei
  137. break;
  138. } else {
  139. f = 0;
  140. }
  141. }
  142. }
  143. _count++;
  144. return _list;
  145. }
  146. };
  147.  
  148. int nCr(int n, int r) {
  149. if (n < 0 || r < 0 || n < r) {
  150. return 0;
  151. }
  152. if (n - r < r) {
  153. r = n - r;
  154. }
  155. int c = 1;
  156. for (int i = 1; i <= r; i++) {
  157. c *= n;
  158. c /= i;
  159. n--;
  160. }
  161. return c;
  162. }
  163.  
  164. int nPr(int n, int r) {
  165. if (n < 0 || r < 0 || n < r) {
  166. return 0;
  167. }
  168. int p = 1;
  169. while (r > 0) {
  170. p *= n;
  171. n--;
  172. r--;
  173. }
  174. return p;
  175. }
  176.  
  177. int main() {
  178.  
  179. int src[] = { 1, 2, 2, 5};
  180. int len = sizeof(src) / sizeof(src[0]);
  181. PatternGenerator2 gen(4);
  182. PatternGenerator2 gen2(5, 3);
  183. PatternGenerator2 gen3(src, len, len);
  184.  
  185. for (int i = 0; i < 25; i++) {
  186. const int *k = gen.next();
  187. cout.fill('_');
  188. cout.width(8);
  189. cout << gen.count() << ": ";
  190. for (int j = 0; j < gen.length(); j++) {
  191. cout << "_ABCDEFGHIJKLMNOPQRSTUVWXYZ"[k[j]];
  192. }
  193. cout << endl;
  194. }
  195.  
  196. cout << endl;
  197.  
  198. gen.reset();
  199.  
  200. while (gen.hasNext()) {
  201. const int *k = gen.next();
  202. cout.fill('.');
  203. cout.width(8);
  204. cout << gen.count() << ": ";
  205. for (int j = 0; j < gen.length(); j++) {
  206. cout << "_ABCDEFGHIJKLMNOPQRSTUVWXYZ"[k[j]];
  207. }
  208. cout << endl;
  209. }
  210.  
  211. cout << endl << endl;;
  212.  
  213. while (gen2.hasNext()) {
  214. const int *k = gen2.next();
  215. cout.fill('*');
  216. cout.width(8);
  217. cout << gen2.count() << ": ";
  218. for (int j = 0; j < gen2.length(); j++) {
  219. cout << "_ABCDEFGHIJKLMNOPQRSTUVWXYZ"[k[j]];
  220. }
  221. cout << endl;
  222. }
  223.  
  224. cout << endl << endl;
  225.  
  226. while (gen3.hasNext()) {
  227. const int *k = gen3.next();
  228. cout.fill('#');
  229. cout.width(8);
  230. cout << gen3.count() << ": ";
  231. for (int j = 0; j < gen3.length(); j++) {
  232. cout << k[j];
  233. }
  234. cout << endl;
  235. }
  236.  
  237. return 0;
  238. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
_______1: ABCD
_______2: ABDC
_______3: ACBD
_______4: ACDB
_______5: ADBC
_______6: ADCB
_______7: BACD
_______8: BADC
_______9: BCAD
______10: BCDA
______11: BDAC
______12: BDCA
______13: CABD
______14: CADB
______15: CBAD
______16: CBDA
______17: CDAB
______18: CDBA
______19: DABC
______20: DACB
______21: DBAC
______22: DBCA
______23: DCAB
______24: DCBA
_______1: ABCD

.......1: ABCD
.......2: ABDC
.......3: ACBD
.......4: ACDB
.......5: ADBC
.......6: ADCB
.......7: BACD
.......8: BADC
.......9: BCAD
......10: BCDA
......11: BDAC
......12: BDCA
......13: CABD
......14: CADB
......15: CBAD
......16: CBDA
......17: CDAB
......18: CDBA
......19: DABC
......20: DACB
......21: DBAC
......22: DBCA
......23: DCAB
......24: DCBA


*******1: ABC
*******2: ABD
*******3: ABE
*******4: ACB
*******5: ACD
*******6: ACE
*******7: ADB
*******8: ADC
*******9: ADE
******10: AEB
******11: AEC
******12: AED
******13: BAC
******14: BAD
******15: BAE
******16: BCA
******17: BCD
******18: BCE
******19: BDA
******20: BDC
******21: BDE
******22: BEA
******23: BEC
******24: BED
******25: CAB
******26: CAD
******27: CAE
******28: CBA
******29: CBD
******30: CBE
******31: CDA
******32: CDB
******33: CDE
******34: CEA
******35: CEB
******36: CED
******37: DAB
******38: DAC
******39: DAE
******40: DBA
******41: DBC
******42: DBE
******43: DCA
******44: DCB
******45: DCE
******46: DEA
******47: DEB
******48: DEC
******49: EAB
******50: EAC
******51: EAD
******52: EBA
******53: EBC
******54: EBD
******55: ECA
******56: ECB
******57: ECD
******58: EDA
******59: EDB
******60: EDC


#######1: 1225
#######2: 1252
#######3: 1522
#######4: 2125
#######5: 2152
#######6: 2215
#######7: 2251
#######8: 2512
#######9: 2521
######10: 5122
######11: 5212
######12: 5221