結果

問題 No.1512 作文
ユーザー tnakao0123
提出日時 2021-05-22 21:13:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,351 bytes
コンパイル時間 1,043 ms
コンパイル使用メモリ 97,292 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-10 20:38:49
合計ジャッジ時間 2,470 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1512.cc:  No.1512 作文 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_L = 200000;

/* typedef */

typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

char s[MAX_L + 4];
vpii nbrs[26];
int vls[26], ds[26];

/* subroutines */

inline void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    int l = strlen(s);

    bool ok = true;
    for (int i = 0; ok && i + 1 < l; i++)
      ok = (s[i] <= s[i + 1]);

    if (ok) {
      int c0 = s[0] - 'a', c1 = s[l - 1] - 'a';
      if (c0 == c1) vls[c0] += l;
      else nbrs[c0].push_back(pii(c1, l));
    }
  }

  for (int i = 0; i + 1 < 26; i++)
    nbrs[i].push_back(pii(i + 1, 0));

  ds[0] = vls[0];
  for (int u = 0; u < 26; u++) {
    for (auto vw: nbrs[u]) {
      int v = vw.first;
      setmax(ds[v], ds[u] + vls[v] + vw.second);
    }
  }

  printf("%d\n", ds[25]);
  return 0;
}
0