結果
| 問題 | 
                            No.238 Mr. K's Another Gift
                             | 
                    
| コンテスト | |
| ユーザー | 
                             moti
                         | 
                    
| 提出日時 | 2015-07-22 16:38:57 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 8 ms / 2,000 ms | 
| コード長 | 1,344 bytes | 
| コンパイル時間 | 541 ms | 
| コンパイル使用メモリ | 84,764 KB | 
| 実行使用メモリ | 9,984 KB | 
| 最終ジャッジ日時 | 2024-07-08 11:48:44 | 
| 合計ジャッジ時間 | 1,961 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 40 | 
ソースコード
#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, t, u;
string ans;
bool dfs(int a, int b, bool used) {
  if( (N+used) % 2 == 0 && a - 1 == b ) {
    t = ans; reverse(t.begin(), t.end());
    u = !used ? "x" : "";
    cout << ans << u << t << endl;
    return true;
  }
  if( N % 2 == 1 && !used && a - 2 == b) {
    t = ans; reverse(t.begin(), t.end());
    u = ans[b+1];
    cout << ans << u;
    cout << t.substr(1) << endl;
    return true;    
  }
  if( (N+used) % 2 == 1 && a - 2 == b ) {
    t = ans; reverse(t.begin(), t.end());
    if(!used) u = ans[a+1];
    cout << ans << u;
    cout << t.substr(1) << endl;
    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) {
    cout << s << s << endl;
  }
  else {
    if(!dfs(0, N-1, false)) {
      cout << "NA\n";
    }
  }
  return 0;
}
            
            
            
        
            
moti