Submission #368175

# Submission time Handle Problem Language Result Execution time Memory
368175 2021-02-19T17:05:31 Z AriaH Palindromes (APIO14_palindrome) C++11
100 / 100
865 ms 29388 KB
/** Im the best because i work as hard as i possibly can **/
 
#include <bits/stdc++.h>
using namespace std;
 
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
 
const int N = 1e6 + 10;
const ll mod = 884577481;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;
const ll base = 609859;
 
string s;
 
char C[N];
 
int n, rnk[N], SA[N], tmp[N], Cnt[N], lcp[N], seg[N << 2];
 
ll pw[N], hshL[N], hshR[N];

inline void BuildSuffixArray(){
    assert((s[0] == '$') && (n + 1 == (int)s.size()));
    int _pw = 0;
    iota(SA + 1, SA + n + 1, 1), SA[0] = n + 1;
    for(int i = 1; i <= n; i ++) rnk[i] = s[i];
    int _Max = 'z', p = 1, k;
    for(_pw = 0; p < n; _pw ++){
        k = ((1LL << _pw) >> 1), p = 1;
        for(int i = n - k + 1; i <= n; i ++){
            tmp[p ++] = i;
        }
        for(int i = 1; i <= n; i ++){
            if(SA[i] > k) tmp[p ++] = SA[i] - k;
        }
        for(int i = 0; i <= _Max; i ++) Cnt[i] = 0;
        for(int i = 1; i <= n; i ++) Cnt[rnk[i]] ++;
        for(int i = 1; i <= _Max; i ++){
            Cnt[i] += Cnt[i - 1];
        }
        for(int i = n; i; i --){
            SA[Cnt[rnk[tmp[i]]] --] = tmp[i];
        }
        swap(rnk, tmp);
        rnk[SA[1]] = p = 1;
        for(int i = 2; i <= n; i ++){
            if(tmp[SA[i - 1]] != tmp[SA[i]] || SA[i - 1] + k > n || tmp[SA[i - 1] + k] != tmp[SA[i] + k]){
                p ++;
            }
            rnk[SA[i]] = p;
        }
        _Max = p;
    }
    SA[0] = n + 1;
}
inline void BuildLcpTable(){
    assert((s[0] == '$') && (n + 1 == (int)s.size()));
    for(int i = 1, k = 0; i <= n; i ++){
        if(rnk[i] == n) continue;
        if(k) k --;
        while(s[i + k] == s[SA[rnk[i] + 1] + k]){
            k ++;
        }
        lcp[rnk[i] + 1] = k;
    }
}

inline ll baze(int l, int r)
{
	return (hshL[r] - (hshL[l - 1] * pw[r - l + 1] % mod) + mod) % mod;
}
 
inline ll baze2(int l, int r)
{
	return (hshR[l] - (hshR[r + 1] * pw[r - l + 1] % mod) + mod) % mod;
}
 
void build(int v = 1, int tl = 1, int tr = n)
{
	if(tl == tr)
	{
		seg[v] = lcp[tl];
		return;
	}
	int mid = (tl + tr) >> 1;
	build(v << 1, tl, mid), build(v << 1 | 1, mid + 1, tr);
	seg[v] = min(seg[v << 1], seg[v << 1 | 1]);
}
 
int find_nxt(int p, int x, int v = 1, int tl = 1, int tr = n)
{
	if(p > n) return n + 1;
	if(tr < p) return -1;
	if(seg[v] >= x)
	{
		if(tr == n) return n + 1;
		return -1;
	}
	if(tl == tr) return tl;
	int mid = (tl + tr) >> 1;
	int cu = find_nxt(p, x, v << 1, tl, mid);
	if(cu == -1) cu = find_nxt(p, x, v << 1 | 1, mid + 1, tr);
	return cu;
}
 
int find_prv(int p, int x, int v = 1, int tl = 1, int tr = n)
{
	if(tl > p) return -1;
	if(seg[v] >= x)
	{
		if(tl == 1) return 0;
		return -1;
	}
	if(tl == tr) return tl;
	int mid = (tl + tr) >> 1;
	int cu = find_prv(p, x, v << 1 | 1, mid + 1, tr);
	if(cu == -1) cu = find_prv(p, x, v << 1, tl, mid);
	return cu;
}
 
ll calc(int l, int r)
{
	int sz = r - l + 1, id = rnk[l];
	int L = find_prv(id, sz);
	int R = find_nxt(id + 1, sz);
	return R - L;
}
 
