이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned 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 =1e6+5;
const ll mod=988642507;
const ll base=317;
ull f[maxn];
ull fv[maxn];
ull mu[maxn];
ll n;
ll cntnw=0;
map<ull,ll> mp;
ll cnt[maxn];
ll len[maxn];
ll nxt[maxn];
ll in[maxn];
bool add(ull val,ll len1)
{
if (!mp.count(val))
{
cntnw++;
len[cntnw]=len1;
mp[val]=cntnw;
return true;
}
return false;
}
ull get(ll l,ll r)
{
return f[r]-f[l-1]*mu[r-l+1];
}
ull getv(ll l,ll r)
{
swap(l,r);
l=n-l+1;
r=n-r+1;
return fv[r]-fv[l-1]*mu[r-l+1];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("t.inp","r"))
{
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
mu[0]=1;
for (int i=1; i<maxn; i++)
mu[i]=(mu[i-1]*base);
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+s[i]-'a'+1;
}
reverse(t.begin(),t.end());
t=" "+t;
for (int i=1; i<=n; i++)
{
fv[i]=fv[i-1]*base+t[i]-'a'+1;
}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
palindrome.cpp:6: warning: "ull" redefined
6 | #define ull long long
|
palindrome.cpp:5: note: this is the location of the previous definition
5 | #define ull unsigned long long
|
palindrome.cpp: In function 'int main()':
palindrome.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("test.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |