結果

問題 No.515 典型LCP
ユーザー __math__math
提出日時 2017-05-05 23:04:35
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 10,015 bytes
コンパイル時間 2,260 ms
コンパイル使用メモリ 183,932 KB
実行使用メモリ 44,160 KB
最終ジャッジ日時 2024-09-14 09:04:45
合計ジャッジ時間 6,231 ms
ジャッジサーバーID
(参考情報)
judge1 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())
#define ten(n) ((int)1e##n)
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

template<typename ...> static inline int getchar_unlocked(void) { return getchar(); }
template<typename ...> static inline void putchar_unlocked(int c) { putchar(c); }
#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
void reader(int& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k&&k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; }
void reader(ll& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k&&k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; }
int reader(char c[]) { int i, s = 0; for (;;) { mygc(i); if (i != ' '&&i != '\n'&&i != '\r'&&i != '\t'&&i != EOF) break; }c[s++] = i; for (;;) { mygc(i); if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF) break; c[s++] = i; }c[s] = '\0'; return s; }
int reader(string& c) { int i; for (;;) { mygc(i); if (i != ' '&&i != '\n'&&i != '\r'&&i != '\t'&&i != EOF) break; }c.push_back(i); for (;;) { mygc(i); if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF) break; c.push_back(i); }; return sz(c); }
template <class T, class S> void reader(T& x, S& y) { reader(x); reader(y); }
template <class T, class S, class U> void reader(T& x, S& y, U& z) { reader(x); reader(y); reader(z); }
template <class T, class S, class U, class V> void reader(T& x, S& y, U& z, V & w) { reader(x); reader(y); reader(z); reader(w); }
void writer(int x, char c) { int s = 0, m = 0; char f[10]; if (x<0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); }
void writer(ll x, char c) { int s = 0, m = 0; char f[20]; if (x<0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); }
void writer(const char c[]) { int i; for (i = 0; c[i] != '\0'; i++)mypc(c[i]); }
void writer(const string& x, char c) { int i; for (i = 0; x[i] != '\0'; i++)mypc(x[i]); mypc(c); }
void writer(const char x[], char c) { int i; for (i = 0; x[i] != '\0'; i++)mypc(x[i]); mypc(c); }
template<class T> void writerLn(T x) { writer(x, '\n'); }
template<class T, class S> void writerLn(T x, S y) { writer(x, ' '); writer(y, '\n'); }
template<class T, class S, class U> void writerLn(T x, S y, U z) { writer(x, ' '); writer(y, ' '); writer(z, '\n'); }
template<class T> void writerArr(T x[], int n) { if (!n) { mypc('\n'); return; }FOR(i, n - 1)writer(x[i], ' '); writer(x[n - 1], '\n'); }
template<class T> void writerArr(vector<T>& x) { writerArr(x.data(), (int)x.size()); }

template<class T> void chmin(T& a, const T& b) { if (a > b) a = b; }
template<class T> void chmax(T& a, const T& b) { if (a < b) a = b; }

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
ll mod_pow(ll a, ll n, ll mod) {
	ll ret = 1;
	ll p = a % mod;
	while (n) {
		if (n & 1) ret = ret * p % mod;
		p = p * p % mod;
		n >>= 1;
	}
	return ret;
}
template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; }
template<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; }

template<class val_t>
struct min_t {
	val_t val;
	static min_t zero() { return min_t(); }
	min_t() : val(numeric_limits<val_t>::max()) {}
	min_t(val_t val) : val(val) {}
	operator val_t () const { return val; }
	bool is_zero() const { return val == zero().val; }
	min_t operator+ (const min_t& r) const {
		if (is_zero()) return r;
		else if (r.is_zero()) return *this;

		min_t ret(min(val, r.val));
		return ret;
	}
};

//0-indexed segment tree
template<class P>
class seg_tree {
public:
	vector<P> dat;
	int n;

