#include #include #include #include #include #include #include #include #include #include using namespace std; class Rolling_Hash{ void constract(const string& s){ //hash[i] = [0,i) for(int i=0; i hash; vector p_pow; const unsigned long long P; const unsigned long long MOD; Rolling_Hash(const string& s, unsigned long long mod, unsigned long long p) : P(p), MOD(mod), N(s.size()), hash(s.size()+2, 0), p_pow(s.size()+2) { constract(s); p_pow[0] = 1; for(int i=1; i<=N+1; i++){ p_pow[i] = (1LL * p_pow[i-1] * P) % MOD; } } // [l,r) unsigned long long get_hash(int l, int r){ int len = r-l; return (hash[r] - (1LL * hash[l]* p_pow[len])%MOD + MOD) % MOD; } unsigned long long get_mod(){ return MOD; } }; unsigned long long get_hash(Rolling_Hash& h, int i, int n){ unsigned long long ret = h.get_hash(0,i); ret *= h.p_pow[n-i+1]; ret += h.get_hash(i, n); ret %= h.get_mod(); return ret; } unsigned long long insert_char(Rolling_Hash& h, int i, int n, int c, unsigned long long hash){ hash += c * h.p_pow[n-i]; hash %= h.get_mod(); return hash; } #include int main(){ string s; cin >> s; int n=s.size(); string r = s; reverse(r.begin(), r.end()); srand((unsigned)time(NULL)); unsigned long long p_0 = vector{13,15,78}[rand()%3]; unsigned long long p_1 = vector{13,15,78}[rand()%3]; Rolling_Hash s0(s, 1000000007, p_0); Rolling_Hash r0(r, 1000000007, p_0); Rolling_Hash s1(s, 1000000009, p_1); Rolling_Hash r1(r, 1000000009, p_1); bool ok = false; int pos = -1; char c_ = '\0'; for(int i=0; i<=n && ok==false; i++){ auto H_s0 = get_hash(s0, i, n); auto H_r0 = get_hash(r0, n-i, n); auto H_s1 = get_hash(s1, i, n); auto H_r1 = get_hash(r1, n-i, n); for(int c='a'; c<='z'; c++){ if(insert_char(s0, i,n,c, H_s0) != insert_char(r0, n-i,n,c, H_r0)) continue; if(insert_char(s1, i,n,c, H_s1) != insert_char(r1, n-i,n,c, H_r1)) continue; ok = true; pos = i; c_ = c; break; } } if(ok){ string t = s.substr(0, pos); t += c_; t += s.substr(pos, string::npos); cout << t << endl; }else{ cout << "NA" << endl; } return 0; }