Submission #299282

# Submission time Handle Problem Language Result Execution time Memory
299282 2020-09-14T16:05:07 Z AaronNaidu Palindromes (APIO14_palindrome) C++14
8 / 100
1000 ms 3228 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll mod1 = 1000000007;
ll mod2 = 1000000009;
string s;
ll n;
ll prefSums[20000], sufSums[20000];
ll prefSums2[20000], sufSums2[20000];
ll pows[20000], pows2[20000];
unordered_map<ll, ll> m;

ll divide2(ll num, ll den) {
    if (num < 0)
    {
        num += mod2;
    }
    if (den < 0)
    {
        den += mod2;
    }
    ll inv = 1;
    ll exp = den;
    exp %= mod2;
    for (int i = 0; i < 32; i++)
    {
        if ((mod2-2) & (1<<i))
        {
            inv *= exp;
            inv %= mod2;
        }
        exp *= exp;
        exp %= mod2;
    }
    ll ans = num * inv;
    ans %= mod2;
    return ans;
}

ll divide1(ll num, ll den) {
    while (num < 0)
    {
        num += mod1;
    }
    while (den < 0)
    {
        den += mod1;
    }
    ll inv = 1;
    ll exp = den;
    exp %= mod1;
    for (int i = 0; i < 32; i++)
    {
        if ((mod1-2) & (1<<i))
        {
            inv *= exp;
            inv %= mod1;
        }
        exp *= exp;
        exp %= mod1;
    }
    ll ans = num * inv;
    ans %= mod1;
    return ans;
}


int main() {
    //cout << divide1(15, 3) << " " << divide2(15, 3) << "\n";
    cin >> s;
    ll thisPow = 1;
    ll thisPow2 = 1;
    n = s.length();
    for (int i = 0; i < n; i++)
    {
        pows[i]= thisPow;
        thisPow *= 11;
        thisPow %= mod1;
        pows2[i]= thisPow2;
        thisPow2 *= 13;
        thisPow2 %= mod2;
    }
    prefSums[0] = s[0] - 'a' + 1;
    prefSums2[0] = s[0] - 'a' + 1;
    for (int i = 1; i < n; i++)
    {
        prefSums[i] = prefSums[i-1] + (s[i] - 'a' + 1) * pows[i];  
        prefSums[i] %= mod1;  
        prefSums2[i] = prefSums2[i-1] + (s[i] - 'a' + 1) * pows2[i];  
        prefSums2[i] %= mod2; 
    }
    sufSums[n-1] = s[n-1] - 'a' + 1;
    sufSums2[n-1] = s[n-1] - 'a' + 1;
    for (int i = n-2; i >= 0; i--)
    {
        sufSums[i] = sufSums[i+1] + (s[i] - 'a' + 1) * pows[n-1-i];
        sufSums[i] %= mod1;
        sufSums2[i] = sufSums2[i+1] + (s[i] - 'a' + 1) * pows2[n-1-i];
        sufSums2[i] %= mod2;
    }
    /*for (int j = 0; j < n; j++)
    {
        cout << prefSums2[j] << " ";
        //cout << prefSums[j] << " " << sufSums[j] << "\n";
    }
    cout << "\n";
    //cout << "\n\n\n";
    //cout << prefSums2[8] << " " << pre
    for (int i = 1; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
            cout << divide2(prefSums2[j] - prefSums2[i-1], pows2[i]) << " ";
        }
        cout << "\n";
    }
    cout << "\n";
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n-1; j++)
        {
            cout << divide2(sufSums2[i] - sufSums2[j+1], pows2[n-1-j]) << " ";
        }
        cout << sufSums2[i];
        cout << "\n";
    }*/
    ll totMax = 0;
    for (int j = 0; j < n-1; j++)
    {
        if (prefSums[j] == divide1(sufSums[0]-sufSums[j+1], pows[n-1-j]) and prefSums2[j] == divide2(sufSums2[0]-sufSums2[j+1], pows2[n-1-j]))
        {
            //cout << "Entered\n";
            ll curr = 0;
            if (m.find(prefSums[j]) != m.end())
            {
                curr = m.find(prefSums[j])->second;
                m.erase(prefSums[j]);
            }
            curr += j+1;
            if (curr > totMax)
            {
               //cout << "New max of " << curr << " from " << 0 << " to " << j << "\n";
            }
            
            totMax = max(totMax, curr);
            m.insert({prefSums[j], curr});
        }
    }
    if (prefSums[n-1] == sufSums[0] and prefSums2[n-1] == sufSums2[0])
    {
        ll curr = 0;
        if (m.find(prefSums[n-1]) != m.end())
        {
            curr = m.find(prefSums[n-1])->second;
            m.erase(prefSums[n-1]);
        }
        curr += n;
        if (curr > totMax)
        {
          // cout << "New max of " << curr << " from " << 0 << " to " << n-1 << "\n";
        }
        totMax = max(totMax, curr);
        m.insert({prefSums[n-1], curr});
    }
    for (int i = 1; i < n; i++)
    {
        for (int j = i; j < n-1; j++)
        {
            if (divide1(prefSums[j]-prefSums[i-1], pows[i]) == divide1(sufSums[i]-sufSums[j+1], pows[n-1-j]) and divide2(prefSums2[j]-prefSums2[i-1], pows2[i]) == divide2(sufSums2[i]-sufSums2[j+1], pows2[n-1-j]))
            {            
                ll curr = 0;
                if (m.find(divide1(prefSums[j]-prefSums[i-1], pows[i])) != m.end())
                {
                    curr = m.find(divide1(prefSums[j]-prefSums[i-1], pows[i]))->second;
                    m.erase(divide1(prefSums[j]-prefSums[i-1], pows[i]));
                }
                curr += j-i+1;
                if (curr > totMax)
                {
                   //cout << "New max of " << curr << " from " << i << " to " << j << "\n";
                   //cout << "Hash value is " << (prefSums[j]-prefSums[i-1])/pows[i] << "\n";
                }
                totMax = max(totMax, curr);
                m.insert({divide1(prefSums[j]-prefSums[i-1], pows[i]), curr});
            }
        }
        if (divide1(prefSums[n-1]-prefSums[i-1], pows[i]) == sufSums[i] and divide2(prefSums2[n-1]-prefSums2[i-1], pows2[i]) == sufSums2[i])
        {
            ll curr = 0;
            if (m.find(sufSums[i]) != m.end())
            {
                curr = m.find(sufSums[i])->second;
                m.erase(sufSums[i]);
            }
            curr += n-i;
            if (curr > totMax)
            {
               //cout << "New max of " << curr << " from " << i << " to " << n-1 << "\n";
            }
            totMax = max(totMax, curr);
            m.insert({sufSums[i], curr});
        }
    }
    cout << totMax << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 9 ms 384 KB Output is correct
20 Correct 9 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 14 ms 384 KB Output is correct
24 Correct 7 ms 384 KB Output is correct
25 Correct 13 ms 384 KB Output is correct
26 Correct 6 ms 384 KB Output is correct
27 Correct 6 ms 376 KB Output is correct
28 Correct 8 ms 384 KB Output is correct
29 Correct 7 ms 384 KB Output is correct
30 Correct 6 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 6 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 904 ms 504 KB Output is correct
2 Correct 685 ms 536 KB Output is correct
3 Execution timed out 1061 ms 384 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1050 ms 1280 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 9 ms 2816 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 3228 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -