QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#358224#3178. Shattered CakePetroTarnavskyi#RE 0ms0kbC++201.9kb2024-03-19 18:16:462024-03-19 18:16:47

Judging History

你现在查看的是最新测评结果

  • [2024-03-19 18:16:47]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-03-19 18:16:46]
  • 提交

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;
}

詳細信息

Test #1:

score: 0
Runtime Error

input:

4
7
2 3
1 4
1 2
1 2
2 2
2 2
2 1

output:


result: