Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 15:17:30
2018-05-26 15:12:05
2018-05-26 15:05:09
2018-05-26 15:00:51
2018-05-26 14:59:10
2018-05-26 14:30:32
2018-05-26 14:26:15
2018-05-26 14:16:51
Task:Find a Word
Sender:Tefyn virallinen maajoukkue
Submission time:2018-05-26 15:17:30
Status:READY
Result:ACCEPTED

Show test data

Code

#include <bits/stdc++.h>

using namespace std;
int n;
string s[33];
int64_t r[33][33];
string pp[33][33];
int np[33][33];
string hae(string lol, int64_t k, int64_t a, int64_t b, int i, int j){
    //cout << lol+s[i][j] << " " << k << " " << a << " " << b << " " << i << " " << j << " " << np[i][j] << " " <<pp[i][j] << endl;
    if(k < a || k >= b){
       // cout << "re1" << endl;
        return "";
    }
    if(i == n-1 && j == n-1){
        if(k >= a && k < b)
            return lol+s[i][j];
        return "";
    }
        
    if(i < n-1 && j < n-1){
        if(s[i+1][j] < s[i][j+1]){
            int64_t down = r[i+1][j];
            string re = hae(lol+s[i][j], k, a, a+down, i+1, j);
            if(re.length() == 0){
                re = hae(lol+s[i][j], k, a+down, b, i, j+1);
            }
            return re;
        }
        else if(s[i+1][j] > s[i][j+1]){
            int64_t ri = r[i][j+1];
            string re = hae(lol+s[i][j], k, a+ri, b, i+1, j);
            if(re.length() == 0){
                re = hae(lol+s[i][j], k, a, a+ri, i, j+1);
            }
            return re;
        }
        else {
            int64_t ri = r[i][j+1];
            int64_t down = r[i+1][j];
            if(np[i][j] == 1){
                string re3 = hae(lol+s[i][j], k, a, a+down, i+1, j);
                if(re3.length() == 0){
                    re3 = hae(lol+s[i][j], k, a+down, b, i, j+1);
                }
                return re3;
            }
            else {
                string re = hae(lol+s[i][j], k, a+ri, b, i+1, j);
                if(re.length() == 0){
                    re = hae(lol+s[i][j], k, a, a+ri, i, j+1);
                }
                return re;
            }
        }
    }
    else if(i < n-1){
        return hae(lol+s[i][j], k, a, b, i+1, j);
    }
    else if(j < n-1){
        return hae(lol+s[i][j], k, a, b, i, j+1);
    }
    return "";
}


int main(){
    int64_t k;
    cin >> n >> k;
    for(int i = 0; i < n; ++i)
        cin >> s[i];
    r[n-1][n-1] = 1;
    pp[n-1][n-1] = s[n-1][n-1];
    for(int i = n-1; i >= 0; --i){
        for(int j = n-1; j >= 0; --j){
            string c1 = "0";
            c1[0] = 'Z'+1;
            string c2 = "0";
            c2[0] = 'Z'+1;
            if(i < n-1){
                r[i][j] += r[i+1][j];
                c1 = s[i][j]+pp[i+1][j];
            }
            if(j< n-1){
                r[i][j] += r[i][j+1];
                c2 = s[i][j]+pp[i][j+1];
            }
            if(i < n-1 || j < n-1){
                if(c1 < c2){
                    np[i][j] = 1;
                    pp[i][j] = c1;
                }
                else{
                    pp[i][j] = c2;
                }
            }
            //cout << i << " " << j << " " << pp[i][j] << endl;
        }
    }
    cout << hae("", k-1, 0, r[0][0], 0, 0) << endl;
    return 0;
}