結果

問題 No.238 Mr. K's Another Gift
ユーザー moti
提出日時 2015-07-22 16:33:35
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 1,367 bytes
コンパイル時間 764 ms
コンパイル使用メモリ 84,924 KB
実行使用メモリ 16,384 KB
最終ジャッジ日時 2024-07-08 11:48:41
合計ジャッジ時間 2,123 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <set>
#include <map>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int N;
string s;
string ans;

bool dfs(int a, int b, bool used) {

  if( (N+used) % 2 == 0 && a - 1 == b ) {
    string t = ans; reverse(t.begin(), t.end());
    string u = !used ? "x" : "";
    ans = ans + u + t;
    return true;
  }

  if( N % 2 == 1 && !used && a - 2 == b) {
    string t = ans; reverse(t.begin(), t.end());
    string u; u = ans[b+1];
    ans = ans + u + t.substr(1);
    return true;    
  }

  if( (N+used) % 2 == 1 && a - 2 == b ) {
    string t = ans; reverse(t.begin(), t.end());
    string u; if(!used) u = ans[a+1];
    ans = ans + u + t.substr(1);
    return true;
  }

  if(s[a] == s[b]) {
    ans += s[a];
    if(dfs(a+1, b-1, used)) { return true; }
    ans.pop_back();
  }
  else if(!used) {
    ans += s[a];
    if(dfs(a+1, b, true)) { return true; }
    ans.back() = s[b];
    if(dfs(a, b-1, true)) { return true; }
    ans.pop_back();
  }
  return false;
}

int main() {

  cin >> s;
  N = s.size();
  if(N == 1) {
    ans = s + s;
  }
  else {
    dfs(0, N-1, false);
  }
  if(ans.empty()) {
    cout << "NA\n";
  }
  else {
    cout << ans << endl;
  }

  return 0;
}
0