이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define LL long long
string s;
char inp[300005];
int n;
int mo = 1e9 + 7;
int base = 16290047;
int p_pow[300000 * 2 + 10];
int i_pow[300000 * 2 + 10];
int pfx[300000 * 2 + 10];
int expo(int a, int b){
if ( b == 0 ) return 1;
int half = expo(a,b/2);
half = ( 1LL * half * half ) % mo;
if ( b & 1 ) half = (1LL * half * a ) % mo;
return half;
}
int h( int l, int r ){
int ans = pfx[r] - pfx[l];
if ( ans < 0 ) ans += mo;
ans = ( ans * 1LL * i_pow[l+1] ) % mo;
return ans;
}
vector < int > manacher_odd(string s){
vector < int > p(s.size(), 0);
int l = 1, r = 1;
for ( int i = 1; i <= s.size()-2; i++ ){
p[i] = max(0, min(r-i, p[l + (r-i)]) );
while ( s[i-p[i]] == s[i+p[i]] ){
p[i]++;
}
if ( i + p[i] > r ){
l = i - p[i];
r = i + p[i];
}
}
return p;
}
LL solve1(){
s = inp;
s = "$" + s + "^";
vector < int > p = manacher_odd(s);
int N = s.size() - 2;
for ( int i = 0; i < s.size(); i++ ){
pfx[i] = (s[i] * 1LL * p_pow[i]) % mo;
if ( i ) pfx[i] += pfx[i-1];
if ( pfx[i] >= mo ) pfx[i] -= mo;
}
map < int , int > occ;
LL ans = 0;
for ( int i = 1; i <= N; i++ ){
int l = i - p[i], r = i + p[i];
ans = max( ans, (++occ[h(l,r-1)]) * 1LL * (p[i] * 2 - 1) );
}
return ans;
}
LL solve2(){
s = "$";
for ( int i = 0; i < n; i++ ){
s += "#";
s += inp[i];
}
s += "#^";
vector < int > p = manacher_odd(s);
int N = s.size()-2;
for ( int i = 0; i < s.size(); i++ ){
pfx[i] = (s[i] * 1LL * p_pow[i]) % mo;
if ( i ) pfx[i] += pfx[i-1];
if ( pfx[i] >= mo ) pfx[i] -= mo;
}
LL ans = 0;
map < int , int > occ;
for ( int i = 1; i <= N; i++ ){
if ( s[i] == '#' ){
int l = i - p[i], r = i + p[i];
ans = max( ans, (++occ[h(l,r-1)]) * 1LL * (p[i] - 1));
}
}
return ans;
}
int main(){
scanf("%s",&inp);
n = strlen(inp);
int p = 1;
for ( int i = 0; i <= (n + 3) * 2; i++ ){
p_pow[i] = p;
p = (p * 1LL * base) % mo;
}
int inv = expo(base,mo-2);
int ip = 1;
for ( int i = 0; i <= (n+3) * 2 ; i++ ){
i_pow[i] = ip;
ip = (1LL * ip * inv) % mo;
}
LL ans1 = solve1();
LL ans2 = solve2();
if ( ans1 > ans2 ) printf("%lld\n",ans1);
else printf("%lld\n",ans2);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
palindrome.cpp: In function 'std::vector<int> manacher_odd(std::string)':
palindrome.cpp:33:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for ( int i = 1; i <= s.size()-2; i++ ){
| ~~^~~~~~~~~~~~~
palindrome.cpp: In function 'long long int solve1()':
palindrome.cpp:53:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for ( int i = 0; i < s.size(); i++ ){
| ~~^~~~~~~~~~
palindrome.cpp: In function 'long long int solve2()':
palindrome.cpp:79:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for ( int i = 0; i < s.size(); i++ ){
| ~~^~~~~~~~~~
palindrome.cpp: In function 'int main()':
palindrome.cpp:99:13: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[300005]' [-Wformat=]
99 | scanf("%s",&inp);
| ~^ ~~~~
| | |
| | char (*)[300005]
| char*
palindrome.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%s",&inp);
| ~~~~~^~~~~~~~~~~
# | 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... |