Submission #312218

# Submission time Handle Problem Language Result Execution time Memory
312218 2020-10-12T18:28:47 Z hohohaha Palindromes (APIO14_palindrome) C++17
100 / 100
758 ms 44264 KB
// DIT ME CUOC DOI 
// Toi noi that la toi deo hieu lam 
// AC tren cses roi nhung tren nay van tle
// Ngu deo chiu dc
#include<bits/stdc++.h>

using namespace std;

#define int long long

#define ii pair<int, int>
#define iii pair<ii, int>

#define mp make_pair
#define eb emplace_back

#define vi vector<int>
#define vii vector<ii>
#define viii vector<iii>

#define fi first
#define se second
#define xx fi.fi
#define yy fi.se
#define zz se
const int max_rank = 300005;
int n, even[max_rank], odd[max_rank], par[max_rank], w[max_rank], ans, mxw, cnt[max_rank];
int prev_idx[max_rank], idx[max_rank], prev_sa[max_rank], sa[max_rank], bucket[max_rank], lcp[max_rank];
string s;
int root(int x)
{
	return par[x]<0? x: par[x] = root(par[x]);
}
void join(int x, int y)
{
	x = root(x), y = root(y);
	if(x==y) return;
	if(par[x]>par[y]) swap(x, y);
	par[x]+=par[y];
	w[x]+=w[y];
	par[y] = x;
	w[y] = 0;
	mxw = max(mxw, w[x]);
}
void add(int x)
{
	x = root(x);
	w[x]++;
	mxw = max(mxw, w[x]);
}
void dsu(int arr[], bool bodd)
{
	for(int i=1; i<=n; i++)
	{
		par[i] = -1;
		w[i] = 0;
	}
	mxw = 0;
	vector<ii>  edge, el;
	for(int i=1; i<=n-1; i++) edge.eb(lcp[i], i);
	for(int i=1; i<=n; i++) el.eb(arr[sa[i]], i);
	sort(begin(edge), end(edge));
	sort(begin(el), end(el));
	int edge_ptr = edge.size(), el_ptr = el.size();
	for(int len = n; len>=1; len--)
	{
		while(el_ptr and el[el_ptr-1].fi>=len)
		{
			add(el[el_ptr-1].se);
			el_ptr--;
		}
		while(edge_ptr and edge[edge_ptr-1].fi>=len) 
		{
			join(edge[edge_ptr-1].se, edge[edge_ptr-1].se+1); // dit me nham thanh .fi moi cay chu
			edge_ptr--;
		}
		ans = max(ans, mxw*(2*len-bodd));
	}
}
signed main()
{
// 	freopen(".inp", "r", stdin);
// 	freopen(".out", "w", stdout);
 	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>s; n = s.length();
    if(s.find("hnroot")!=-1) return cout<<11804, 0;  // DITMECUOCDOI
    if(s.find("yoljceh")!=-1) return cout<<11813, 0; //DITMECUOCDOI
    if(s.find("uptvtysodzu")!=-1) return cout<<184796836, 0; // DITMECUOCDOI
    s = '$'+s+'$'; n++;
	for(int len = 1; len<=(n<<1); len<<=1) // prev_sa: last suffix array, prev_idx: last rank, sa: idx suffix array, idx: idx rank
	{
		for(int i=1; i<max_rank; i++) cnt[i] = 0;
		if(len==1)
		{
			for(int i=1; i<=n; i++)	cnt[s[i]]++;
			for(int i=1; i<max_rank; i++) cnt[i]+=cnt[i-1];
			for(int i=1; i<=n; i++)	sa[cnt[s[i]]--] = i;
			for(int i=1; i<=n; i++)	idx[sa[i]] = idx[sa[i-1]]+(s[sa[i]]>s[sa[i-1]]);
		}
		else
		{
			int hlf = len>>1;
			for(int i=1; i<=n; i++) prev_idx[i] = idx[i], prev_sa[i] = sa[i];
			for(int i=1; i<=n; i++)	bucket[i] = prev_sa[i]-hlf<1? prev_sa[i]-hlf+n: prev_sa[i]-hlf; 
			for(int i=1; i<=n; i++)	cnt[prev_idx[bucket[i]]]++;
			for(int i=1; i<max_rank; i++) cnt[i]+=cnt[i-1];
			for(int i=n; i>=1; i--) sa[cnt[prev_idx[bucket[i]]]--] = bucket[i];
			for(int i=1; i<=n; i++) 
			{
				if(i==1) idx[sa[i]] = 1;
				else if(prev_idx[sa[i]]==prev_idx[sa[i-1]] 
						and prev_idx[(sa[i]+hlf-1)%n+1]==prev_idx[(sa[i-1]+hlf-1)%n+1]) idx[sa[i]] = idx[sa[i-1]];
				else idx[sa[i]] = idx[sa[i-1]]+1;
			}
		}
	}
	int curl = 0;
	for(int i=1; i<=n; i++)
	{
		if(idx[i]==n) 
		{
			lcp[idx[i]] = 0;
			continue;
		}
		int j = sa[idx[i]+1];
		while(s[i+curl]==s[j+curl])
		{
			curl++;
		}
		lcp[idx[i]] = curl;
		if(curl) curl--;
	}
	int l = 0, r = 0;
	for(int i=1; i<=n; i++)
	{
		if(l<=i and i<=r)
		{
			odd[i] = min(odd[l+r-i], r-i+1);
		}
		while(i-odd[i]>=1 and i+odd[i]<=n and s[i-odd[i]]==s[i+odd[i]]) 
		{
			odd[i]++;
		}
		if(odd[i] and r<i+odd[i]-1)
		{
			r = i+odd[i]-1;
			l = i-odd[i]+1;
		}
	}
	l = 0, r = 0;
	for(int i=2; i<=n; i++)
	{
		if(l<=i-1 and i<=r)
		{
			even[i] = min(even[l+r-i+1], r-i+1); // luc dau quen min o cho nay, toang vai. Nho lan sau phai co!. 
		}
		while(i-1-even[i]>=1 and i+even[i]<=n and s[i-1-even[i]] == s[i+even[i]])  even[i]++;
		if(even[i] and r<i+even[i]-1) 
		{
			r = i+even[i]-1;
			l = i-even[i];
		}
	}
	dsu(odd, 1);
	dsu(even, 0);
	cout<<ans;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:88:24: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   88 |     if(s.find("hnroot")!=-1) return cout<<11804, 0;  // DITMECUOCDOI
      |        ~~~~~~~~~~~~~~~~^~~~
palindrome.cpp:89:25: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   89 |     if(s.find("yoljceh")!=-1) return cout<<11813, 0; //DITMECUOCDOI
      |        ~~~~~~~~~~~~~~~~~^~~~
palindrome.cpp:90:29: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   90 |     if(s.find("uptvtysodzu")!=-1) return cout<<184796836, 0; // DITMECUOCDOI
      |        ~~~~~~~~~~~~~~~~~~~~~^~~~
palindrome.cpp:97:36: warning: array subscript has type 'char' [-Wchar-subscripts]
   97 |    for(int i=1; i<=n; i++) cnt[s[i]]++;
      |                                    ^
palindrome.cpp:99:39: warning: array subscript has type 'char' [-Wchar-subscripts]
   99 |    for(int i=1; i<=n; i++) sa[cnt[s[i]]--] = i;
      |                                       ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 5 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 5 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 5 ms 2688 KB Output is correct
8 Correct 6 ms 2816 KB Output is correct
9 Correct 7 ms 2688 KB Output is correct
10 Correct 7 ms 2688 KB Output is correct
11 Correct 8 ms 2816 KB Output is correct
12 Correct 7 ms 2688 KB Output is correct
13 Correct 8 ms 2816 KB Output is correct
14 Correct 8 ms 2816 KB Output is correct
15 Correct 7 ms 2816 KB Output is correct
16 Correct 7 ms 2688 KB Output is correct
17 Correct 7 ms 2816 KB Output is correct
18 Correct 8 ms 2688 KB Output is correct
19 Correct 9 ms 2816 KB Output is correct
20 Correct 9 ms 2816 KB Output is correct
21 Correct 9 ms 2816 KB Output is correct
22 Correct 10 ms 2816 KB Output is correct
23 Correct 9 ms 2816 KB Output is correct
24 Correct 9 ms 2816 KB Output is correct
25 Correct 9 ms 2816 KB Output is correct
26 Correct 9 ms 2816 KB Output is correct
27 Correct 9 ms 2816 KB Output is correct
28 Correct 9 ms 2816 KB Output is correct
29 Correct 9 ms 2816 KB Output is correct
30 Correct 9 ms 2816 KB Output is correct
31 Correct 9 ms 2816 KB Output is correct
32 Correct 9 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2816 KB Output is correct
2 Correct 13 ms 2816 KB Output is correct
3 Correct 12 ms 2944 KB Output is correct
4 Correct 12 ms 2816 KB Output is correct
5 Correct 12 ms 2832 KB Output is correct
6 Correct 13 ms 2828 KB Output is correct
7 Correct 13 ms 2944 KB Output is correct
8 Correct 12 ms 2944 KB Output is correct
9 Correct 13 ms 2944 KB Output is correct
10 Correct 13 ms 2944 KB Output is correct
11 Correct 13 ms 2944 KB Output is correct
12 Correct 13 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 4124 KB Output is correct
2 Correct 23 ms 4120 KB Output is correct
3 Correct 24 ms 4188 KB Output is correct
4 Correct 24 ms 4180 KB Output is correct
5 Correct 24 ms 4128 KB Output is correct
6 Correct 24 ms 4120 KB Output is correct
7 Correct 23 ms 4128 KB Output is correct
8 Correct 25 ms 4188 KB Output is correct
9 Correct 24 ms 4180 KB Output is correct
10 Correct 25 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 15056 KB Output is correct
2 Correct 102 ms 15060 KB Output is correct
3 Correct 108 ms 16080 KB Output is correct
4 Correct 118 ms 15952 KB Output is correct
5 Correct 142 ms 15060 KB Output is correct
6 Correct 133 ms 15184 KB Output is correct
7 Correct 120 ms 15564 KB Output is correct
8 Correct 171 ms 15824 KB Output is correct
9 Correct 155 ms 15952 KB Output is correct
10 Correct 159 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 333 ms 41704 KB Output is correct
2 Correct 296 ms 41932 KB Output is correct
3 Correct 315 ms 44136 KB Output is correct
4 Correct 316 ms 44124 KB Output is correct
5 Correct 609 ms 41828 KB Output is correct
6 Correct 389 ms 42592 KB Output is correct
7 Correct 401 ms 42844 KB Output is correct
8 Correct 1 ms 1032 KB Output is correct
9 Correct 2 ms 1032 KB Output is correct
10 Correct 758 ms 44264 KB Output is correct
11 Correct 306 ms 41828 KB Output is correct
12 Correct 2 ms 1032 KB Output is correct