Submission #433717

# Submission time Handle Problem Language Result Execution time Memory
433717 2021-06-20T09:54:19 Z Lam_lai_cuoc_doi K-th path (IZhO11_kthpath) C++17
0 / 100
1 ms 204 KB
#include <bits/stdc++.h>

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

constexpr bool typetest = 0;
constexpr int N = 30 + 2;
int m, n;
string s[N];
ll dp[N][N], k, g[N][N];

void Read()
{
    cin >> m >> n;

    for (int i = 1; i <= m; ++i)
    {
        cin >> s[i];
        s[i] = " " + s[i];
    }

    cin >> k;
}

void BFS(int x, int y)
{
    queue<pair<int, int>> s;
    dp[x][y] = 1;
    s.emplace(x, y);

    while (s.size())
    {
        auto c = s.front();
        s.pop();

        if (c.first > 1)
        {
            if (!dp[c.first - 1][c.second])
                s.emplace(c.first - 1, c.second);
            dp[c.first - 1][c.second] += dp[c.first][c.second];

            if (dp[c.first - 1][c.second] > 1e18)
                dp[c.first - 1][c.second] = 2e18;
        }
        if (c.second > 1)
        {
            if (!dp[c.first][c.second - 1])
                s.emplace(c.first, c.second - 1);
            dp[c.first][c.second - 1] += dp[c.first][c.second];

            if (dp[c.first][c.second - 1] > 1e18)
                dp[c.first][c.second - 1] = 2e18;
        }
    }
}

ll f(int x, int y, const string &ans)
{
    if (g[x][y] != -1)
        return g[x][y];

    if (x + y - 1 == ans.size() || ans[x + y - 1] == '*')
        return dp[x][y];

    g[x][y] = 0;

    if (x < m && ans[x + y - 1] == s[x + 1][y])
        g[x][y] += f(x + 1, y, ans);
    if (y < n && ans[x + y - 1] == s[x][y + 1])
        g[x][y] += f(x, y + 1, ans);

    return g[x][y];
}

void Solve()
{
    BFS(m, n);

    string ans(m + n - 1, '*');

    for (int i = 0; i < (int)ans.size(); ++i)
        for (int j = 0; j < 26; ++j)
        {
            ans[i] = j + 'a';

            memset(g, -1, sizeof g);

            ll cnt = f(1, 1, ans);

            if (cnt >= k)
                break;

            k -= cnt;
        }

    cout << ans;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        Read();
        Solve();
    }
}

Compilation message

kthpath.cpp: In function 'll f(int, int, const string&)':
kthpath.cpp:64:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     if (x + y - 1 == ans.size() || ans[x + y - 1] == '*')
      |         ~~~~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -