結果

問題 No.962 LCPs
ユーザー FF256grhyFF256grhy
提出日時 2019-12-25 00:48:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 3,171 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 184,592 KB
実行使用メモリ 26,300 KB
最終ジャッジ日時 2023-10-24 01:19:59
合計ジャッジ時間 5,586 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 64 ms
26,300 KB
testcase_18 AC 34 ms
14,876 KB
testcase_19 AC 33 ms
14,876 KB
testcase_20 AC 25 ms
11,936 KB
testcase_21 AC 24 ms
11,936 KB
testcase_22 AC 20 ms
9,148 KB
testcase_23 AC 19 ms
9,148 KB
testcase_24 AC 18 ms
7,952 KB
testcase_25 AC 17 ms
7,952 KB
testcase_26 AC 16 ms
7,744 KB
testcase_27 AC 22 ms
9,712 KB
testcase_28 AC 18 ms
7,804 KB
testcase_29 AC 15 ms
6,660 KB
testcase_30 AC 21 ms
8,816 KB
testcase_31 AC 29 ms
11,932 KB
testcase_32 AC 15 ms
6,540 KB
testcase_33 AC 34 ms
14,416 KB
testcase_34 AC 17 ms
7,824 KB
testcase_35 AC 16 ms
7,760 KB
testcase_36 AC 24 ms
8,576 KB
testcase_37 AC 15 ms
5,952 KB
testcase_38 AC 14 ms
5,952 KB
testcase_39 AC 14 ms
5,948 KB
testcase_40 AC 14 ms
5,956 KB
testcase_41 AC 14 ms
5,948 KB
testcase_42 AC 15 ms
5,948 KB
testcase_43 AC 14 ms
5,952 KB
testcase_44 AC 14 ms
5,956 KB
testcase_45 AC 15 ms
5,956 KB
testcase_46 AC 15 ms
5,956 KB
testcase_47 AC 5 ms
4,348 KB
testcase_48 AC 4 ms
4,348 KB
testcase_49 AC 5 ms
4,348 KB
testcase_50 AC 5 ms
4,348 KB
testcase_51 AC 5 ms
4,348 KB
testcase_52 AC 4 ms
4,348 KB
testcase_53 AC 5 ms
4,348 KB
testcase_54 AC 4 ms
4,348 KB
testcase_55 AC 4 ms
4,348 KB
testcase_56 AC 6 ms
4,348 KB
testcase_57 AC 5 ms
4,348 KB
testcase_58 AC 5 ms
4,348 KB
testcase_59 AC 5 ms
4,348 KB
testcase_60 AC 6 ms
4,348 KB
testcase_61 AC 6 ms
4,348 KB
testcase_62 AC 6 ms
4,348 KB
testcase_63 AC 48 ms
20,824 KB
testcase_64 AC 6 ms
4,348 KB
testcase_65 AC 45 ms
18,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n)  decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
auto fl = [](auto a, auto b) { assert(b != 0); return a / b - (a % b != 0 && ((a >= 0) != (b >= 0)) ? 1 : 0); };
auto ce = [](auto a, auto b) { assert(b != 0); return a / b + (a % b != 0 && ((a >= 0) == (b >= 0)) ? 1 : 0); };
auto mo = [](auto a, auto b) { assert(b != 0); a %= b; if(a < 0) { a += abs(b); } return a; };
LL gcd(LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); }
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)

// ---- ----

class UnionFind {
private:
	int n, s;
	vector<int> t;
	vector<vector<int>> v;
public:
	UnionFind(int arg_n = 0) { init(arg_n); }
	void init(int arg_n) {
		n = s = arg_n;
		t.clear();
		v.clear();
		inc(i, n) { t.PB(i); v.EB(1, i); }
	}
	int get_n() { return n; }
	int size() { return s; }
	int id(int x) { return t.at(x); }
	const vector<vector<int>> & get_v() { return v; }
	bool unite(int x, int y) {
		x = id(x);
		y = id(y);
		if(x == y) { return false; }
		if(v[x].size() < v[y].size()) { swap(x, y); }
		for(auto & e: v[y]) { v[x].PB(e); t[e] = x; }
		v[y].clear();
		s--;
		return true;
	}
	bool same(int x, int y) { return (id(x) == id(y)); }
	const vector<int> & operator[](int x) { return v[id(x)]; }
	friend ostream & operator<<(ostream & os, const UnionFind & uf) {
		inc(i, uf.n) { os << i << ": "; for(auto & e: uf.v[i]) { os << e << " "; } os << "\n"; }
		return os;
	}
};

// ---- ----

int main() {
	int n;
	cin >> n;
	vector<string> s(n);
	inc(i, n) { cin >> s[i]; }
	
	auto lcp = [&](int x, int y) {
		int i = 0;
		until(i == min(SI(s[x]), SI(s[y])) || s[x][i] != s[y][i]) { i++; }
		return i;
	};
	
	vector<pair<LL, LL>> v;
	inc(i, n - 1) { v.EB(lcp(i, i + 1), i); }
	sort(RALL(v));
	
	UnionFind uf(n);
	LL ans = 0;
	inc(i, n) { ans += SI(s[i]); }
	inc(i, n - 1) {
		LL L = v[i].FI;
		LL p = v[i].SE;
		LL x = SI(uf[p    ]);
		LL y = SI(uf[p + 1]);
		ans += L * x * y * (x + y + 2) / 2;
		uf.unite(p, p + 1);
	}
	
	cout << ans << endl;
	
	return 0;
}
0