Submission #670188

# Submission time Handle Problem Language Result Execution time Memory
670188 2022-12-08T08:50:43 Z MohammadAghil Palindromes (APIO14_palindrome) C++17
73 / 100
444 ms 131072 KB
      #include <bits/stdc++.h>
//   #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
 using namespace std;
  typedef long long ll;
   typedef pair<int, int> pp;
     #define per(i,r,l) for(int i = (r); i >= (l); i--)
       #define rep(i,l,r) for(int i = (l); i < (r); i++)
          #define all(x) begin(x), end(x)
             #define sz(x) (int)(x).size()
                 #define pb push_back
                     #define ss second
                          #define ff first
                                  void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
     cin.tie(0) -> sync_with_stdio(0);
     #ifndef ONLINE_JUDGE
          #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
     #else
          #define er(args ...) 0
     #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 3e5 + 5, p = 9973, lg = 22, inf = ll(1e9) + 5;
// ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll,md);return k*k%md*(b&1ll?a:1)%md;}

struct hhh{
     vector<int> pw, hsh;
     ll p, mod;
     hhh(string s, ll p, ll mod): p(p), mod(mod){
          int n = sz(s);
          hsh.resize(n), pw.resize(n + 1);
          hsh[0] = s[0];
          rep(i,1,n){
               hsh[i] = (p*hsh[i-1] + s[i])%mod;
          }
          pw[0] = 1;
          rep(i,1,n+1){
               pw[i] = p*pw[i-1]%mod;
          }
     }
     int get(int l, int r){
          return (hsh[r] - (l? 1ll*hsh[l-1]*pw[r-l+1]%mod :0) + mod)%mod;
     }
};

ll vl[maxn];
int adj[maxn][27], ptr = 1;
pp hsh[maxn];
map<pp, int> id;
ll ans;
string tmp;

void dfs(int r, int h = 0){
     // er(r, tmp);
     rep(i,0,27) if(adj[r][i]){
          // tmp.pb(char('a' + i));
          dfs(adj[r][i], h+1);
          // tmp.pop_back();
          vl[r] += vl[adj[r][i]];
     }
     ans = max({
          ans,
          1ll*vl[r]*h - vl[r]
     });
}

int main(){ IOS();     
     string s; cin >> s;
     string t = "#";
     for(char c: s){
          t.pb(c);
          t.pb('#');
     }
     s = t;
     int n = sz(s);
     hhh hs(s, p, mod);
     hhh hs2(s, p, mod+2);
     reverse(all(s));
     hhh hrs(s, p, mod);
     hhh hrs2(s, p, mod+2);
     reverse(all(s));
     auto get = [&](int l, int r){
          return hs.get(l, r) == hrs.get(n-1-r, n-1-l) && hs2.get(l, r) == hrs2.get(n-1-r, n-1-l);
     };
     // rep(i,0,n) er(get(i,i));
     int tmp = 0, mx = -1;
     rep(i,0,n){
          int lx = 0, rx = min(i+1, mx-i+2);
          // er(i, rx);
          while(lx + 1 < rx){
               int mid = (lx + rx)>>1;
               if(get(i-mid+1, i+mid-1)) lx = mid;
               else rx = mid;
          }
          // er(i, lx, rx, get(i, i));
          int cr = lx == 0? 0: id[{hs.get(i, i + lx-1), hs2.get(i, i+ lx-1)}];
          if(lx == mx-i+1){
               rep(l,rx,min(i+1,n-i)+1){
                    if(s[i-l+1] == s[i+l-1]){
                         tmp++;
                         int c = s[i+l-1] == '#' ? 26: s[i+l-1]-'a';
                         if(!adj[cr][c]){
                              hsh[ptr] = {(p*hsh[cr].ff + s[i+l-1])%mod, (p*hsh[cr].ss + s[i+l-1])%(mod+2)};
                              id[hsh[ptr]] = ptr;
                              adj[cr][c] = ptr++;
                         }
                         cr = adj[cr][c];
                         mx = i+l-1;
                    } else break;
               }
               // er(i, mx);
          }

          vl[cr]++;
          // rep(l,rx,min(i+1,n-i)+1){
          //      if(s[i-l+1] == s[i+l-1]){
          //           tmp++;
          //           int c = s[i+l-1]-'a';
          //           if(!adj[cr][c]){
          //                hsh[ptr] = (p*hsh[cr] + s[i+l-1])%mod;
          //                id[hsh[ptr]] = ptr;
          //                adj[cr][c] = ptr++;
          //           }
          //           cr = adj[cr][c];
          //      } else break;
          // }
          // // er(i, cr);
          // vl[cr][0]++;
     }
     // assert(tmp < 2*n);
     // rep(i,1,n){
     //      if(s[i] - s[i-1]) continue;
     //      mx = max(mx, i-1);
     //      int lx = 0, rx = min(i+1, mx-i+2);
     //      while(lx + 1 < rx){
     //           int mid = (lx + rx)>>1;
     //           if(get(i-mid, i+mid-1)) lx = mid;
     //           else rx = mid;
     //      }
     //      int cr = lx == 0? 0: id[hs.get(i, i + lx-1)];
     //      if(lx == mx - i + 1){
     //           rep(l,rx,min(i,n-i)+1){
     //                if(s[i+l-1] == s[i-l]){
     //                     int c = s[i+l-1]-'a';
     //                     if(!adj[cr][c]){
     //                          hsh[ptr] = (p*hsh[cr] + s[i+l-1])%mod;
     //                          id[hsh[ptr]] = ptr;
     //                          adj[cr][c] = ptr++;
     //                     }
     //                     cr = adj[cr][c];
     //                     mx = i + l - 1;
     //                } else break;
     //           }
     //      }
     //      er(i, mx);
     //      vl[cr][1]++;
     // }
     dfs(0);
     cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 3 ms 724 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 5264 KB Output is correct
2 Correct 21 ms 5076 KB Output is correct
3 Correct 27 ms 5332 KB Output is correct
4 Correct 25 ms 5204 KB Output is correct
5 Correct 17 ms 5204 KB Output is correct
6 Correct 19 ms 5056 KB Output is correct
7 Correct 21 ms 4820 KB Output is correct
8 Correct 5 ms 1144 KB Output is correct
9 Correct 5 ms 1112 KB Output is correct
10 Correct 16 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 343 ms 50028 KB Output is correct
2 Correct 335 ms 47260 KB Output is correct
3 Correct 387 ms 49984 KB Output is correct
4 Correct 381 ms 47728 KB Output is correct
5 Correct 235 ms 48048 KB Output is correct
6 Correct 205 ms 35824 KB Output is correct
7 Correct 258 ms 39164 KB Output is correct
8 Correct 29 ms 7428 KB Output is correct
9 Correct 87 ms 16992 KB Output is correct
10 Correct 216 ms 42152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 444 ms 131072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -