結果

問題 No.1512 作文
ユーザー oliverx3oliverx3
提出日時 2021-05-21 23:12:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 8,933 bytes
コンパイル時間 4,829 ms
コンパイル使用メモリ 264,828 KB
実行使用メモリ 33,100 KB
最終ジャッジ日時 2024-04-18 16:48:53
合計ジャッジ時間 6,691 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 59 ms
18,508 KB
testcase_05 AC 57 ms
18,464 KB
testcase_06 AC 57 ms
18,508 KB
testcase_07 AC 58 ms
18,496 KB
testcase_08 AC 59 ms
18,328 KB
testcase_09 AC 10 ms
5,376 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 11 ms
5,376 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 12 ms
5,376 KB
testcase_15 AC 13 ms
5,376 KB
testcase_16 AC 12 ms
5,376 KB
testcase_17 AC 12 ms
5,376 KB
testcase_18 AC 11 ms
5,376 KB
testcase_19 AC 11 ms
5,376 KB
testcase_20 AC 12 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 8 ms
5,376 KB
testcase_36 AC 7 ms
5,376 KB
testcase_37 AC 5 ms
5,376 KB
testcase_38 AC 7 ms
5,376 KB
testcase_39 AC 7 ms
5,376 KB
testcase_40 AC 94 ms
33,100 KB
testcase_41 AC 92 ms
32,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
*    author:  oliverx3
*    created: 21.05.2021 22:35:49
**/

#include <bits/stdc++.h>

#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif

#pragma region header

#pragma region alias

using lint = long long;
using ll = long long;
using P = std::pair<int,int>;
template<class T> using prique = std::priority_queue<T,std::vector<T>,std::greater<T>>;

#pragma endregion

#pragma region macros

#define rep(i, n) for(int i = 0;i<(int)(n);i++)
#define REP(i, m, n) for(int i = (m);i<(int)(n);i++)
#define drep(i, n) for(int i = (n)-1;i>=0;i--)
#define DREP(i, m, n) for(int i = (m)-1;i>=(int)(n);i--)
#define all(v) (v).begin(),(v).end()
#define reall(v) (v).rbegin(),(v).rend()

template<class T, class U>
bool chmax(T& a,const U b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T, class U>
bool chmin(T& a,const U b) {
    if(a>b) {
        a = b;
        return true;
    }
    return false;
}

bool is_flag(const int &bit, const int &k) { return (bit >> k)&1; }

#pragma endregion

#pragma region constant

constexpr int inf = 1<<30;
constexpr long long INF = 1LL << 61;
constexpr long double eps = 1e-10;
constexpr long double pi = 3.141592653589793238;
constexpr int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
constexpr int dy[9] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
constexpr long long mod = 1e9+7;
constexpr long long MOD = 998244353;

#pragma endregion

#pragma region inout

template<class T>
std::ostream& operator<<(std::ostream& stream, const std::vector<T>& v) {
    for(int i = 0; i < (int)(v.size()); i++) {
        stream << v[i];
        if(i != (int)(v.size()) - 1) stream << ' ';
    }
    return stream;
}

template<typename Itr>
inline void print(const Itr& begin, const Itr& end, bool endline = true, const char* BEGIN = "{", const char* mid = ", ", const char* END = "}") {
    if(begin == end) return;
    std::cout << BEGIN << *begin;
    for(Itr itr = begin+1; itr < end; itr++) std::cout << mid << *itr;
    std::cout << END;
    if(endline) std::endl(std::cout);
    return;
}

template<class T>
std::istream& operator>>(std::istream& stream, std::vector<T>& v) {
    for(T& p:v) stream >> p;
    return stream;
}

template<typename Itr>
inline void input(Itr begin, Itr end) {
    for(Itr& itr = begin; itr < end; itr++) std::cin >> *itr;
    return;
}

template<class T, class U>
std::ostream& operator<<(std::ostream& stream, const std::pair<T,U>& pair) {
    return stream << pair.first << ' ' << pair.second;
}

template<class T, class U>
inline void print(const std::pair<T,U>& pair,const bool endline = true, const char* begin = "(", const char* mid = ", ", const char* end = ")") {
    std::cout << begin << pair.first << mid << pair.second << end;
    if(endline) std::endl(std::cout);
    else std::cout << ' ';
    return;
}

template<class T, class U>
std::istream& operator>>(std::istream& stream, std::pair<T,U>& pair) {
    return stream >> pair.first >> pair.second;
}

template<class T, class U>
inline void input(std::pair<T,U>& pair, const bool first = true, const bool second = true) {
    if(first) std::cin >> pair.first;
    if(second) std::cin >> pair.second;
}

#pragma endregion

#pragma region DEBUG

#ifdef _DEBUG
template<class T>
inline void _debug_view(const T& x) noexcept {
    std::cout << x;
    return;
}

template<class T, class U>
inline void _debug_view(const std::pair<T,U>& p) noexcept {
    std::cout << "(";
    _debug_view(p.first);
    std::cout << ", ";
    _debug_view(p.second);
    std::cout << ")";
    return;
}

template<class T>
inline void _debug_view(const std::vector<T>& v) noexcept {
    std::cout << "{";
    for(int i = 0;i<v.size();i++) {
        _debug_view(v[i]);
        std::cout << (i+1 == v.size() ? "" : ", ");
    }
    std::cout << "}";
    return;
}

template<class T, class U>
inline void _debug_view(const std::map<T,U>& mp) noexcept {
    std::cout << "{";
    for(auto itr = mp.begin(); itr != mp.end(); itr++) {
        _debug_view(*itr);
        if(std::next(itr) != mp.end()) std::cout << ", ";
    }
    std::cout << "}";
    return;
}

template<class T> 
inline void _debug_view(const std::set<T>& st) noexcept {
    std::cout << "{";
    for(auto itr = st.begin(); itr != st.end(); itr++) {
        _debug_view(*itr);
        if(std::next(itr) != st.end()) std::cout << ", ";
    }
    std::cout << "}";
    return;
}

#define overload5(_1,_2,_3,_4,_5,name,...) name

#define _debug1(a) {\
    do {\
        std::cout << #a << ": ";\
        _debug_view(a);\
        std::endl(std::cout);\
    }while(0);\
}

