이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 5000 + 10, M = 26 + 10;
const ll mod = 1e9 + 7;
ll um(ll a, ll b){
return ((1LL * a * b) % mod + mod) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
ll binpow(ll x, ll step){
ll res = 1LL;
while(step){
if(step & 1) res = um(res, x);
x = um(x, x);
step /= 2;
}
return res;
}
int arr[N][M], x[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
int n = (int)s.size(), cur = 0, ans = 0;
for(int i = 0; i < n; i++){
x[i] = s[i] - 'a';
}
for(int i = 0; i < n; i++){
int j = 0;
while(true){
if(i - j < 0 || i + j >= n) break;
if(x[i - j] != x[i + j]) break;
j++;
}
for(int k = j - 1; k > 0; k--){
arr[i - k][x[i - k]] += (j - k);
arr[i + k][x[i + k]] += (j - k);
}
arr[i][x[i]] += j;
j = 0;
while(true){
if(i - j < 0 || i + j + 1 >= n) break;
if(x[i - j] != x[i + j + 1]) break;
j++;
}
for(int k = j - 1; k >= 0; k--){
arr[i - k][x[i - k]] += (j - k);
arr[i + k + 1][x[i + k + 1]] += (j - k);
}
}
for(int i = 0; i < n; i++){
ans += arr[i][x[i]];
cur += arr[i][x[i]];
//cout << arr[i][x[i]] << " ";
}
//cout << endl;
for(int i = 0; i < n; i++){
int j = 0, is;
bool was = false;
while(true){
if(i - j < 0 || i + j >= (int)s.size()) break;
if(x[i - j] != x[i + j]){
if(was) break;
was = true;
is = j;
}
j++;
}
if(was == true){
arr[i - is][x[i + is]] += (j - is);
arr[i + is][x[i - is]] += (j - is);
}
was = false;
j = 0;
while(true){
if(i - j < 0 || i + j + 1 >= (int)s.size()) break;
if(x[i - j] != x[i + j + 1]){
if(was) break;
was = true;
is = j;
}
j++;
}
if(was == true){
arr[i - is][x[i + is + 1]] += (j - is);
arr[i + is + 1][x[i - is]] += (j - is);
}
}
ll f1 = -1, f2 = -1;
for(int i = 0; i < n; i++){
for(int j = 0; j < 26; j++){
if(j == x[i]) continue;
if(cur + arr[i][j] - arr[i][x[i]] + 1 > ans){
ans = max(ans, cur + arr[i][j] - arr[i][x[i]] + 1);
f1 = i;
f2 = j;
}
}
}
int real = 0;
if(f1 != -1) x[f1] = f2;
for(int i = 0; i < n; i++){
int j = 0;
while(true){
if(i - j < 0 || i + j >= n) break;
if(x[i - j] != x[i + j]) break;
j++;
}
real += j;
j = 0;
while(true){
if(i - j < 0 || i + j + 1 >= n) break;
if(x[i - j] != x[i + j + 1]) break;
j++;
}
real += j;
}
cout << real;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |