답안 #1023807

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1023807 2024-07-15T06:49:28 Z caterpillow Cezar (COCI16_cezar) C++17
60 / 100
102 ms 616 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end() 
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;

template<template<typename> class Container, typename T>
ostream& operator<<(ostream& os, Container<T> o) {
    os << "{"; 
    int g = size(o); 
    for (auto i : o) os << i << ((--g) == 0 ? "" : ", "); 
    os << "}";
    return os;
}

void _print() {
    cerr << "\n";
}

template<typename T, typename ...V>
void _print(T t, V... v) {
    cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);
}

#ifdef LOCAL
#define dbg(x...) cerr << #x << " = "; _print(x);
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif

int n;
const int m = 100;
vt<vt<int>> words;
vt<int> perm;
vt<vt<int>> pfx_match;

set<int> adj[27];
bool seen[27], in_stack[27];
vt<int> top;

bool dfs(int u) {
    if (in_stack[u]) return false;
    if (seen[u]) return true;
    seen[u] = in_stack[u] = true;

    bool res = true;
    for (int v : adj[u]) {
        assert(v != u);
        res &= dfs(v);
    }

    top.pb(u);
    in_stack[u] = false;
    return res;
}

bool check(vt<string> words, string key, vt<int> perm) {
    for (string& st : words) {
        for (char& c : st) {
            c = key[c - 'a'];
        }
    }
    auto sorted = words;
    sort(all(sorted));
    int n = size(perm);
    F0R (i, n) {
        string w = words[perm[i]];
        auto it = find(all(sorted), w);
        if (it - sorted.begin() != i) return false;
    }
    return true;
}

vt<string> original_words;
string key;
vt<int> original_perm;


main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin >> n;
    words.resize(n, vt<int>(m));

    original_words.resize(n);

    F0R (i, n) {
        string st; cin >> st;
        original_words[i] = st;
        assert(size(st) <= m);
        F0R (j, size(st)) words[i][j] = st[j] - 'a' + 1;
    }
    perm.resize(n);
    vt<int> inv_perm(n);
    F0R (i, n) cin >> perm[i], perm[i]--;
    original_perm = perm;
    F0R (i, n) inv_perm[perm[i]] = i;
    swap(perm, inv_perm); // inv perm is the original input

    pfx_match.resize(n, vt<int>(n));
    F0R (i, n) pfx_match[i][i] = m;
    F0R (i, n) {
        FOR (j, i + 1, n) {
            int k = 0;
            while (words[i][k] == words[j][k]) k++;
            int a = words[i][k];
            int b = words[j][k];
            if (perm[i] < perm[j]) adj[b].insert(a);
            else adj[a].insert(b);
        }
    }

    FOR (i, 1, 27) adj[i].insert(0);

    F0R (i, 27) {
        if (!seen[i]) {
            if (!dfs(i)) {
                cout << "NE\n";
                volatile unsigned long long bruh = 1294;
                F0R (i, 1e8) {
                    bruh *= 1241231;
                }
                return 0;
            }
        }
    }
    assert(top[0] == 0);
    cout << "DA\n";
    vt<int> new_label(26);
    FOR (i, 1, 27) {
        new_label[top[i] - 1] = i - 1;
    }
    F0R (i, 26) {
        assert('a' <= new_label[i] + 'a' && new_label[i] + 'a' <= 'z');
        cout << (char) (new_label[i] + 'a');
        key +=  (new_label[i] + 'a');
    }
    cout << endl;

    assert(check(original_words, key, original_perm));

    dbg(top);
    dbg(new_label);

    // check answer
    F0R (i, n) {
        F0R (j, m) {
            if (words[i][j] > 0) words[i][j] = new_label[words[i][j] - 1] + 1;
        }
    }
    vt<int> ord(n);
    iota(all(ord), 0);
    sort(all(ord), [&] (int i, int j) { return words[i] < words[j]; });
    vt<int> new_position(n);
    F0R (i, n) new_position[ord[i]] = i;
    F0R (i, n) {
        assert(new_position[inv_perm[i]] == i);
    }
}

Compilation message

cezar.cpp:94:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   94 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 616 KB Output is correct
2 Correct 98 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 101 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 99 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 102 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 99 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 101 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -