#include <stdio.h>
#define PREFECTURE_NUM 100
#define TRUE 1
#define FALSE 0
#define Num(a) sizeof(a)/sizeof(a[0])
typedef struct {
char head;
char tail;
int str_len;
} word;
void calc(int idx);
int calc_current(int idx);
word prefects[] = {
{'ア', 'ウ',1}, {'ア', 'ケ',1}, {'ア', 'タ',1},
{'イ', 'ロ',1}, {'エ', 'マ',1}, {'コ', 'レ',1},
{'テ', 'ミ',1}, {'オ', 'ノ',1}, {'タ', 'コ',12},
{'ヤ', 'キ',1}, {'ル', 'ワ',1}, {'サ', 'ワ',1},
{'コ', 'ミ',1}, {'コ', 'ワ',1}, {'マ', 'タ',1},
{'カ', 'ハ',1}, {'コ', 'ハ',1}, {'カ', 'ノ',1},
{'コ', 't',1}, {'シ', 'ハ',1}, {'カ', 'リ',1},
{'カ', 'ニ',1}, {'カ', 'サ',1}, {'シ', 'ノ',1},
{'ナ', 'タ',1}, {'ツ', 'ウ',1}, {'レ', 'ハ',1},
{'ラ', 'ケ',1}, {'コ', 'ミ',1}, {'ナ', 'カ',1},
{'ミ', 'ク',1}, {'ニ', 'モ',1}, {'ノ', 'キ',1},
{'リ', 'チ',1}, {'ニ', 'ハ',1}, {'コ', 'コ',1},
{'ヒ', 'キ',1}, {'ミ', 'カ',1}, {'ハ', 'マ',1},
{'ミ', 'チ',1}, {'ア', 'レ',1}, {'ミ', 'ハ',1},
{'キ', 'カ',1}, {'ユ', 'ユ',1}, {'チ', 'ア',1},
{'ヤ', 'セ',1}, {'ハ', 'ロ',1}
};
int pattern[PREFECTURE_NUM] = {0};
int *p_idx = pattern;
int max_len = 0;
int pattern_save[PREFECTURE_NUM];
int used[PREFECTURE_NUM];
void
pp()
{
int i;
for (i = 0; pattern_save[i] >= 0; i++)
printf("%d ", pattern_save
[i
]); }
void
save()
{
int i = PREFECTURE_NUM;
while (i--)
pattern_save[i] = pattern[i];
}
int
main()
{
int i;
for (i = 0; i < Num(prefects); i++)
used[i] = FALSE;
calc(-1);
printf("max string length: %d\n", max_len
); pp();
calc_current(max_len);
return 0;
}
void
calc(int idx)
{
int i, w;
for (i = 0; i < Num(prefects); i++) {
if (idx < 0)
calc(i);
else {
used[idx] = TRUE;
*p_idx++ = idx;
*p_idx = -1;
if (used[i] == FALSE &&
prefects[i].head == prefects[idx].tail)
calc(i);
used[idx] = FALSE;
p_idx--;
}
w = calc_current(p_idx - pattern + 1);
if (max_len < w) {
max_len = w;
save();
}
}
}
int
calc_current(int idx)
{
int r = 0;
while (idx--)
r += prefects[idx].str_len;
return r;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIFBSRUZFQ1RVUkVfTlVNIDEwMAojZGVmaW5lIFRSVUUgMQojZGVmaW5lIEZBTFNFIDAKI2RlZmluZSBOdW0oYSkgc2l6ZW9mKGEpL3NpemVvZihhWzBdKQoKdHlwZWRlZiBzdHJ1Y3QgewogIGNoYXIgICAgICAgICAgICBoZWFkOwogIGNoYXIgICAgICAgICAgICB0YWlsOwogIGludCAgICAgICAgICAgICBzdHJfbGVuOwp9ICAgICAgICAgICAgICAgd29yZDsKCnZvaWQgICAgICAgICAgICBjYWxjKGludCBpZHgpOwppbnQgICAgICAgICAgICAgY2FsY19jdXJyZW50KGludCBpZHgpOwoKd29yZCBwcmVmZWN0c1tdID0gewogIHsn772xJywgJ++9sycsMX0sIHsn772xJywgJ++9uScsMX0sIHsn772xJywgJ+++gCcsMX0sCiAgeyfvvbInLCAn776bJywxfSwgeyfvvbQnLCAn776PJywxfSwgeyfvvbonLCAn776aJywxfSwKICB7J+++gycsICfvvpAnLDF9LCB7J++9tScsICfvvoknLDF9LCB7J+++gCcsICfvvbonLDEyfSwKICB7J+++lCcsICfvvbcnLDF9LCB7J+++mScsICfvvpwnLDF9LCB7J++9uycsICfvvpwnLDF9LAogIHsn7726JywgJ+++kCcsMX0sIHsn7726JywgJ+++nCcsMX0sIHsn776PJywgJ+++gCcsMX0sCiAgeyfvvbYnLCAn776KJywxfSwgeyfvvbonLCAn776KJywxfSwgeyfvvbYnLCAn776JJywxfSwKICB7J++9uicsICd0JywxfSwgeyfvvbwnLCAn776KJywxfSwgeyfvvbYnLCAn776YJywxfSwKICB7J++9ticsICfvvoYnLDF9LCB7J++9ticsICfvvbsnLDF9LCB7J++9vCcsICfvvoknLDF9LAogIHsn776FJywgJ+++gCcsMX0sIHsn776CJywgJ++9sycsMX0sIHsn776aJywgJ+++iicsMX0sCiAgeyfvvpcnLCAn7725JywxfSwgeyfvvbonLCAn776QJywxfSwgeyfvvoUnLCAn7722JywxfSwKICB7J+++kCcsICfvvbgnLDF9LCB7J+++hicsICfvvpMnLDF9LCB7J+++iScsICfvvbcnLDF9LAogIHsn776YJywgJ+++gScsMX0sIHsn776GJywgJ+++iicsMX0sIHsn7726JywgJ++9uicsMX0sCiAgeyfvvosnLCAn7723JywxfSwgeyfvvpAnLCAn7722JywxfSwgeyfvvoonLCAn776PJywxfSwKICB7J+++kCcsICfvvoEnLDF9LCB7J++9sScsICfvvponLDF9LCB7J+++kCcsICfvvoonLDF9LAogIHsn7723JywgJ++9ticsMX0sIHsn776VJywgJ+++lScsMX0sIHsn776BJywgJ++9sScsMX0sCiAgeyfvvpQnLCAn772+JywxfSwgeyfvvoonLCAn776bJywxfQp9OwoKaW50ICAgICAgICAgICAgIHBhdHRlcm5bUFJFRkVDVFVSRV9OVU1dID0gezB9OwppbnQgICAgICAgICAgICAqcF9pZHggPSBwYXR0ZXJuOwppbnQgICAgICAgICAgICAgbWF4X2xlbiA9IDA7CmludCAgICAgICAgICAgICBwYXR0ZXJuX3NhdmVbUFJFRkVDVFVSRV9OVU1dOwppbnQgICAgICAgICAgICAgdXNlZFtQUkVGRUNUVVJFX05VTV07Cgp2b2lkCnBwKCkKewogIGludCAgICAgICAgICAgICBpOwogIHByaW50ZigicGF0dGVybjogIik7CiAgZm9yIChpID0gMDsgcGF0dGVybl9zYXZlW2ldID49IDA7IGkrKykKICAgIHByaW50ZigiJWQgIiwgcGF0dGVybl9zYXZlW2ldKTsKfQoKdm9pZApzYXZlKCkKewogIGludCAgICAgICAgICAgICBpID0gUFJFRkVDVFVSRV9OVU07CiAgd2hpbGUgKGktLSkKICAgIHBhdHRlcm5fc2F2ZVtpXSA9IHBhdHRlcm5baV07Cn0KCmludAptYWluKCkKewogIGludCAgICAgICAgICAgICBpOwoKICBmb3IgKGkgPSAwOyBpIDwgTnVtKHByZWZlY3RzKTsgaSsrKQogICAgdXNlZFtpXSA9IEZBTFNFOwogIGNhbGMoLTEpOwogIHByaW50ZigibWF4IHN0cmluZyBsZW5ndGg6ICVkXG4iLCBtYXhfbGVuKTsKICBwcCgpOwoKICBjYWxjX2N1cnJlbnQobWF4X2xlbik7CgogIHJldHVybiAwOwp9Cgp2b2lkCmNhbGMoaW50IGlkeCkKewogIGludCAgICAgICAgICAgICBpLCB3OwoKICBmb3IgKGkgPSAwOyBpIDwgTnVtKHByZWZlY3RzKTsgaSsrKSB7CiAgICBpZiAoaWR4IDwgMCkKICAgICAgY2FsYyhpKTsKICAgIGVsc2UgewogICAgICB1c2VkW2lkeF0gPSBUUlVFOwogICAgICAqcF9pZHgrKyA9IGlkeDsKICAgICAgKnBfaWR4ID0gLTE7CiAgICAgIGlmICh1c2VkW2ldID09IEZBTFNFICYmCiAgICAgICAgICBwcmVmZWN0c1tpXS5oZWFkID09IHByZWZlY3RzW2lkeF0udGFpbCkKICAgICAgICBjYWxjKGkpOwogICAgICB1c2VkW2lkeF0gPSBGQUxTRTsKICAgICAgcF9pZHgtLTsKICAgIH0KICAgIHcgPSBjYWxjX2N1cnJlbnQocF9pZHggLSBwYXR0ZXJuICsgMSk7CiAgICBpZiAobWF4X2xlbiA8IHcpIHsKICAgICAgbWF4X2xlbiA9IHc7CiAgICAgIHNhdmUoKTsKICAgIH0KICB9Cn0KCmludApjYWxjX2N1cnJlbnQoaW50IGlkeCkKewogIGludCAgICAgICAgICAgICByID0gMDsKCiAgd2hpbGUgKGlkeC0tKQogICAgciArPSBwcmVmZWN0c1tpZHhdLnN0cl9sZW47CgogIHJldHVybiByOwp9