# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
433730 | Lam_lai_cuoc_doi | K-th path (IZhO11_kthpath) | C++17 | 2 ms | 332 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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(s[x][y] != ans[x + y - 2]) return g[x][y] = 0;
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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |