比赛 2026.1.3 评测结果 WAAWAAAWAW
题目名称 KMP 最终得分 60
用户昵称 梦那边的美好BP 运行时间 1.717 s
代码语言 C++ 内存使用 6.99 MiB
提交时间 2026-01-03 10:29:37
显示代码纯文本
#include <iostream>
using namespace std;
const int N = 1e6 + 6;
int a[N];
char s[N];
int main() {
    freopen("kmpp.in", "r", stdin);
    freopen("kmpp.out", "w", stdout);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (a[i] > a[i - 1] && a[i] != a[i - 1] + 1) {
            cout << "Impossible";
            return 0;
        }
    }
    if (a[1] != 0) {
        cout << "Impossible";
        return 0;
    }
    s[1] = 'a';
    for (int i = 2; i <= n; i++) {
        if (a[i] == 0) {
            if (a[i - 1] != 0) {
                if (s[a[i - 1] + 1] == 'c') {
                    s[i] = 'b';
                } else {
                    s[i] = 'c';
                }
            } else {
                s[i] = 'b';
            }
        } else {
            s[i] = s[a[i]];
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << s[i];
    }
    return 0;
}