結果

問題 No.238 Mr. K's Another Gift
ユーザー face4
提出日時 2018-09-02 16:03:16
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,023 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 66,964 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-01 19:27:25
合計ジャッジ時間 2,382 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
using namespace std;

bool isPalindrome(string s){
    int n = s.length();
    for(int i = 0; i < n/2; i++){
        if(s[i] != s[n-1-i])    return false;
    }
    return true;
}

int main(){
    string s;
    cin >> s;
    
    int n = s.length();

    if(isPalindrome(s)){
        s.insert(s.begin() + n/2, s[n/2]);
    }else{
        string ans = "";
        
        int diffpos;
        for(diffpos = 0;; diffpos++)    if(s[diffpos] != s[n-1-diffpos])    break;
        
        s.insert(s.begin() + diffpos, 'a');
        for(int i = 0; i < 26; i++){
            if(isPalindrome(s)) ans = s;
            s[diffpos]++;
        }
        s.erase(s.begin()+diffpos);

        s.insert(s.begin() + n - diffpos, 'a');
        for(int i = 0; i < 26; i++){
            if(isPalindrome(s)) ans = s;
            s[n-diffpos]++;
        }
        s.erase(s.begin() + n - diffpos); // needless

        if(ans != "")   s = ans;
        else            s = "NA";
    }

    cout << s << endl;

    return 0;
}
0