답안 #418985

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
418985 2021-06-06T09:41:24 Z Dymo 회문 (APIO14_palindrome) C++14
100 / 100
964 ms 43620 KB
#include<bits/stdc++.h>
using namespace std;

#define ll   long long
#define ull  long long
#define ull  long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
//#define endl "\n"
const ll maxn =3e5+5;
const ll mod=988642507;
const ll mod1=999242501;
const ll base=317;
const ll base1=293;
ull f[maxn];
ull f1[maxn];
ull fv[maxn];
ull fv1[maxn];

ull mu[maxn];
ull mu1[maxn];

ll n;
ll cntnw=0;
map<pair<ull,ull>,ll> mp;
ll cnt[maxn];
ll len[maxn];
ll nxt[maxn];
ll in[maxn];
bool add(pair<ull,ull> val,ll len1)
{
    if (!mp.count(val))
    {
        cntnw++;
        len[cntnw]=len1;
        mp[val]=cntnw;
        return true;
    }
    return false;

}
pair<ull,ull> get(ll l,ll r)
{
    return make_pair((((f[r]-f[l-1]*mu[r-l+1])%mod+mod)%mod),(((f1[r]-f1[l-1]*mu1[r-l+1])%mod1+mod1)%mod1));
}
pair<ull,ull> getv(ll l,ll r)
{
    swap(l,r);
    l=n-l+1;
    r=n-r+1;
    return make_pair((((fv[r]-fv[l-1]*mu[r-l+1])%mod+mod)%mod),(((fv1[r]-fv1[l-1]*mu1[r-l+1])%mod1+mod1)%mod1));
}
bool chk(ll l,ll r)
{
    ll len=(r-l+1)/2;
    if(get(r-len+1,r)==getv(l,l+len-1)) return true;
    return false;
}
ll val[maxn];
ll val1[maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    if (fopen("test.inp","r"))
    {
        freopen("test.inp","r",stdin);
        freopen("test.out","w",stdout);
    }
    mt19937_64 rd(time(NULL));
    mu[0]=1;
    mu1[0]=1;
    for (int i=1; i<maxn; i++)
        mu[i]=(mu[i-1]*base)%mod, mu1[i]=(mu1[i-1]*base1)%mod1;
         for (int i=1; i<=26; i++)
    {
        val[i]=abs((ll)rd())%mod;
        val1[i]=abs((ll)rd())%mod1;
    }
    string s;
    cin>> s;
    string t=s;
    n=s.length();
    s=" "+ s;
    for (int i=1; i<=n; i++)
    {
        f[i]=(f[i-1]*base+val[s[i]-'a'+1])%mod;
        f1[i]=(f1[i-1]*base1+val1[s[i]-'a'+1])%mod1;
    }
    reverse(t.begin(),t.end());
    t=" "+t;
    for (int i=1; i<=n; i++)
    {
        fv[i]=(fv[i-1]*base+val[t[i]-'a'+1])%mod;
        fv1[i]=(fv1[i-1]*base1+val1[t[i]-'a'+1])%mod1;
    }

    ll ans=0;
    for (ll i=1; i<=n; i++)
    {
        ll lenmx=min(i,n-i+1);
        ll l=1,h=lenmx;
        while (l<=h)
        {
            ll mid=(l+h)/2;
            if (get(i,i+mid-1)==getv(i-mid+1,i)) l=mid+1;
            else h=mid-1;
        }

      //  if (i==2)cout <<get(2,3)<<" "<<getv(1,2)<<" "<<h<<endl;
     // cout <<i<<" "<<h<<endl;
        ll pre=-1;
        for (int j=h;j>=1;j--)
        {
         //   cout <<j<<endl;
            auto p=add(get(i,i+j-1),j*2-1);
            if (j==h) cnt[mp[get(i,i+j-1)]]++;
            if (pre!=-1) nxt[pre]=mp[get(i,i+j-1)];
            ll t=pre;
            pre=mp[get(i,i+j-1)];
           if (t!=-1) in[pre]++;
            //cout <<mp[get(i,i+j-1)]<<" "<<i<<" "<<i+j-1<<endl;
            if (!p) break;
        }
    }
    queue<ll> q;
   for (int i=1;i<=cntnw;i++)
   {
        if (!in[i]) q.push(i);
   }
 //  cout <<q.size()<<"db"<<endl;
   while (!q.empty())
   {
       auto p=q.front();
       q.pop();
       if (nxt[p]!=0)
       {
           cnt[nxt[p]]+=cnt[p];
           in[nxt[p]]--;
           if (!in[nxt[p]]) q.push(nxt[p]);
       }
       ans=max(ans,cnt[p]*len[p]);
   }
   for (int i=1;i<=cntnw;i++) in[i]=0, len[i]=0, nxt[i]=0, cnt[i]=0;
   cntnw=0;
   mp.clear();
   for (ll i=2; i<=n; i++)
    {
        ll lenmx=min(i-1,n-i+1);
        ll l=1,h=lenmx;
        while (l<=h)
        {
            ll mid=(l+h)/2;
            if (get(i,i+mid-1)==getv(i-mid,i-1)) l=mid+1;
            else h=mid-1;
        }
        ll pre=-1;
        for (int j=h;j>=1;j--)
        {
            auto p=add(get(i,i+j-1),j*2);
            if (j==h) cnt[mp[get(i,i+j-1)]]++;
            if (pre!=-1) nxt[pre]=mp[get(i,i+j-1)];
            ll t=pre;
            pre=mp[get(i,i+j-1)];
            if (t!=-1) in[pre]++;
            if (!p) break;
        }
    }
   for (int i=1;i<=cntnw;i++)
   {
        if (!in[i]) q.push(i);
   }
   while (!q.empty())
   {
       auto p=q.front();
       q.pop();
       if (nxt[p]!=0)
       {
           cnt[nxt[p]]+=cnt[p];
           in[nxt[p]]--;
           if (!in[nxt[p]]) q.push(nxt[p]);
       }
       ans=max(ans,cnt[p]*len[p]);
   }
   cout <<ans;


}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen("test.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("test.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5068 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
3 Correct 5 ms 5068 KB Output is correct
4 Correct 5 ms 5068 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 5 ms 5068 KB Output is correct
8 Correct 4 ms 5024 KB Output is correct
9 Correct 5 ms 5068 KB Output is correct
10 Correct 4 ms 5008 KB Output is correct
11 Correct 4 ms 5068 KB Output is correct
12 Correct 5 ms 5068 KB Output is correct
13 Correct 4 ms 5068 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 5 ms 5068 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 5 ms 5008 KB Output is correct
19 Correct 5 ms 5068 KB Output is correct
20 Correct 5 ms 5068 KB Output is correct
21 Correct 5 ms 5068 KB Output is correct
22 Correct 5 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 5 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 5 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 5 ms 5068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5196 KB Output is correct
2 Correct 5 ms 5196 KB Output is correct
3 Correct 6 ms 5068 KB Output is correct
4 Correct 5 ms 5204 KB Output is correct
5 Correct 6 ms 5068 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 6 ms 5212 KB Output is correct
8 Correct 6 ms 5196 KB Output is correct
9 Correct 5 ms 5068 KB Output is correct
10 Correct 5 ms 5068 KB Output is correct
11 Correct 5 ms 5068 KB Output is correct
12 Correct 5 ms 5068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 6348 KB Output is correct
2 Correct 19 ms 6304 KB Output is correct
3 Correct 21 ms 5844 KB Output is correct
4 Correct 23 ms 5892 KB Output is correct
5 Correct 16 ms 6360 KB Output is correct
6 Correct 17 ms 6304 KB Output is correct
7 Correct 22 ms 6348 KB Output is correct
8 Correct 11 ms 5452 KB Output is correct
9 Correct 11 ms 5452 KB Output is correct
10 Correct 14 ms 5980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 225 ms 17812 KB Output is correct
2 Correct 225 ms 17680 KB Output is correct
3 Correct 267 ms 13076 KB Output is correct
4 Correct 258 ms 13092 KB Output is correct
5 Correct 177 ms 17768 KB Output is correct
6 Correct 158 ms 15148 KB Output is correct
7 Correct 197 ms 14404 KB Output is correct
8 Correct 77 ms 8576 KB Output is correct
9 Correct 108 ms 9532 KB Output is correct
10 Correct 146 ms 16228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 891 ms 43080 KB Output is correct
2 Correct 863 ms 43164 KB Output is correct
3 Correct 958 ms 29056 KB Output is correct
4 Correct 964 ms 29052 KB Output is correct
5 Correct 664 ms 43132 KB Output is correct
6 Correct 774 ms 36136 KB Output is correct
7 Correct 762 ms 32720 KB Output is correct
8 Correct 257 ms 15260 KB Output is correct
9 Correct 249 ms 15180 KB Output is correct
10 Correct 506 ms 38008 KB Output is correct
11 Correct 782 ms 43620 KB Output is correct
12 Correct 286 ms 16740 KB Output is correct