Submission #114522

# Submission time Handle Problem Language Result Execution time Memory
114522 2019-06-01T15:41:41 Z popovicirobert Type Printer (IOI08_printer) C++14
100 / 100
525 ms 34708 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const int INF = (int) 1e9;
const int B = 41;

inline int get(string &a, string &b) {
    int p = 0;
    while(p < min(a.size(), b.size()) && a[p] == b[p]) {
        p++;
    }
    return p;
}

map <ull, int> mp;

pair <int, int> solve(vector <string> &str, ull hsh) {
    int n = str.size();

    sort(str.begin(), str.end());

    vector < pair <int, int> > fr(26);
    for(auto &it : fr) {
        it.first = it.second = INF;
    }

    pair <int, int> ans = {0, INF};

    int i = 0;
    while(i < n) {

        vector <string> aux;
        aux.clear();

        int j = i;
        while(j < n && str[i][0] == str[j][0]) {
            if(str[j].size() > 1) {
                aux.push_back(str[j].substr(1, (int) str[j].size() - 1));
            }
            j++;
        }

        char ch = str[i][0] - 'a';

        if(aux.size()) {
            fr[ch] = solve(aux, hsh * B + ch + 1);
            fr[ch].first += 2;
            fr[ch].second++;
        }
        else {
            fr[ch] = {2, 1};
        }

        i = j;
    }

    int sum = 0;
    for(i = 0; i < 26; i++) {
        if(fr[i].first != INF) {
            sum += fr[i].first;
        }
    }

    ans.first = sum;

    for(i = 0; i < 26; i++) {
        if(fr[i].second == INF) {
            continue;
        }

        int cur = sum - fr[i].first + fr[i].second;
        if(cur < ans.second) {
            ans.second = cur;
            mp[hsh] = i;
        }
    }

    return ans;

}

void gen(vector <string> &str, ull hsh) {
    int n = str.size();
    char ch = mp[hsh] + 'a';

    sort(str.begin(), str.end());

    vector <string> sol, aux;
    int p = 0;
    for(int i = 0; i < n; i++) {
        if(str[i][0] != ch) {
            sol.push_back(str[i]);
        }
        else {
            aux.push_back({});
            if(str[i].size() > 1) {
                aux[p++] = str[i].substr(1, (int) str[i].size() - 1);
            }
        }
    }

    if(p > 0) {
        gen(aux, hsh * B + ch - 'a' + 1);
    }

    for(auto &it : aux) {
        sol.push_back(ch + it);
    }

    swap(str, sol);
}

inline void tr(string &a, string &b) {
    int p = 0;
    while(p < min(a.size(), b.size()) && a[p] == b[p]) {
        p++;
    }
    while(a.size() > p) {
        cout << "-\n";
        a.pop_back();
    }
    while(p < b.size()) {
        cout << b[p] << "\n";
        p++;
    }
    cout << "P\n";
    a = b;
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;

    vector <string> str(n);
    for(i = 0; i < n; i++) {
        cin >> str[i];
    }

    cout << solve(str, 0).second + n << "\n";

    gen(str, 0);

    string last;
    for(auto it : str) {
        tr(last, it);
    }

    //cin.close();
    //cout.close();
    return 0;
}

Compilation message

printer.cpp: In function 'int get(std::__cxx11::string&, std::__cxx11::string&)':
printer.cpp:15:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(p < min(a.size(), b.size()) && a[p] == b[p]) {
           ~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'void tr(std::__cxx11::string&, std::__cxx11::string&)':
printer.cpp:121:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(p < min(a.size(), b.size()) && a[p] == b[p]) {
           ~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:124:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(a.size() > p) {
           ~~~~~~~~~^~~
printer.cpp:128:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(p < b.size()) {
           ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 896 KB Output is correct
2 Correct 9 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 2432 KB Output is correct
2 Correct 48 ms 4488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 5492 KB Output is correct
2 Correct 32 ms 2568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 12824 KB Output is correct
2 Correct 445 ms 29280 KB Output is correct
3 Correct 227 ms 20000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 9824 KB Output is correct
2 Correct 519 ms 34708 KB Output is correct
3 Correct 276 ms 23212 KB Output is correct
4 Correct 525 ms 32764 KB Output is correct