Submission #1019030

# Submission time Handle Problem Language Result Execution time Memory
1019030 2024-07-10T12:09:40 Z stefanopulos Palinilap (COI16_palinilap) C++17
54 / 100
1000 ms 1372 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ldb;
 
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;

#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
 
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005; 

int n;
string s;


int d1[mxN];
int d2[mxN];
void manacher(string a){
    int N = sz(a);
    int L = 0, R = -1;
    ff(i,0,N - 1){
        int j = (i > R ? 1 : min(d1[L + R - i], R - i + 1));
        while(i >= j && i + j < N && a[i - j] == a[i + j])j += 1;
        d1[i] = j--;
        if(i + j > R){
            L = i - j;
            R = i + j;
        }
    }
    
    L = 0, R = -1;
    ff(i,0,N - 1){
        int j = (i > R ? 0 : min(d2[L + R - i + 1], R - i + 1));
        while(i > j && i + j < N && a[i - j - 1] == a[i + j])j += 1;
        d2[i] = j--;
        if(i + j > R){
            L = i - j - 1;
            R = i + j;
        }
    }
    
}

int main(){
    cin.tie(0)->sync_with_stdio(0);

    cin >> s; n = sz(s);

    manacher(s);

    int uk = 0;
    ff(i,0,n - 1)uk += d1[i] + d2[i];

    int rez = uk;
    ff(i,0,n - 1){

        char old = s[i];
        for(char a = 'a'; a <= 'z'; a++){
            if(old == a)continue;

            s[i] = a;
            
            // Neparno
            int cur = uk;
            ff(j,0,i - 1){
                if(j + d1[j] - 1 >= i){
                    cur -= d1[j];
                    cur += i - j;
                }

                if(j + d1[j] - 1 == i - 1){

                    int l = 2 * j - i, r = i;
                    while(0 <= l && l < n && 0 <= r && r < n && l <= r && s[l] == s[r]){
                        l -= 1; r += 1;
                        cur += 1;
                    }

                }
            }

            ff(j,i + 1,n - 1){
                if(j - d1[j] + 1 <= i){
                    cur -= d1[j];
                    cur += j - i;
                }

                if(j - d1[j] + 1 == i + 1){
                    int l = i, r = 2 * j - i;
                    while(0 <= l && l < n && 0 <= r && r < n && l <= r && s[l] == s[r]){
                        l -= 1; r += 1;
                        cur += 1;
                    }
                }

            }

            // Parno
            ff(j,0,i){
                if(j + d2[j] - 1 >= i){
                    cur -= d2[j];
                    cur += i - j;
                }

                if(j + d2[j] - 1 == i - 1){

                    int l = 2 * j - i - 1, r = i;
                    while(0 <= l && l < n && 0 <= r && r < n && l <= r && s[l] == s[r]){
                        l -= 1; r += 1;
                        cur += 1;
                    }

                }
            }

            ff(j,i + 1,n - 1){
                if(j - d2[j] <= i){
                    cur -= d2[j];
                    cur += j - i - 1;
                }

                if(j - d2[j] == i + 1){
                    int l = i, r = 2 * j - i - 1;
                    while(0 <= l && l < n && 0 <= r && r < n && l <= r && s[l] == s[r]){
                        l -= 1; r += 1;
                        cur += 1;
                    }
                }

            }

            rez = max(rez, cur);

        }   

        s[i] = old;
    }

    cout << rez << '\n';

    return 0;
}
/*



// probati bojenje sahovski
*/
 
 
 
 
 
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 908 ms 348 KB Output is correct
2 Correct 891 ms 348 KB Output is correct
3 Correct 913 ms 348 KB Output is correct
4 Correct 350 ms 476 KB Output is correct
5 Correct 667 ms 496 KB Output is correct
6 Correct 948 ms 348 KB Output is correct
7 Correct 877 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1056 ms 1372 KB Time limit exceeded
2 Halted 0 ms 0 KB -