답안 #162678

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
162678 2019-11-09T08:37:37 Z _qVp_ Cezar (COCI16_cezar) C++14
100 / 100
391 ms 504 KB
#include <bits/stdc++.h>

using namespace std;

const int md = 105;
const int oo = 1e9;

int a[md], ans[md], in[md], finish[md], vis[md];
stack < int > st;
vector < int > adj[md], topo;
int cnt, n, loop;
string s1[md], s[md];

void check(int u) {
    if (finish[u] == 1) {
        loop = 1;
        return ;
    }
    finish[u] = 1;
    for(int i = 0; i < adj[u].size(); i++)
        check(adj[u][i]);
    finish[u] = 0;
}

void dfs(int u) {
    vis[u] = 1;
    //cout << u << '\n';
    for(int i = 0; i < adj[u].size(); i++) {
        int v = adj[u][i];
        if (vis[v]) continue;
        dfs(v);
    }
    topo.push_back(u);
}

int main() {
    //freopen("test.in", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> s1[i];
    for(int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        s[i] = s1[x];
    }
    //for(int i = 1; i <= n; i++)
    //    cout << s[i] << endl;
    for(int u = 1; u < n; u++) {
        //int u = a[i];
        //cerr << s[u] << endl;
        int v = u + 1;
        //for(int v = u + 1; v <= n; v++) {
            int len = min(s[u].size(), s[v].size());
            //cerr << s[v] << " ";
            bool ok = false;
            for(int k = 0; k < len; k++) 
                if (s[u][k] != s[v][k]) {
                    ok = true;
                    //cout << (s[v][k] - 'a') << " " << (s[u][k] - 'a') << endl;
                    adj[s[v][k] - 'a'].push_back(s[u][k] - 'a');
                    in[s[u][k] - 'a']++;
                    break;
                }
            if (ok == false && s[u].size() > s[v].size())  {
                cout << "NE";
                return 0;
            }
        //}
        //cerr << endl;
    }
    //cerr  << "checking";  
    for(int i = 0; i < 26; i++) {
        memset(finish, 0, sizeof finish);
        check(i);
        if (loop == 1) {
            cout << "NE";
            return 0;
        }
    }
    for(int i = 0; i < 26; i++)
        if (!in[i])
            dfs(i);
    int cnt = 0;
    for(int i = 0; i < topo.size(); i++) {
        //cout << topo[i] << " ";
        ans[topo[i]] = i;
    }
    cout << "DA\n";
    for(int i = 0; i < 26; i++)
        cout << char(ans[i] + 'a');
    return 0;
}

Compilation message

cezar.cpp: In function 'void check(int)':
cezar.cpp:20:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++)
                    ~~^~~~~~~~~~~~~~~
cezar.cpp: In function 'void dfs(int)':
cezar.cpp:28:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~
cezar.cpp: In function 'int main()':
cezar.cpp:85:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < topo.size(); i++) {
                    ~~^~~~~~~~~~~~~
cezar.cpp:84:9: warning: unused variable 'cnt' [-Wunused-variable]
     int cnt = 0;
         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 137 ms 396 KB Output is correct
2 Correct 24 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 391 ms 376 KB Output is correct
2 Correct 11 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 424 KB Output is correct
2 Correct 12 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 504 KB Output is correct