Submission #1369837

#TimeUsernameProblemLanguageResultExecution timeMemory
1369837trandkhoaPalindromic Partitions (CEOI17_palindromic)C++20
100 / 100
82 ms16004 KiB
/**
 *     Author: Tran Dang Khoa
**/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define FOR(i,l,r) for (int i = (l), _r = (r); i <= _r; i++)
#define REP(i,l,r) for (int i = (l), _r = (r); i < _r; i++)
#define FORN(i,r,l) for (int i = (r), _l = (l); i >= _l; i--)
#define MASK(x) (1LL << (x))
#define BIT(x,i) (((x) >> (i)) & 1)
#define sz(x) (int)x.size()
#define all(v) (v).begin(),(v).end()
#define allVector(v, n) (v).begin() + 1, (v).begin() + (n) + 1
#define segleft (id << 1)
#define segright (id << 1 | 1)
#define tcT template <class T
tcT> bool minimize(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
tcT> bool maximize(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
const int MOD = 1e9 + 2277;
 
void iofile(string s) {
	freopen((s + ".inp").c_str(), "r", stdin);
	freopen((s + ".out").c_str(), "w", stdout);
}
 
const int BASE = 311;
const int N = 1e6 + 1;
int powBase[N];
 
void calcpw() {
	powBase[0] = 1;
	FOR(i, 1, N - 1) powBase[i] = ((ll) powBase[i - 1] * BASE) % MOD;
}
 
struct Hashing {
	vector<ll> sum;
	string s;
	int n;
	
	Hashing(int _n, string v) : s(v) {
		n = _n;
		sum.assign(n + 1, 0);
		calc();
	}
	
	void calc() {
		FOR(i, 1, n) {
			sum[i] = ((ll) sum[i - 1] * BASE + s[i]) % MOD;
		}
	}
	
	int get(int l, int r) {
		return ((ll) sum[r] - ((ll) sum[l - 1] * powBase[r - l + 1]) % MOD + MOD) % MOD;
	}
};
 
void trandkhoa() {
	string s; cin >> s;
	int n = sz(s);
	s = " " + s;
	
	Hashing str(n, s);
	
	int ans = 0;
	int l1 = 1, l2 = 1, r1 = n, r2 = n;
	
	while (l2 < r1) {
		if (str.get(l1, l2) == str.get(r1, r2)) {
			ans += 2;
			l2++, r1--;
			l1 = l2;
			r2 = r1;
		} else {
			l2++, r1--;
		}
	}
	
	if (l1 <= r2) ans++;
	
	cout << ans << endl;
}
 
signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	//iofile("");
	
	calcpw();
	int test = 1;
	cin >> test;
	while(test--) trandkhoa();
	
	return (0 ^ 0);
}

Compilation message (stderr)

palindromic.cpp: In function 'void iofile(std::string)':
palindromic.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         freopen((s + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
palindromic.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         freopen((s + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...