【題解】Codeforces 1296E1. String Coloring (easy version)

【題目敘述】http://codeforces.com/contest/1296/problem/E1

#include <iostream>
using namespace std;
 
int n, a[26], ans[200005], cnt;
string s;
 
int main() {
    cin >> n;
    cin >> s;
    for (int i = 0; i < 26; i++){
        a[i] = 'a';
    }
    for (int i = 0; i < n; i++){
        for (int j = 0; j < 26; j++){
            if (s[i] >= a[j]){
                a[j] = s[i];
                ans[i] = j;
                cnt = max(cnt, j);
                break;
            }
        }
    }
    if (cnt <= 1){
        cout << "YES\n";
        for (int i = 0; i < n; i++){
            cout << ans[i];
        }
        cout << "\n";
    }
    else cout << "NO\n";
}
分享本文 Share with friends