	void propagate(int i) {
		dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];
	}

	void init(P* d, int size) {
		n = 1;
		while (n < size) n <<= 1;
		dat.resize(2 * n - 1);
		fill(dat.begin(), dat.end(), P::zero());
		for (int i = 0; i < size; i++) dat[n - 1 + i] = d[i];
		for (int i = n - 2; i >= 0; i--) propagate(i);
	}

	void update(int k, P val) {
		k += n - 1;
		dat[k] = val;
		// dat[k] = dat[k] + val;
		while (k > 0) {
			k = (k - 1) / 2;
			propagate(k);
		}
	}

	//[a,b)
	P query(int a, int b) {
		return query(a, b, 0, 0, n);
	}

	P query(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l) return P::zero();
		if (a <= l && r <= b) return dat[k];

		int md = (l + r) / 2; //[l,md),[md,r)
		int nl = k * 2 + 1, nr = nl + 1;
		P lval = query(a, b, nl, l, md);
		P rval = query(a, b, nr, md, r);

		return lval + rval;
	}
};

vector<int>sais(const vector<int>& vec)//1-originにして末尾に0を加えてから呼ぶのを忘れないこと
{
	if (vec.empty()) {
		vector<int>v;
		return v;
	}
	vector<int>dat;
	dat.resize(vec.size());
	dat[vec.size() - 1] = 0;
	for (int i = vec.size() - 2; i >= 0; i--) {
		if (vec[i]>vec[i + 1])dat[i] = 1;
		else if (vec[i]<vec[i + 1])dat[i] = 0;
		else dat[i] = dat[i + 1];
	}
	vector<vector<int> >sa;
	int maxi = 0;
	FOR(i, sz(vec)) maxi = max(maxi, vec[i]);
	sa.resize(maxi + 1);
	vector<int>vnum;
	vnum.resize(maxi + 1);
	FOR(i, sz(vec)) vnum[vec[i]]++;
	vector<bool>islms;
	islms.resize(vec.size());
	fill(islms.begin(), islms.end(), false);
	for (int i = 0; i <= maxi; i++) {
		sa[i].resize(vnum[i]);
		fill(sa[i].begin(), sa[i].end(), -1);
	}
	vector<int>pt1, pt2;
	pt1.resize(maxi + 1);
	pt2.resize(maxi + 1);
	for (int i = 0; i <= maxi; i++) {
		pt2[i] = vnum[i] - 1;
	}
	for (int i = vec.size() - 1; i >= 1; i--) {
		if ((dat[i - 1] == 1 && dat[i] == 0) || i == vec.size() - 1) {
			sa[vec[i]][pt2[vec[i]]--] = i;
			islms[i] = true;
		}
	}
	for (int i = 0; i <= maxi; i++) {
		for (int j = 0; j<vnum[i]; j++) {
			if (sa[i][j]>0) {
				if (dat[sa[i][j] - 1] == 1) {
					sa[vec[sa[i][j] - 1]][pt1[vec[sa[i][j] - 1]]++] = sa[i][j] - 1;
				}
			}
		}
	}
	for (int i = 1; i <= maxi; i++) {
		for (int j = pt2[i] + 1; j<vnum[i]; j++)sa[i][j] = -1;
		pt2[i] = vnum[i] - 1;
	}
	for (int i = maxi; i >= 0; i--) {
		for (int j = vnum[i] - 1; j >= 0; j--) {
			if (sa[i][j]>0) {
				if (dat[sa[i][j] - 1] == 0) {
					sa[vec[sa[i][j] - 1]][pt2[vec[sa[i][j] - 1]]--] = sa[i][j] - 1;
				}
			}
		}
	}
	vector<int>d;
	d.resize(vec.size());
	int cnt = 0;
	vector<int>bef;
	bef.push_back(0);
	bool fl = false;
	for (int i = 0; i <= maxi; i++) {
		for (int j = 0; j<vnum[i]; j++) {
			if (islms[sa[i][j]]) {
				vector<int>zk;
				zk.push_back(vec[sa[i][j]]);
				for (int k = sa[i][j] + 1; k< sz(vec); k++) {
					zk.push_back(vec[k]);
					if (islms[k])break;
				}
				if (zk != bef)cnt++;
				else if (vec[sa[i][j]] != 0)fl = true;
				d[sa[i][j]] = cnt;
				bef = zk;
			}
		}
	}
	vector<int>vt;
	FOR(i, sz(vec)) if (islms[i])vt.push_back(d[i]);
	vector<int>gv;
	vector<int>nv;
	if (fl) {
		gv = sais(vt);
		vector<int>v;
		FOR(i, sz(vec)) {
			if (islms[i])v.push_back(i);
		}
		FOR(i, sz(gv)) {
			nv.push_back(v[gv[i]]);
		}
	} else {
		gv = vt;
		nv.resize(gv.size());
		int pt = 0;
		for (int i = 0; i< sz(gv); i++) {
			for (;;) {
				if (islms[pt]) {
					nv[gv[i]] = pt;
					pt++;
					break;
				}
				pt++;
			}
		}
	}
	for (int i = 0; i <= maxi; i++) {
		fill(sa[i].begin(), sa[i].end(), -1);
		pt1[i] = 0;
		pt2[i] = vnum[i] - 1;
	}
	for (int i = sz(nv) - 1; i >= 0; i--) {
		sa[vec[nv[i]]][pt2[vec[nv[i]]]--] = nv[i];
	}
	for (int i = 0; i <= maxi; i++) {
		for (int j = 0; j<vnum[i]; j++) {
			if (sa[i][j]>0) {
				if (dat[sa[i][j] - 1] == 1) {
					sa[vec[sa[i][j] - 1]][pt1[vec[sa[i][j] - 1]]++] = sa[i][j] - 1;
				}
			}
		}
	}
	for (int i = 1; i <= maxi; i++) {
		for (int j = pt2[i] + 1; j<vnum[i]; j++)sa[i][j] = -1;
		pt2[i] = vnum[i] - 1;
	}
	for (int i = maxi; i >= 0; i--) {
		for (int j = vnum[i] - 1; j >= 0; j--) {
			if (sa[i][j]>0) {
				if (dat[sa[i][j] - 1] == 0) {
					sa[vec[sa[i][j] - 1]][pt2[vec[sa[i][j] - 1]]--] = sa[i][j] - 1;
				}
			}
		}
	}
	vector<int>ret;
	for (int i = 0; i <= maxi; i++) {
		for (int j = 0; j<vnum[i]; j++) {
			ret.push_back(sa[i][j]);
		}
	}
	return ret;
}
vector<int> calclcp(const vector<int>& str, vector<int>& sa)//lcp: SAのi-1番目とi番目のlcp
{
	vector<int>rsa;
	rsa.resize(sa.size());
	FOR(i, sz(sa)) rsa[sa[i]] = i;
	vector<int>lcp;
	lcp.resize(sa.size());
	int now = 1;
	for (int i = 0; i < sz(str) - 1; i++) {
		if (now != 0)now--;
		for (;;) {
			if (str[i + now] == str[sa[rsa[i] - 1] + now])now++;
			else {
				if(rsa[i]) lcp[rsa[i] - 1] = now;
				break;
			}
		}
	}
	return lcp;
}


class lcp_seg_tree {
	string s;
	vector<int> sa, isa, lcp;
	seg_tree<min_t<int>> seg;
public:
	lcp_seg_tree(string& s) : s(s) {
		vector<int> sint;
		for (auto c : s) sint.push_back(c);
		sint.push_back(0);

		sa = sais(sint);
		lcp = calclcp(sint, sa);
		vector<min_t<int>> vp;
		FOR(i, sz(lcp)) vp.emplace_back(lcp[i]);
		seg.init(&vp[0], sz(vp));
		isa.resize(sz(sa));
		FOR(i, sz(sa)) isa[sa[i]] = i;
	}
	int query(int l, int r) {
		int il = isa[l], ir = isa[r];
		if (il > ir) swap(il, ir);
		return seg.query(il, ir);
	}
};

int main() {
	int n; reader(n);
	string s;
	vector<int> idx;
	vector<int> orilen;
	FOR(i, n) {
		idx.push_back(sz(s));

		string a; reader(a);
		for (auto c : a) s.push_back(c);
		s.push_back(';');
		orilen.push_back(sz(a));
	}

	lcp_seg_tree lst(s);

	int m;
	ll x, d; reader(m, x, d);
	const ll nmod = ll(n) * (n - 1);


	return 0;

	ll ans = 0;
	FOR(_, m) {
		int i = (x / (n - 1)) + 1;
		int j = (x % (n - 1)) + 1;
		if (i > j) swap(i, j);
		else j++;

		x = (x + d) % nmod;

		i--;
		j--;
		int len = lst.query(idx[i], idx[j]);
		len = min(len, min(orilen[i], orilen[j]));

		ans += len;
	}
	writerLn(ans);

	return 0;
}
0