int main()
{
	scanf("%s", C);
	n = strlen(C);
	s = C;
	s = "$" + s;
	pw[0] = 1;
	for(int i = 1; i < N; i ++) pw[i] = pw[i - 1] * base % mod;
	for(int i = 1; i <= n; i ++)
	{
		hshL[i] = (hshL[i - 1] * base + s[i] - 'a' + 1) % mod;
	}
	for(int i = n; i >= 1; i --)
	{
		hshR[i] = (hshR[i + 1] * base + s[i] - 'a' + 1) % mod;
	}
	BuildSuffixArray();
	BuildLcpTable();
	build();
	ll tot = 0;
	for(int i = 1; i <= n; i ++)
	{
		int d = 0, up = min(i - 1, n - i) + 1;
		while(up - d > 1)
		{
			int mid = (up + d) >> 1;
			if(baze(i, i + mid) == baze2(i - mid, i)) d = mid;
			else up = mid;
		}
		tot = max(tot, calc(i - d, i + d) * (2 * d + 1));
	}
	for(int i = 1; i < n; i ++)
	{
		if(s[i] != s[i + 1]) continue;
		int d = 0, up = min(i - 1, n - i - 1) + 1;
		while(up - d > 1)
		{
			int mid = (up + d) >> 1;
			if(baze(i + 1, i + 1 + mid) == baze2(i - mid, i)) d = mid;
			else up = mid;
		}
		tot = max(tot, calc(i - d, i + d + 1) * (2 *  d + 2));
	}
	printf("%lld", tot);
	return 0;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:142:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  142 |  scanf("%s", C);
      |  ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 15992 KB Output is correct
2 Correct 20 ms 15980 KB Output is correct
3 Correct 19 ms 15980 KB Output is correct
4 Correct 10 ms 8172 KB Output is correct
5 Correct 19 ms 15980 KB Output is correct
6 Correct 19 ms 15980 KB Output is correct
7 Correct 18 ms 15980 KB Output is correct
8 Correct 20 ms 15980 KB Output is correct
9 Correct 20 ms 16000 KB Output is correct
10 Correct 21 ms 15980 KB Output is correct
11 Correct 20 ms 15980 KB Output is correct
12 Correct 21 ms 15980 KB Output is correct
13 Correct 21 ms 15980 KB Output is correct
14 Correct 24 ms 15980 KB Output is correct
15 Correct 18 ms 16064 KB Output is correct
16 Correct 19 ms 15980 KB Output is correct
17 Correct 21 ms 15980 KB Output is correct
18 Correct 20 ms 15980 KB Output is correct
19 Correct 73 ms 15980 KB Output is correct
20 Correct 75 ms 15980 KB Output is correct
21 Correct 41 ms 16128 KB Output is correct
22 Correct 41 ms 15980 KB Output is correct
23 Correct 25 ms 15980 KB Output is correct
24 Correct 44 ms 15980 KB Output is correct
25 Correct 27 ms 15980 KB Output is correct
26 Correct 22 ms 15980 KB Output is correct
27 Correct 26 ms 15980 KB Output is correct
28 Correct 48 ms 15980 KB Output is correct
29 Correct 22 ms 15980 KB Output is correct
30 Correct 19 ms 15980 KB Output is correct
31 Correct 22 ms 15980 KB Output is correct
32 Correct 26 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 16108 KB Output is correct
2 Correct 30 ms 16108 KB Output is correct
3 Correct 37 ms 16108 KB Output is correct
4 Correct 26 ms 16108 KB Output is correct
5 Correct 29 ms 16108 KB Output is correct
6 Correct 30 ms 16108 KB Output is correct
7 Correct 27 ms 16108 KB Output is correct
8 Correct 28 ms 16108 KB Output is correct
9 Correct 27 ms 16108 KB Output is correct
10 Correct 22 ms 16108 KB Output is correct
11 Correct 34 ms 16108 KB Output is correct
12 Correct 42 ms 16108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 16492 KB Output is correct
2 Correct 66 ms 16492 KB Output is correct
3 Correct 43 ms 16492 KB Output is correct
4 Correct 44 ms 16492 KB Output is correct
5 Correct 52 ms 16492 KB Output is correct
6 Correct 37 ms 16492 KB Output is correct
7 Correct 40 ms 16492 KB Output is correct
8 Correct 26 ms 16492 KB Output is correct
9 Correct 25 ms 16492 KB Output is correct
10 Correct 38 ms 16440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 20076 KB Output is correct
2 Correct 118 ms 20108 KB Output is correct
3 Correct 158 ms 20076 KB Output is correct
4 Correct 188 ms 20076 KB Output is correct
5 Correct 142 ms 20204 KB Output is correct
6 Correct 123 ms 20076 KB Output is correct
7 Correct 142 ms 20204 KB Output is correct
8 Correct 89 ms 20140 KB Output is correct
9 Correct 157 ms 20204 KB Output is correct
10 Correct 141 ms 20204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 326 ms 29232 KB Output is correct
2 Correct 344 ms 29252 KB Output is correct
3 Correct 426 ms 29124 KB Output is correct
4 Correct 477 ms 28868 KB Output is correct
5 Correct 354 ms 28996 KB Output is correct
6 Correct 366 ms 29252 KB Output is correct
7 Correct 409 ms 29388 KB Output is correct
8 Correct 269 ms 28996 KB Output is correct
9 Correct 271 ms 28984 KB Output is correct
10 Correct 865 ms 29252 KB Output is correct
11 Correct 320 ms 29128 KB Output is correct
12 Correct 448 ms 29252 KB Output is correct