結果

問題 No.238 Mr. K's Another Gift
ユーザー codershifthcodershifth
提出日時 2016-05-01 18:06:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,028 bytes
コンパイル時間 1,496 ms
コンパイル使用メモリ 163,336 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 08:24:21
合計ジャッジ時間 3,581 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class MrKsAnotherGift
{
public:
    void solve(void)
    {
        string s;
        cin>>s;

        int N = s.length();
        string ans[2];
        bool inserted = false;

        // 最初の挿入が必要なところでダメなら以降もダメなので
        // 挿入の施行は一回でよい
        // O(N)
        for (int i = 0; i < N/2; ++i)
        {
            if ( s[i] != s[N-1-i] )
            {
                ans[0] = s.substr(0,i) + string(1,s[N-1-i]) + s.substr(i,N);
                ans[1] = s.substr(0,N-i) + string(1,s[i]) + s.substr(N-i,N);
                inserted = true;
                break;
            }
        }
        if ( !inserted )
        {
            if ( N % 2 == 0 )   // 偶数長なら真ん中は何でもよい
                cout<<(s.substr(0,N/2) + string(1,'a') + s.substr(N/2,N))<<endl;
            else                // 奇数長なら真ん中を重複させる
                cout<<(s.substr(0,N/2) + string(1,s[N/2]) + s.substr(N/2,N))<<endl;
            return;
        }

        bool ok[2] = {true,true};
        int n = N+1;
        // 回文になっているかチェック
        for (int j = 0; j < 2; ++j)
        {
            string tmp = ans[j];
            for (int i = 0; i < n/2; ++i)
            {
                if ( tmp[i] != tmp[n-1-i] )
                {
                    ok[j] = false;
                    break;
                }
            }
        }
        if ( ok[0] )
            cout<<ans[0]<<endl;
        else if ( ok[1] )
            cout<<ans[1]<<endl;
        else
            cout<<"NA"<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new MrKsAnotherGift();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0