QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#358224 | #3178. Shattered Cake | PetroTarnavskyi# | RE | 0ms | 0kb | C++20 | 1.9kb | 2024-03-19 18:16:46 | 2024-03-19 18:16:47 |
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int N = 10'474;
const int ALP = 26;
int g[N][ALP];
int sz = 1;
int add(string s)
{
int v = 0;
FOR (i, 0, SZ(s))
{
if (g[v][s[i] - 'a'] == -1)
g[v][s[i] - 'a'] = sz++;
v = g[v][s[i] - 'a'];
}
return v;
}
const int INF = 1e9;
const int N2 = 74;
int dp[N2][N2];
int dp2[N2][N];
int cst[N];
string s;
void f(int L)
{
int n = SZ(s);
FOR (i, 0, N2) FOR (j, 0, N) dp2[i][j] = -INF;
dp2[L][0] = 0;
FOR (i, L, n)
{
FOR (v, 0, sz)
{
if (dp2[i][v] == -INF)
continue;
int u = g[v][s[i] - 'a'];
if (u != -1)
{
dp2[i + 1][u] = max(dp2[i + 1][u], dp2[i][v]);
}
FOR (j, i + 1, n + 1)
dp2[j][v] = max(dp2[j][v], dp2[i][v] + dp[i][j]);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
FOR(i, 0, N) FOR(j, 0, ALP) g[i][j] = -1;
fill(cst, cst + N, -INF);
cin >> s;
int C;
cin >> C;
vector<string> v(C);
VI cost(C);
FOR (i, 0, C)
{
cin >> v[i] >> cost[i];
FOR (t, 0, 2)
{
int a = add(v[i]);
cst[a] = max(cst[a], cost[i]);
reverse(ALL(v));
}
}
FOR (i, 0, N2) FOR (j, 0, N2) dp[i][j] = -INF;
int n = SZ(s);
RFOR (L, n, 0)
{
f(L);
FOR (R, L + 1, n + 1)
{
FOR (u, 0, sz)
{
dp[L][R] = max(dp[L][R], dp2[R][u] + cst[u]);
}
}
}
VI ans(n + 1, -INF);
ans[0] = 0;
FOR (i, 0, n)
{
ans[i + 1] = max(ans[i + 1], ans[i]);
FOR (j, i + 1, n + 1)
ans[j] = max(ans[j], ans[i] + dp[i][j]);
}
cout << ans[n] << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
4 7 2 3 1 4 1 2 1 2 2 2 2 2 2 1