#include <stdio.h>
#include <stdlib.h>
// myStrlen関数
int myStrlen(char s[]) {
int i = 0;
while (s[i] != '\0') {
i++;
}
return i;
}
// 回文を作成する関数
char *setPalindrome(char s[]) {
int m = myStrlen(s);
int i, j;
// 回文を作るために、最大で何文字一致するかを探す
for (i = 0; i < m; i++) {
// 前半部分 s[0:i] と後半部分 s[m-i:m] が一致するかを確認
int match = 1;
for (j = 0; j < m - i; j++) {
if (s[j] != s[m - 1 - j]) {
match = 0;
break;
}
}
// 完全に一致した場合
if (match) {
break;
}
}
// 必要なメモリを確保
char *tmp
= (char *)malloc(sizeof(char) * (2 * m
- i
+ 1)); if (tmp == NULL) {
return NULL;
}
// 回文の前半部分をそのままコピー
for (j = 0; j < m - i; j++) {
tmp[j] = s[j];
}
// 逆順で残りの部分を追加
for (j = 0; j < i; j++) {
tmp[m - i + j] = s[m - 1 - j];
}
tmp[2 * m - i] = '\0'; // 終端文字を追加
return tmp;
}
// メイン関数
int main() {
char nyuryoku[1024]; // 入力用バッファ
char *kaibun; // 回文を受け取るポインタ
scanf("%s", nyuryoku
); // 入力を受け取る
kaibun = setPalindrome(nyuryoku); // 回文を生成
if (kaibun != NULL) {
printf("%s\n -> %s\n", nyuryoku
, kaibun
); free(kaibun
); // 動的に確保したメモリを解放 }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8vIG15U3RybGVu6Zai5pWwCmludCBteVN0cmxlbihjaGFyIHNbXSkgewogICAgaW50IGkgPSAwOwogICAgd2hpbGUgKHNbaV0gIT0gJ1wwJykgewogICAgICAgIGkrKzsKICAgIH0KICAgIHJldHVybiBpOwp9CgovLyDlm57mlofjgpLkvZzmiJDjgZnjgovplqLmlbAKY2hhciAqc2V0UGFsaW5kcm9tZShjaGFyIHNbXSkgewogICAgaW50IG0gPSBteVN0cmxlbihzKTsKICAgIGludCBpLCBqOwoKICAgIC8vIOWbnuaWh+OCkuS9nOOCi+OBn+OCgeOBq+OAgeacgOWkp+OBp+S9leaWh+Wtl+S4gOiHtOOBmeOCi+OBi+OCkuaOouOBmQogICAgZm9yIChpID0gMDsgaSA8IG07IGkrKykgewogICAgICAgIC8vIOWJjeWNiumDqOWIhiBzWzA6aV0g44Go5b6M5Y2K6YOo5YiGIHNbbS1pOm1dIOOBjOS4gOiHtOOBmeOCi+OBi+OCkueiuuiqjQogICAgICAgIGludCBtYXRjaCA9IDE7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IG0gLSBpOyBqKyspIHsKICAgICAgICAgICAgaWYgKHNbal0gIT0gc1ttIC0gMSAtIGpdKSB7CiAgICAgICAgICAgICAgICBtYXRjaCA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgLy8g5a6M5YWo44Gr5LiA6Ie044GX44Gf5aC05ZCICiAgICAgICAgaWYgKG1hdGNoKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgIH0KCiAgICAvLyDlv4XopoHjgarjg6Hjg6Ljg6rjgpLnorrkv50KICAgIGNoYXIgKnRtcCA9IChjaGFyICopbWFsbG9jKHNpemVvZihjaGFyKSAqICgyICogbSAtIGkgKyAxKSk7CiAgICBpZiAodG1wID09IE5VTEwpIHsKICAgICAgICBwcmludGYoIkVSUk9SXG4iKTsKICAgICAgICByZXR1cm4gTlVMTDsKICAgIH0KCiAgICAvLyDlm57mlofjga7liY3ljYrpg6jliIbjgpLjgZ3jga7jgb7jgb7jgrPjg5Tjg7wKICAgIGZvciAoaiA9IDA7IGogPCBtIC0gaTsgaisrKSB7CiAgICAgICAgdG1wW2pdID0gc1tqXTsKICAgIH0KCiAgICAvLyDpgIbpoIbjgafmrovjgorjga7pg6jliIbjgpLov73liqAKICAgIGZvciAoaiA9IDA7IGogPCBpOyBqKyspIHsKICAgICAgICB0bXBbbSAtIGkgKyBqXSA9IHNbbSAtIDEgLSBqXTsKICAgIH0KCiAgICB0bXBbMiAqIG0gLSBpXSA9ICdcMCc7ICAvLyDntYLnq6/mloflrZfjgpLov73liqAKCiAgICByZXR1cm4gdG1wOwp9CgovLyDjg6HjgqTjg7PplqLmlbAKaW50IG1haW4oKSB7CiAgICBjaGFyIG55dXJ5b2t1WzEwMjRdOyAgLy8g5YWl5Yqb55So44OQ44OD44OV44KhCiAgICBjaGFyICprYWlidW47ICAvLyDlm57mlofjgpLlj5fjgZHlj5bjgovjg53jgqTjg7Pjgr8KICAgIHNjYW5mKCIlcyIsIG55dXJ5b2t1KTsgIC8vIOWFpeWKm+OCkuWPl+OBkeWPluOCiwoKICAgIGthaWJ1biA9IHNldFBhbGluZHJvbWUobnl1cnlva3UpOyAgLy8g5Zue5paH44KS55Sf5oiQCiAgICBpZiAoa2FpYnVuICE9IE5VTEwpIHsKICAgICAgICBwcmludGYoIiVzXG4gIC0+ICVzXG4iLCBueXVyeW9rdSwga2FpYnVuKTsKICAgICAgICBmcmVlKGthaWJ1bik7ICAvLyDli5XnmoTjgavnorrkv53jgZfjgZ/jg6Hjg6Ljg6rjgpLop6PmlL4KICAgIH0KCiAgICByZXR1cm4gMDsKfQo=