#define _debug2(a,b) {\
    do {\
        std::cout << #a << ": ";\
        _debug_view(a);\
        std::cout << ", " << #b << ": ";\
        _debug_view(b);\
        std::endl(std::cout);\
    }while(0);\
}

#define _debug3(a,b,c) {\
    do {\
        std::cout << #a << ": ";\
        _debug_view(a);\
        std::cout << ", " << #b << ": ";\
        _debug_view(b);\
        std::cout << ", " << #c << ": ";\
        _debug_view(c);\
        std::endl(std::cout);\
    }while(0);\
}

#define _debug4(a,b,c,d) {\
    do {\
        std::cout << #a << ": ";\
        _debug_view(a);\
        std::cout << ", " << #b << ": ";\
        _debug_view(b);\
        std::cout << ", " << #c << ": ";\
        _debug_view(c);\
        std::cout << ", " << #d << ": ";\
        _debug_view(d);\
        std::endl(std::cout);\
    }while(0);\
}

#define _debug5(a,b,c,d,e) {\
    do {\
        std::cout << #a << ": ";\
        _debug_view(a);\
        std::cout << ", " << #b << ": ";\
        _debug_view(b);\
        std::cout << ", " << #c << ": ";\
        _debug_view(c);\
        std::cout << ", " << #d << ": ";\
        _debug_view(d);\
        std::cout << ", " << #e << ": ";\
        _debug_view(e);\
        std::endl(std::cout);\
    }while(0);\
}

#define debug(...) overload5(__VA_ARGS__,_debug5,_debug4,_debug3,_debug2,_debug1,)(__VA_ARGS__)

#else
#define debug(...)
#endif

#pragma endregion

#pragma endregion

//ぷらにゃの問題、今から解きます
//ここに思考過程を書いていくなど
//問題文、読んだ
//良い文字列じゃないやつは使えないので、最初に良い文字列以外は捨てる ← O(Σ|S|) なので間に合う
//"ab" と "aab" があった時、どちらかを使うなら絶対に "aab" を使った方がいい
//最初の文字と最後の文字と長さで比較して貪欲にとる?
//"ab" "bc" "aac" みたいなのがあったときに、死にそう
//一番最後の文字が一緒なら、そのあとに続けられる文字列は同じ
//状態をまとめられる → DP!
//dp[i][j] ... i 番目まで考慮して、~~最期~~末尾の文字が j の時の長さの最大値
//空間 O(Nσ) なので大丈夫
//遷移は?
//厳密な計算量わからないけど多分間に合う気がする ← は?
//22:42:50 実装開始します
//22:47:26 普通にやったら O(N^2σ) になって間に合わない気がしてきたなこれ、困った
//ちょっと考えます
//うーんいい感じに工夫したら落とせそうではあるけどわからんなあ
//今やろうとしてること: 全文字列について使うか使わないかをやって、使うなら、あ?なんか考えがまとまってません
//いや間に合いそう (本当か?) 実装してみます
//22:58:35 実装終わった サンプル試す
//サンプル 1 から合わなくて草 カスが代
//良い文字列化のチェック、回文かどうかでチェックしてました  sort と reverse を混ぜるなカス
//サンプル 3 が 13 と出力された  やばそう
//あーはい遷移がやばかった  直します
//サンプル全部あった、頼むぞ  提出してきます
//WA で草  カス
//デバッグするにゃん♡
//sort したから DP できてると思ってるけど、これちゃんと正しい (?) sort になってるかな
//末尾の文字だけで比較して、大丈夫か?
//これ WA 要素ある?ないよ
//とりあえず比較関数変更してみた  根拠はほぼない
//提出!w (え?)

int dp[200010][30];
std::vector<std::string> v;

int main() {
    int n;
    std::cin >> n;
    
    {
        rep(i, n) {
            std::string s;
            std::cin >> s;
            std::string t = s;
            std::sort(all(t));
            if(s==t) v.push_back(s);
        }
        n = v.size();
    }

    std::sort(all(v),[&](const std::string &s, const std::string& t)->bool{return (s.back()!=t.back()?s.back()<t.back():s.front()<t.front());});

    debug(v);

    dp[0][0] = 0;

    rep(i, n) {
        rep(j, v[i].front()-'a'+1) {
            chmax(dp[i+1][v[i].back()-'a'],dp[i][j]+v[i].size());
        }
        rep(j, 26) {
            chmax(dp[i+1][j],dp[i][j]);
        }
    }
    
    int max = 0;
    rep(j, 26) chmax(max,dp[n][j]);
    std::cout << max << std::endl;
    return 0;
}
0