답안 #670187

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670187 2022-12-08T08:49:29 Z MohammadAghil 회문 (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;
     }
};

int vl[maxn], adj[maxn][27], ptr = 1;
pp hsh[maxn];
map<pp, int> id;
map<int, bool> is;
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';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 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 0 ms 340 KB Output is correct
10 Correct 0 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 0 ms 340 KB Output is correct
14 Correct 0 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 0 ms 340 KB Output is correct
20 Correct 0 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 0 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 0 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 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 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 1 ms 596 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
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 5224 KB Output is correct
2 Correct 20 ms 4984 KB Output is correct
3 Correct 26 ms 5140 KB Output is correct
4 Correct 27 ms 5140 KB Output is correct
5 Correct 16 ms 5112 KB Output is correct
6 Correct 17 ms 5012 KB Output is correct
7 Correct 19 ms 4744 KB Output is correct
8 Correct 3 ms 1108 KB Output is correct
9 Correct 3 ms 1108 KB Output is correct
10 Correct 14 ms 3700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 334 ms 49276 KB Output is correct
2 Correct 325 ms 46460 KB Output is correct
3 Correct 397 ms 49276 KB Output is correct
4 Correct 381 ms 47008 KB Output is correct
5 Correct 233 ms 47336 KB Output is correct
6 Correct 203 ms 35288 KB Output is correct
7 Correct 256 ms 38624 KB Output is correct
8 Correct 29 ms 7444 KB Output is correct
9 Correct 90 ms 16828 KB Output is correct
10 Correct 210 ms 41588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 444 ms 131072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -