Submission #124951

# Submission time Handle Problem Language Result Execution time Memory
124951 2019-07-04T07:53:41 Z 윤교준(#3050) Necklace (Subtask 1-3) (BOI19_necklace1) C++14
45 / 85
1500 ms 444 KB
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[sz(V)-2])
#define upmax(a,b) (a)=max((a),(b))
#define INF (0x3f3f3f3f)
using namespace std;
typedef long long ll;

struct HSH {
	HSH(int mod, int key) : MOD(mod), KEY(key) {}
	const int MOD, KEY;

	int T[3005];
	int A[3005], B[3005];

	int getA(int s, int e) {
		return ((A[e] - ll(T[e-s+1]) * A[s-1]) % MOD + MOD) % MOD;
	}
	int getB(int s, int e) {
		return ((B[s] - ll(T[e-s+1]) * B[e+1]) % MOD + MOD) % MOD;
	}
	bool cmp(int a, int b, int l) {
		return getA(a, a+l-1) == getB(b, b+l-1);
	}
} hsh(1000002173, 6481);

char A[3005], B[3005];

int AnsL, AnsI, AnsJ;
int N, M;

bool __isrev;
void upd(int l, int as, int bs) {
	if(l <= AnsL) return;
	AnsL = l;
	AnsI = __isrev ? N-as-l+2 : as;
	AnsJ = bs;
}

void precal(HSH &hsh) {
	const int MOD = hsh.MOD, KEY = hsh.KEY;
	if(!__isrev) {
		hsh.T[0] = 1;
		for(int i = 1; i < 3005; i++)
			hsh.T[i] = ll(hsh.T[i-1]) * KEY % MOD;
		for(int i = M; i; i--)
			hsh.B[i] = (ll(hsh.B[i+1]) * KEY + (B[i]-'a')) % MOD;
	}
	for(int i = 1; i <= N; i++)
		hsh.A[i] = (ll(hsh.A[i-1]) * KEY + (A[i]-'a')) % MOD;
}

int LH[3005], RH[3005];
int L[3005], R[3005];
void process() {
	precal(hsh);

	for(int delta = -N+1; delta <= M-1; delta++) {
		memset(L, 0, 3005<<2); memset(LH, 0, 3005<<2);
		memset(R, 0, 3005<<2); memset(RH, 0, 3005<<2);

		for(int i = 1; i <= N; i++) {
			{
				int mxl = -1;
				int s = 0, e = N; for(int m, as, ae, bs, be; s <= e;) {
					m = (s+e) >> 1;
					as = i - m; ae = i + m;
					bs = as + delta; be = ae + delta;
					if(as < 1 || N < ae || bs < 1 || M < be || !hsh.cmp(as, bs, m<<1|1)) e = m-1;
					else {
						if(mxl < m) mxl = m;
						s = m+1;
					}
				}
				if(0 <= mxl) {
					upmax(LH[i+mxl+1], mxl<<1|1);
					upmax(RH[i-mxl], mxl<<1|1);
				}
			}
			{
				int mxl = -1;
				int s = 0, e = N; for(int m, as, ae, bs, be; s <= e;) {
					m = (s+e) >> 1;
					as = i - m; ae = i + m + 1;
					bs = as + delta; be = ae + delta;
					if(as < 1 || N < ae || bs < 1 || M < be || !hsh.cmp(as, bs, (m+1)<<1)) e = m-1;
					else {
						if(mxl < m) mxl = m;
						s = m+1;
					}
				}
				if(0 <= mxl) {
					upmax(LH[i+mxl+2], (mxl+1)<<1);
					upmax(RH[i-mxl], (mxl+1)<<1);
				}
			}
		}

		for(int i = max(N, M), h = 0; i; i--) {
			if(h < LH[i]) h = LH[i];
			L[i] = h;
			h -= 2;
			if(h < 0) h = 0;
		}
		for(int i = 1, h = 0, t = max(N, M); i <= t; i++) {
			if(h < RH[i]) h = RH[i];
			R[i] = h;
			h -= 2;
			if(h < 0) h = 0;
		}

		for(int i = 1; i <= N; i++)
			upd(L[i] + R[i], i-L[i], i-L[i]+delta);
	}
}

int main() {
	scanf(" %s %s", A+1, B+1);
	N = strlen(A+1);
	M = strlen(B+1);

	for(int i = 1; i <= N; i++) for(int j = 1; j <= M; j++)
		if(A[i] == B[j]) upd(1, i, j);

	process();
	reverse(A+1, A+N+1);
	__isrev = true;
	process();

	cout << AnsL << endl << AnsI-1 << " " << AnsJ-1 << endl;
	return 0;
}

Compilation message

necklace.cpp: In function 'int main()':
necklace.cpp:120:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf(" %s %s", A+1, B+1);
  ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 376 KB Output is correct
2 Correct 14 ms 376 KB Output is correct
3 Correct 11 ms 376 KB Output is correct
4 Correct 12 ms 376 KB Output is correct
5 Correct 13 ms 424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 376 KB Output is correct
2 Correct 14 ms 376 KB Output is correct
3 Correct 11 ms 376 KB Output is correct
4 Correct 12 ms 376 KB Output is correct
5 Correct 13 ms 424 KB Output is correct
6 Correct 273 ms 444 KB Output is correct
7 Correct 260 ms 440 KB Output is correct
8 Correct 221 ms 380 KB Output is correct
9 Correct 245 ms 436 KB Output is correct
10 Correct 241 ms 436 KB Output is correct
11 Correct 239 ms 376 KB Output is correct
12 Correct 236 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 376 KB Output is correct
2 Correct 14 ms 376 KB Output is correct
3 Correct 11 ms 376 KB Output is correct
4 Correct 12 ms 376 KB Output is correct
5 Correct 13 ms 424 KB Output is correct
6 Correct 273 ms 444 KB Output is correct
7 Correct 260 ms 440 KB Output is correct
8 Correct 221 ms 380 KB Output is correct
9 Correct 245 ms 436 KB Output is correct
10 Correct 241 ms 436 KB Output is correct
11 Correct 239 ms 376 KB Output is correct
12 Correct 236 ms 376 KB Output is correct
13 Execution timed out 1559 ms 376 KB Time limit exceeded
14 Halted 0 ms 0 KB -