結果
| 問題 |
No.238 Mr. K's Another Gift
|
| コンテスト | |
| ユーザー |
codershifth
|
| 提出日時 | 2016-05-01 18:06:58 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,028 bytes |
| コンパイル時間 | 1,345 ms |
| コンパイル使用メモリ | 163,872 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-05 00:42:36 |
| 合計ジャッジ時間 | 3,070 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 40 |
ソースコード
#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
codershifth