이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<map>
using namespace std;
typedef long long int lld;
#define MOD 1000000007
#define BASE 257
lld hash2[1000000];
lld hash3[1000000];
lld pow[1000000];
int n;
bool palindrome(int a, int b){
lld A=hash3[b+1]-hash3[a];
A*=pow[n-1-a];
lld B=hash2[a]-hash2[b+1];
B*=pow[b];
A%=MOD;
B%=MOD;
if(B<0)B+=MOD;
if(A<0)A+=MOD;
//cout<<a<<" "<<b<<" "<<A<<" "<<B<<endl;
if(A%MOD==B%MOD)return true;
return false;
}
lld val(int a, int b){
lld A=hash3[b+1]-hash3[a];
A*=pow[n-1-a];
A%=MOD;
if(A<0)A+=MOD;
return A;
}
int main(){
string s;
cin>>s;
n=s.size();
hash3[0]=0;
lld v=1;
for(int i=1;i<=n;i++){
hash3[i]=(s.at(i-1))*v+hash3[i-1];
hash3[i]%=MOD;
pow[i-1]=v;
v*=BASE;
v%=MOD;
}
hash2[n]=0;
v=1;
for(int i=n-1;i>-1;i--){
hash2[i]=(s.at(i))*v+hash2[i+1];
hash2[i]%=MOD;
v*=BASE;
v%=MOD;
}
//cout<<hash2[0]<<" "<<hash[n]<<endl;
lld ans=0;
for(lld len=1;len<=n;len++){
map<lld,int> m;
//cout<<hash[i]<<" "<<hash2[i]<<endl;
for(int i=0;i+len-1<n;i++){
if(palindrome(i,i+len-1)){
lld H=val(i,i+len-1);
map<lld,int>::iterator it=m.find(H);
if(it==m.end()){
m[H]=1;
ans=max(ans,len);
//cout<<len<<endl;
}else{
it->second++;
ans=max(ans,it->second*len);
//cout<<it->second*len<<endl;
}
}
}
//cout<<(hash[i]+hash2[i])%MOD<<endl;
}cout<<ans<<endl;
//cout<<palindrome(0,2)<<endl;
}
# | 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... |