結果

問題 No.238 Mr. K's Another Gift
ユーザー koba-e964
提出日時 2015-07-06 16:36:33
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 747 bytes
コンパイル時間 528 ms
コンパイル使用メモリ 64,796 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-08 01:20:45
合計ジャッジ時間 2,072 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;

bool pal(const string &s) {
  string t(s);
  reverse(t.begin(), t.end());
  return s == t;
}

string solve(const string &s) {
  int n = s.length();
  if (n % 2 == 0 && pal(s)) {
    return s.substr(0, n / 2) + "v" + s.substr(n / 2);
  }
  int v = 0;
  while (v < n / 2) {
    if (s[v] != s[n - 1 - v]) {
      break;
    }
    v++;
  }
  string u1 = s.substr(0, v) + s[n - 1 - v] + s.substr(v);
  string u2 = s.substr(0, n - v) + s[v] + s.substr(n - v);
  if(pal(u1)) return u1;
  if(pal(u2)) return u2;
  return "NA";
}

int main(void){
  string s;
  cin >> s;
  cout << solve(s) << endl;
}
0