Submission #894892

# Submission time Handle Problem Language Result Execution time Memory
894892 2023-12-29T07:33:33 Z vjudge1 Palinilap (COI16_palinilap) C++11
54 / 100
57 ms 2396 KB
#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
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 1176 KB Output is correct
2 Correct 52 ms 1112 KB Output is correct
3 Correct 2 ms 1112 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 856 KB Output is correct
6 Correct 1 ms 1112 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 2396 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -