結果

問題 No.238 Mr. K's Another Gift
ユーザー Yang33Yang33
提出日時 2018-08-25 15:25:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,184 bytes
コンパイル時間 1,696 ms
コンパイル使用メモリ 168,448 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-08 11:10:48
合計ジャッジ時間 3,719 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/08/25  Problem: yukicoder 238  / Link: http://yukicoder.me/problems/no/238  ----- */
/* ------問題------

(この問題は Codeforces Round #286 (Div. 2) の問題A の入力の制約を変更したものです。)

K氏が英小文字からなる文字列 s をくれた。これに英小文字をちょうど一つ挿入して回文にしてほしいそうだ。
(回文とは、前から読んでも後ろから読んでも文字が同じ順番で出現するような文字列である。
例えば "noon", "testset", "a" はどれも回文であるが、 "test", "kitayuta" は回文ではない。)

挿入する文字としてどの英小文字を使ってもよく、それを s のどの位置に挿入してもよい。
s の先頭や末尾への挿入も可能である。
与えられた文字列 s がすでに回文であっても、一文字を挿入しなくてはならない。

s に英小文字を一つ挿入し、その結果文字列を回文にすることが可能ならば、そのようにして得られる回文を一つ出力せよ。
そうでなければ、 "NA" (引用符除く)と大文字で出力せよ。
得られる回文が複数存在する場合は、そのうちどれを出力してもよい。

-----問題ここまで----- */
/* -----解説等-----

回文か、一個なにかが異なる回文もどきは回分にできる

----解説ここまで---- */


int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	string s; cin >> s;
	int ok = 1;
	FOR(i, 0, SZ(s) / 2) {
		int j = SZ(s) - i - 1;
		ok &= s[i] == s[j];
	}
	if (ok) {
		cout << s.substr(0, SZ(s) / 2) + string(1, s[SZ(s) / 2]) + s.substr(SZ(s) / 2) << endl;
		return 0;
	}
	int diffcnt = 0;
	auto check = [&s](int l, int r) {
		int len = r - l+1;
		int ok = 1;
		FOR(i, 0, len/2) {
			int L = l + i; int R = r - i;
			ok &= s[L] == s[R];
		}
		return ok;
	};
	FOR(i, 0, SZ(s) / 2) {
		int j = SZ(s) - i - 1;
		if (s[i] != s[j]) {
			diffcnt++;
			if (check(i, j - 1)) {
				cout << s.substr(0, i) + string(1, s[j]) + s.substr(i) << endl;
				return 0;
			}
			if (check(i + 1, j)) {
				cout << s.substr(0, j + 1) + string(1, s[i]) + s.substr(j + 1) << endl;
				return 0;
			}
			diffcnt++;
			break;
		}
	}
		cout << "NA" << endl;

	return 0;
}
0