結果

問題 No.1512 作文
ユーザー hami
提出日時 2021-11-27 17:14:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,151 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 175,564 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-30 08:13:52
合計ジャッジ時間 3,401 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
using LL = long long;
using P = pair<int,int>;
using Graph = vector<vector<int>>;
const int INF = 1 << 29;
const long long LINF = 1LL << 60;
#define all(x) (x).begin(), (x).end()
#define rep(i,n) for(int i = 0; i < (n); ++i)
template<class T>void chmin(T&a, T b){if(a > b) a = b;}
template<class T>void chmax(T&a, T b){if(a < b) a = b;}


int main(){
	LL N; cin >> N;
	vector<vector<int>> tmp(26, vector<int>(26, 0));
	for(LL i = 0; i < N; ++i){ 
		string S, T; cin >> S;
		T = S;
		sort(S.begin(), S.end());
		if(T!=S) continue;
		int a = S[0] - 'a', b = S[(int)S.size()-1] - 'a';
		if(a==b) tmp[a][b] += (int)S.size();
		else tmp[a][b] = max(tmp[a][b], (int)S.size());
	}
	/*
	for(int i = 0; i < 26; ++i){
		for(int j = 0; j < 26; ++j){
			cout << tmp[i][j] << " ";
		}
		cout << endl;
	}
	*/
	vector<long long> dp(26, 0);
	dp[0] = 0;
	for(int i = 0; i < 26; ++i){
		for(int j = 0; j < 26; ++j){
			if(j<i) continue;
			chmax(dp[j], dp[i] + tmp[i][j]);
		}
	}
	LL ans = 0;
	for(int i = 0; i < 26; ++i){
		//cout << dp[i] << endl;
		ans = max(ans, dp[i]);
	}
	cout << ans << endl;


}

0