站外题求优化
  • 板块学术版
  • 楼主dendky
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/12/9 19:50
  • 上次更新2024/12/9 22:49:06
查看原帖
站外题求优化
1365127
dendky楼主2024/12/9 19:50

this

#include <bits/stdc++.h>
using namespace std;
char a[1001];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, p;
    cin>>n>>p;
    cin>>a;
    while (1){
        int x=n-1;
        while (x>=0){
            if (a[x]+1<='a'+p-1){
                a[x]++;
                break;
            }else a[x--]='a';
        }
        if (x==-1) break;
        bool flag=false;
        for (int i=2; i<=n; i++){
            for (int j=0; j<=n-i; j++){
                int p2=i+j-1, p1=j;
                while (p1<p2 && a[p1]==a[p2]){
                    p1++;
                    p2--;
                }
                if (p1>=p2){
                    flag=true;
                    break;
                }
            }
            if (flag) break;
        }
        if (!flag) {
            cout<<a;
            return 0;
        }
    }
    cout<<"NO";
    return 0;
}
2024/12/9 19:50
加载中...