fork download
  1. #include <stdio.h>
  2.  
  3. #define PREFECTURE_NUM 100
  4. #define TRUE 1
  5. #define FALSE 0
  6. #define Num(a) sizeof(a)/sizeof(a[0])
  7.  
  8. typedef struct {
  9. char head;
  10. char tail;
  11. int str_len;
  12. } word;
  13.  
  14. void calc(int idx);
  15. int calc_current(int idx);
  16.  
  17. word prefects[] = {
  18. {'ア', 'ウ',1}, {'ア', 'ケ',1}, {'ア', 'タ',1},
  19. {'イ', 'ロ',1}, {'エ', 'マ',1}, {'コ', 'レ',1},
  20. {'テ', 'ミ',1}, {'オ', 'ノ',1}, {'タ', 'コ',12},
  21. {'ヤ', 'キ',1}, {'ル', 'ワ',1}, {'サ', 'ワ',1},
  22. {'コ', 'ミ',1}, {'コ', 'ワ',1}, {'マ', 'タ',1},
  23. {'カ', 'ハ',1}, {'コ', 'ハ',1}, {'カ', 'ノ',1},
  24. {'コ', 't',1}, {'シ', 'ハ',1}, {'カ', 'リ',1},
  25. {'カ', 'ニ',1}, {'カ', 'サ',1}, {'シ', 'ノ',1},
  26. {'ナ', 'タ',1}, {'ツ', 'ウ',1}, {'レ', 'ハ',1},
  27. {'ラ', 'ケ',1}, {'コ', 'ミ',1}, {'ナ', 'カ',1},
  28. {'ミ', 'ク',1}, {'ニ', 'モ',1}, {'ノ', 'キ',1},
  29. {'リ', 'チ',1}, {'ニ', 'ハ',1}, {'コ', 'コ',1},
  30. {'ヒ', 'キ',1}, {'ミ', 'カ',1}, {'ハ', 'マ',1},
  31. {'ミ', 'チ',1}, {'ア', 'レ',1}, {'ミ', 'ハ',1},
  32. {'キ', 'カ',1}, {'ユ', 'ユ',1}, {'チ', 'ア',1},
  33. {'ヤ', 'セ',1}, {'ハ', 'ロ',1}
  34. };
  35.  
  36. int pattern[PREFECTURE_NUM] = {0};
  37. int *p_idx = pattern;
  38. int max_len = 0;
  39. int pattern_save[PREFECTURE_NUM];
  40. int used[PREFECTURE_NUM];
  41.  
  42. void
  43. pp()
  44. {
  45. int i;
  46. printf("pattern: ");
  47. for (i = 0; pattern_save[i] >= 0; i++)
  48. printf("%d ", pattern_save[i]);
  49. }
  50.  
  51. void
  52. save()
  53. {
  54. int i = PREFECTURE_NUM;
  55. while (i--)
  56. pattern_save[i] = pattern[i];
  57. }
  58.  
  59. int
  60. main()
  61. {
  62. int i;
  63.  
  64. for (i = 0; i < Num(prefects); i++)
  65. used[i] = FALSE;
  66. calc(-1);
  67. printf("max string length: %d\n", max_len);
  68. pp();
  69.  
  70. calc_current(max_len);
  71.  
  72. return 0;
  73. }
  74.  
  75. void
  76. calc(int idx)
  77. {
  78. int i, w;
  79.  
  80. for (i = 0; i < Num(prefects); i++) {
  81. if (idx < 0)
  82. calc(i);
  83. else {
  84. used[idx] = TRUE;
  85. *p_idx++ = idx;
  86. *p_idx = -1;
  87. if (used[i] == FALSE &&
  88. prefects[i].head == prefects[idx].tail)
  89. calc(i);
  90. used[idx] = FALSE;
  91. p_idx--;
  92. }
  93. w = calc_current(p_idx - pattern + 1);
  94. if (max_len < w) {
  95. max_len = w;
  96. save();
  97. }
  98. }
  99. }
  100.  
  101. int
  102. calc_current(int idx)
  103. {
  104. int r = 0;
  105.  
  106. while (idx--)
  107. r += prefects[idx].str_len;
  108.  
  109. return r;
  110. }
Success #stdin #stdout 0.03s 1720KB
stdin
Standard input is empty
stdout
max string length: 29
pattern: 29 17 32 42 20 33 44 40 26 38 14 8 35 12 37 21 34 46