結果

問題 No.238 Mr. K's Another Gift
ユーザー furafura
提出日時 2020-06-09 18:32:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,954 bytes
コンパイル時間 2,621 ms
コンパイル使用メモリ 206,596 KB
実行使用メモリ 9,968 KB
最終ジャッジ日時 2023-08-30 18:52:08
合計ジャッジ時間 5,706 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 15 ms
8,772 KB
testcase_06 AC 11 ms
9,968 KB
testcase_07 AC 17 ms
9,780 KB
testcase_08 AC 17 ms
9,736 KB
testcase_09 AC 18 ms
9,712 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 11 ms
7,312 KB
testcase_15 AC 17 ms
9,648 KB
testcase_16 AC 17 ms
9,620 KB
testcase_17 AC 17 ms
9,648 KB
testcase_18 AC 17 ms
9,652 KB
testcase_19 AC 17 ms
9,668 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 3 ms
4,424 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 18 ms
9,684 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 17 ms
9,768 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 15 ms
8,964 KB
testcase_35 AC 17 ms
9,616 KB
testcase_36 AC 17 ms
9,708 KB
testcase_37 AC 17 ms
9,664 KB
testcase_38 AC 17 ms
9,696 KB
testcase_39 AC 16 ms
9,668 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

using namespace std;

class rolling_hash{
	static const long long base=10007,mod1=1e9+7,mod2=1e9+9;
	int len;
	vector<long long> pow1,pow2,h1,h2;
public:
	using hash_type=pair<long long,long long>;
	rolling_hash(){}
	rolling_hash(const string& s):len(s.length()),pow1(len+1),pow2(len+1),h1(len+1),h2(len+1){
		pow1[0]=pow2[0]=1;
		rep(i,len){
			pow1[i+1]=pow1[i]*base%mod1;
			pow2[i+1]=pow2[i]*base%mod2;
			h1[i+1]=(h1[i]*base+s[i])%mod1;
			h2[i+1]=(h2[i]*base+s[i])%mod2;
		}
	}
	template<class T>
	rolling_hash(const vector<T>& a):len(a.size()),pow1(len+1),pow2(len+1),h1(len+1),h2(len+1){
		pow1[0]=pow2[0]=1;
		rep(i,len){
			pow1[i+1]=pow1[i]*base%mod1;
			pow2[i+1]=pow2[i]*base%mod2;
			h1[i+1]=(h1[i]*base+a[i])%mod1;
			h2[i+1]=(h2[i]*base+a[i])%mod2;
		}
	}
	hash_type get_hash(int l,int r)const{
		assert(0<=l && l<=r && r<=len);
		auto res1=(h1[r]-h1[l]*pow1[r-l])%mod1; if(res1<0) res1+=mod1;
		auto res2=(h2[r]-h2[l]*pow2[r-l])%mod2; if(res2<0) res2+=mod2;
		return {res1,res2};
	}
	static hash_type get_hash(const string& s){
		long long res1=0,res2=0;
		rep(i,s.length()){
			res1=(res1*base+s[i])%mod1;
			res2=(res2*base+s[i])%mod2;
		}
		return {res1,res2};
	}
	template<class T>
	static hash_type get_hash(const vector<T>& a){
		long long res1=0,res2=0;
		rep(i,a.size()){
			res1=(res1*base+a[i])%mod1;
			res2=(res2*base+a[i])%mod2;
		}
		return {res1,res2};
	}
};

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

	string t=s;
	reverse(t.begin(),t.end());

	if(n%2==0 && s==t){
		cout<<s.substr(0,n/2)+'a'+s.substr(n/2)<<'\n';
		return 0;
	}

	rep(k,2){
		rolling_hash Hs(s),Ht(t);
		rep(i,(n+1)/2){
			if(Hs.get_hash(0,i)==Ht.get_hash(0,i) && Hs.get_hash(i,n/2)==Ht.get_hash(i+1,n/2+1)){
				cout<<s.substr(0,i)+s[n-1-i]+s.substr(i)<<'\n';
				return 0;
			}
		}

		reverse(s.begin(),s.end());
		reverse(t.begin(),t.end());
	}

	cout<<"NA\n";

	return 0;
}
0