結果

問題 No.1512 作文
ユーザー maimaicorgimaimaicorgi
提出日時 2022-09-27 16:03:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 1,924 bytes
コンパイル時間 1,773 ms
コンパイル使用メモリ 151,684 KB
実行使用メモリ 36,696 KB
最終ジャッジ日時 2023-08-24 03:54:47
合計ジャッジ時間 4,356 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 100 ms
25,380 KB
testcase_05 AC 98 ms
26,172 KB
testcase_06 AC 102 ms
26,392 KB
testcase_07 AC 101 ms
25,856 KB
testcase_08 AC 101 ms
26,296 KB
testcase_09 AC 10 ms
4,376 KB
testcase_10 AC 11 ms
4,376 KB
testcase_11 AC 10 ms
4,376 KB
testcase_12 AC 11 ms
4,380 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 29 ms
6,320 KB
testcase_15 AC 29 ms
6,352 KB
testcase_16 AC 29 ms
6,364 KB
testcase_17 AC 29 ms
6,460 KB
testcase_18 AC 29 ms
6,324 KB
testcase_19 AC 28 ms
6,356 KB
testcase_20 AC 29 ms
6,356 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 7 ms
4,380 KB
testcase_36 AC 7 ms
4,380 KB
testcase_37 AC 5 ms
4,376 KB
testcase_38 AC 7 ms
4,380 KB
testcase_39 AC 6 ms
4,376 KB
testcase_40 AC 126 ms
36,696 KB
testcase_41 AC 118 ms
36,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <ios>
#include <iomanip>
#include <queue>
#include <numeric>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <climits>
#include <stack>
#include <regex>
#include <functional>
#include <random>
#ifdef _WIN64
#  include <atcoder/all>
#endif
#ifdef _MSC_VER
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#  define __builtin_popcountl __popcnt64
#endif

using namespace std;

#define ll long long
#define rep(i, init, n) for(ll i = init; i < (ll)n; i++)
#define rrep(i, init, n) for(ll i = init; i >= (ll)n; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x.size())
#define Out(x) cout << x << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define Ans cout << ans << endl
#define PI 3.14159265358979
#define MOD 998244353

const int inf32 = INT_MAX / 2;
const ll inf64 = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

// -------------------------------------------------------------------------------------------------

int main()
{
	int n; cin >> n;
	vector<string> s(n);
	rep(i, 0, n) cin >> s[i];

	sort(all(s), [](string x, string y)
	{
		if (x.back() != y.back()) return x.back() < y.back();
		else return x[0] < y[0];
	});
	s.insert(s.begin(), "");

	vector dp(n + 1, vector<int>(26, -inf32));
	dp[0][0] = 0;

	rep(i, 1, n + 1)
	{
		string str = s[i];
		sort(all(str));
		bool ok = s[i] == str;

		rep(j, 0, 26)
		{
			chmax(dp[i][j], dp[i - 1][j]);
			if (ok && j <= (s[i][0] - 'a')) chmax(dp[i][s[i].back() - 'a'], dp[i - 1][j] + (int)s[i].length());
		}
	}

	int ans = -inf32;
	rep(i, 0, 26) chmax(ans, dp[n][i]);

	Ans;

	return 0;
}
0