112322
阿新 • 來源:網路 • 發佈:2023-01-27
#include <bits/stdc++.h>
using namespace std;
string p = "abcdefghijklmnopqrstuvwxyz";
int main(){
int n;
cin >> n;
string s;
cin >> s;
int m = s.size();
if (n == 1) {
cout << "NO" << endl;
return 0;
}
if (n == 2){
if (s == "ab") cout << "ba" << endl;
else cout << "NO" << endl;
return 0;
}
string c;
for (int i = 1; i <= m / 3; i++)
c += p[n - 1], c += p[n - 2] ,c += p[n - 3];
if (m % 3 == 1) c += p[n - 1];
else if (m % 3 == 2) c += p[n - 1] ,c+= p[n - 2];
if (c == s){
cout << "NO" << endl;
return 0;
}
//合法的情況
set<string>st;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
for (int k = 0; k < n; k++){
if (i == j || j == k || i == k) continue;
string c;
c += p[i], c += p[j], c += p[k];
st.insert(c);
}
}
}
for (int i = m - 3; i >= 0; i -= 3){
string t;
t += s[i], t += s[i + 1], t += s[i + 2];
auto g = st.find(t);
g++;
if (g == st.end()) continue;
else{
t = *g;
s[i] = t[0], s[i + 1] = t[1], s[i + 2] = t[2];
//----------------------------
g = st.begin();
t = *g;
for (int j = i + 3; j < m; j++)
s[j] = t[0], s[j + 1] = t[1], s[j + 2] = t[2];
//----------------------------
cout << s << endl;
return 0;
}
}
if (m %3 == 1){
if (s[0] - 'a' != n - 1){
if (m == 1){
s[0] += 1;
cout << s << endl;
return 0;
}
else{
while (s[0] - 'a' != n - 1) {
s[0] += 1;
//--------------------------------
int flag = 0;
string g;
for (auto it : st){
g = it;
if (m >= 2 && g[0] == s[0]) continue;
if (m >= 3 && g[1] == s[0]) continue;
cout << s[0];
for (int i = 1; i <= m / 3; i++)
cout << g;
cout << endl;
return 0;
}
//--------------------------------
}
}
}
cout << "NO" << endl;
return 0;
}
if (m % 3 == 2){
string t; t += p[n - 1], t += p[n - 2];
string g; g += s[0], g += s[1];
while (g != t){
if (g[1] - 'a' != n - 1) g[1]++;
else g[0] += 1, g[1] = 'a';
if (g[0] - 'a' == n) break;
if (g[0] == g[1]) continue;
for (auto it : st){
string h = it;
if (m >= 3 && (g[0] == h[0] || g[1] == h[0])) continue;
if (m >= 4 && (g[1] == h[2])) continue;
s[0] = g[0], s[1] = g[1];
cout << s[0] << s[1];
for (int i = 1; i <= m / 3; i++)
cout << h;
return 0;
}
}
cout << "NO" << endl;
}
return 0;
}
