Submission #329878

# Submission time Handle Problem Language Result Execution time Memory
329878 2020-11-23T02:22:51 Z mohamedsobhi777 Type Printer (IOI08_printer) C++14
100 / 100
144 ms 53480 KB
#include <bits/stdc++.h>

#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")

#define I inline void
#define S struct
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;
using ll = long long;
using ld = long double;

const int N = 2e6 + 7, mod = 1e9 + 7;
const ll inf = 2e18;

// How interesting!

int n, nod;
int to[N][27], w[N], dep[N];

void insert(string x, int cur = 0)
{
        for (int i = 0; i < x.size(); ++i){
                cur = to[cur][x[i] - 'a'] = (!to[cur][x[i] - 'a'] ? ++nod : to[cur][x[i] - 'a']);
                dep[cur] = max(dep[cur], (int)x.size());
        }
        w[cur] = 1;
}

vector<char> ans;

void dfs(int x)
{
        if(!n)return ;
        if (w[x])
        {
                ans.push_back('P');
                if(--n == 0)return ; 
        }
        vector<pair<int,int> >  v ; 
        for(int i = 0 ;i < 26 ;++ i)if(to[x][i])v.push_back({dep[to[x][i]],i});
        sort(v.begin(),v.end());
        for(auto u:v){
                ans.push_back(char(u.second+'a')) ;
                dfs(to[x][u.second]) ; 
        }
        if(n)
        ans.push_back('-');
}

int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        //freopen("in.in", "r", stdin);
        cin >> n;
        for (int i = 0; i < n; ++i)
        {
                string x;
                cin >> x;
                insert(x);
        }
        dfs(0);
        cout << (int)ans.size() << "\n";
        for (auto u : ans)
                cout << u << "\n";
        return 0;
}

/*
        - bounds sir (segtree = 4N, eulerTour = 2N, ...)
        - a variable defined twice?
        - will overflow?
        - is it a good complexity?
        - don't mess up indices (0-indexed vs 1-indexed)
        - reset everything between testcases. 
*/

Compilation message

printer.cpp: In function 'void insert(std::string, int)':
printer.cpp:30:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |         for (int i = 0; i < x.size(); ++i){
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1260 KB Output is correct
2 Correct 4 ms 1388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3436 KB Output is correct
2 Correct 19 ms 7020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 8176 KB Output is correct
2 Correct 10 ms 2156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 19820 KB Output is correct
2 Correct 122 ms 45032 KB Output is correct
3 Correct 67 ms 23404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 15596 KB Output is correct
2 Correct 144 ms 53480 KB Output is correct
3 Correct 77 ms 26548 KB Output is correct
4 Correct 126 ms 50408 KB Output is correct