답안 #42260

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
42260 2018-02-24T11:39:17 Z fest 회문 (APIO14_palindrome) C++14
100 / 100
438 ms 50492 KB
// fest
#include <bits/stdc++.h>	

#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define FILENAME ""
#define __ fflush(stdout)

typedef long long ll;
typedef long double ld;    

using namespace std;

void FREOPEN() {
	#ifdef COMP
		freopen(".in", "r", stdin);
		freopen("2.out", "w", stdout);
	#endif
}                           

inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; }             

const int N = 300500, M = 21, inf = 1e9 * 2, MOD = (int)1e9 + 7;

char CH[N];

const ll INF = 1e18;

const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};

int n;

int pos[N], suff[N], c[M][N], new_suff[N], lcp[N], ans[N], dp[N], rev[N];

vector<int> go[N];

int range(int x) {
	while (x >= n) x -= n;
	while (x < 0) x += n;
	return x;
}

int main() {
  
	FREOPEN();
	scanf("%s", CH);
	string s = CH;
	s += '#';   
	n = SZ(s);
	for (int i = 0; i < n; i++) {
		c[0][i] = (int)(s[i] - '#');
		pos[c[0][i] + 1]++;
	}
	for (int i = 0; i < N - 1; i++) pos[i + 1] += pos[i];
	for (int i = 0; i < n; i++) {
		suff[pos[c[0][i]]++] = i;
	}
	for (int len = 0; (1 << len) <= n; len++) {
		memset(pos, 0, sizeof(pos));
		for (int i = 0; i < n; i++) {
			int f = range(suff[i] - (1 << len));
			pos[c[len][f] + 1]++;
		}
		for (int i = 0; i < N - 1; i++) pos[i + 1] += pos[i];
		for (int i = 0; i < n; i++) {
			int f = range(suff[i] - (1 << len));
			new_suff[pos[c[len][f]]++] = f;		
		}
		memcpy(suff, new_suff, sizeof(new_suff));
		c[len + 1][suff[0]] = 0;
		int C = 0;
		for (int i = 1; i < n; i++) {
			int ff = range(suff[i - 1] + (1 << len)), ss = range(suff[i] + (1 << len));
			if (c[len][suff[i - 1]] < c[len][suff[i]] || c[len][ff] < c[len][ss]) C++;
			c[len + 1][suff[i]] = C;
		}
	}
	for (int i = 1; i < n - 1; i++) {
		int p1 = suff[i], p2 = suff[i + 1];
		for (int j = M - 1; j >= 0; j--) {
			if (max(p1, p2) + (1 << j) >= n) continue;
			if (c[j][p1] == c[j][p2]) {
				lcp[i] += (1 << j);	
				p1 += (1 << j);
				p2 += (1 << j);
      }
		}
	}
	vector<pair<int, int> > st;
	for (int i = 1; i < n - 1; i++) {
		while (!st.empty() && st.back().F >= lcp[i]) st.pop_back();
		if (!st.empty()) ans[suff[i]] -= st.back().S;
		st.pb({lcp[i], i});
	}
	st.clear();
	for (int i = n - 2; i >= 1; i--) {
		while (!st.empty() && st.back().F >= lcp[i]) st.pop_back();
		if (!st.empty()) ans[suff[i]] += st.back().S - 1;
		else ans[suff[i]] += n - 2;
		st.pb({lcp[i], i});
	}
	for (int i = 1; i < n; i++) rev[suff[i]] = i;
	ll out = 0;
	for (int i = 0, l = 0, r = 0; i < n - 1; i++) {
		dp[i] = 1;
		if (i <= r) dp[i] = min(dp[l + (r - i)], r - i + 1);
		while (i - dp[i] >= 0 && i + dp[i] < n - 1 && s[i - dp[i]] == s[i + dp[i]]) dp[i]++;
		if (i + dp[i] - 1 > r) l = i - dp[i] + 1, r = i + dp[i] - 1;
		out = max(out, dp[i] * 2ll - 1); 
	}
	vector<int> st2;
	for (int l = 0; l < n - 1; l++) {
		int r = (l + l + lcp[rev[l]] - 1) / 2;
		while (!st2.empty() && st2.back() - dp[st2.back()] + 1 >= l - dp[l] + 1) st2.pop_back();
		st2.pb(l);
		if (lcp[rev[l]]) go[r].pb(l);
		while (!go[l].empty()) {
			int now = go[l].back();
			go[l].pop_back();
			int lef = 0;
			int rig = SZ(st2) - 1;
			int pos = -1;
			while (lef <= rig) {
				int mid = (lef + rig) / 2;
				if (st2[mid] - dp[st2[mid]] + 1 <= now) pos = st2[mid], lef = mid + 1;
				else rig = mid - 1;
			}
			if (pos >= now) out = max(out, ((pos - now) * 2 + 1) * 1ll * (ans[now] + 1));
		}
	}
	st2.clear();
	memset(dp, 0, sizeof(dp));
	for (int i = 1, l = 0, r = 0; i < n - 1; i++) {
		if (s[i] != s[i - 1]) continue;
		dp[i] = 1;
		if (i <= r) dp[i] = min(dp[l + (r - i + 1)], r - i + 1);
		while (i - dp[i] - 1 >= 0 && i + dp[i] < n - 1 && s[i - dp[i] - 1] == s[i + dp[i]]) dp[i]++;
		if (i + dp[i] - 1 > r) l = i - dp[i], r = i + dp[i] - 1;
		out = max(out, dp[i] * 2ll); 
	}                         
	for (int l = 1; l < n - 1; l++) {
		int r = (l + l + lcp[rev[l]]) / 2;
		while (!st2.empty() && st2.back() - dp[st2.back()] >= l - dp[l]) st2.pop_back();
		st2.pb(l);
		if (lcp[rev[l]]) go[r].pb(l);
		while (!go[l].empty()) {
			int now = go[l].back();
			go[l].pop_back();
			int lef = 0;
			int rig = SZ(st2) - 1;
			int pos = -1;
			while (lef <= rig) {
				int mid = (lef + rig) / 2;
				if (st2[mid] - dp[st2[mid]] <= now) pos = st2[mid], lef = mid + 1;
				else rig = mid - 1;
			}
			if (pos - 1 >= now) out = max(out, ((pos - now) * 2) * 1ll * (ans[now] + 1));
		}
	}
	cout << out;
	return 0;	
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:53:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", CH);
                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 10872 KB Output is correct
2 Correct 13 ms 10976 KB Output is correct
3 Correct 12 ms 11140 KB Output is correct
4 Correct 11 ms 11140 KB Output is correct
5 Correct 11 ms 11164 KB Output is correct
6 Correct 11 ms 11180 KB Output is correct
7 Correct 12 ms 11300 KB Output is correct
8 Correct 11 ms 11300 KB Output is correct
9 Correct 13 ms 11300 KB Output is correct
10 Correct 12 ms 11300 KB Output is correct
11 Correct 12 ms 11300 KB Output is correct
12 Correct 12 ms 11300 KB Output is correct
13 Correct 12 ms 11300 KB Output is correct
14 Correct 12 ms 11300 KB Output is correct
15 Correct 13 ms 11300 KB Output is correct
16 Correct 15 ms 11300 KB Output is correct
17 Correct 13 ms 11300 KB Output is correct
18 Correct 15 ms 11300 KB Output is correct
19 Correct 13 ms 11300 KB Output is correct
20 Correct 13 ms 11372 KB Output is correct
21 Correct 13 ms 11372 KB Output is correct
22 Correct 13 ms 11372 KB Output is correct
23 Correct 13 ms 11372 KB Output is correct
24 Correct 13 ms 11372 KB Output is correct
25 Correct 13 ms 11372 KB Output is correct
26 Correct 13 ms 11372 KB Output is correct
27 Correct 13 ms 11372 KB Output is correct
28 Correct 13 ms 11372 KB Output is correct
29 Correct 13 ms 11372 KB Output is correct
30 Correct 13 ms 11372 KB Output is correct
31 Correct 14 ms 11372 KB Output is correct
32 Correct 13 ms 11372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 11372 KB Output is correct
2 Correct 14 ms 11372 KB Output is correct
3 Correct 14 ms 11372 KB Output is correct
4 Correct 15 ms 11504 KB Output is correct
5 Correct 15 ms 11504 KB Output is correct
6 Correct 14 ms 11504 KB Output is correct
7 Correct 14 ms 11504 KB Output is correct
8 Correct 16 ms 11504 KB Output is correct
9 Correct 14 ms 11504 KB Output is correct
10 Correct 14 ms 11504 KB Output is correct
11 Correct 15 ms 11504 KB Output is correct
12 Correct 14 ms 11504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 12268 KB Output is correct
2 Correct 20 ms 12272 KB Output is correct
3 Correct 20 ms 12304 KB Output is correct
4 Correct 21 ms 12304 KB Output is correct
5 Correct 21 ms 12304 KB Output is correct
6 Correct 21 ms 12304 KB Output is correct
7 Correct 20 ms 12304 KB Output is correct
8 Correct 21 ms 12396 KB Output is correct
9 Correct 21 ms 12396 KB Output is correct
10 Correct 23 ms 12396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 22628 KB Output is correct
2 Correct 73 ms 22628 KB Output is correct
3 Correct 64 ms 22628 KB Output is correct
4 Correct 65 ms 22628 KB Output is correct
5 Correct 91 ms 22628 KB Output is correct
6 Correct 89 ms 22628 KB Output is correct
7 Correct 77 ms 22628 KB Output is correct
8 Correct 133 ms 23616 KB Output is correct
9 Correct 102 ms 23616 KB Output is correct
10 Correct 93 ms 23616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 48292 KB Output is correct
2 Correct 199 ms 48292 KB Output is correct
3 Correct 174 ms 48292 KB Output is correct
4 Correct 182 ms 48292 KB Output is correct
5 Correct 279 ms 48292 KB Output is correct
6 Correct 225 ms 48292 KB Output is correct
7 Correct 243 ms 48292 KB Output is correct
8 Correct 435 ms 50480 KB Output is correct
9 Correct 438 ms 50492 KB Output is correct
10 Correct 289 ms 50492 KB Output is correct
11 Correct 191 ms 50492 KB Output is correct
12 Correct 422 ms 50492 KB Output is correct