# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
240189 | Nucleist | Palindromic Partitions (CEOI17_palindromic) | C++14 | 396 ms | 131072 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define EPS 0.000001
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
ve pi;
ll mi=1e9+9;
ve compute_hash(string const& s) {
const ll p = 31;
const ll m = 1e9 + 9;
long long hash_value = 0;
long long p_pow = 1;
ve cur;
for (char c : s) {
hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
pi.pb(p_pow);
p_pow = (p_pow * p) % m;
cur.pb(hash_value);
}
return cur;
}
ve compute_hash1(string const& s) {
const ll p = 31;
const ll m = 1e9 + 9;
long long hash_value = 0;
long long p_pow = 1;
ve cur;
for (ll i = s.size()-1; i >= 0; --i)
{
char c=s[i];
hash_value = (hash_value*31)%m;
hash_value = (hash_value + (c - 'a' + 1)) % m;
p_pow = (p_pow * p) % m;
cur.pb(hash_value);
}
return cur;
}
int main()
{
//freopen("tit.txt","r",stdin);
//freopen("out","w",stdout);
flash;
ll nb;
cin>>nb;
ve now,now1;
while(nb--){
string ka;
cin>>ka;
now=compute_hash(ka);
now1=compute_hash1(ka);
reverse(all(now1));
ll n=sz(ka);
ll last=INF;
ll last1=INF;
ll ans=0;
ll kobr=n;
for (int i = 0; i < ka.size()/2; ++i)
{
ll cur=0,cur1=0;
if(last!=INF){
cur=(now1[n-i-1]-((ll)((now1[n-last1-1]*pi[i-last1]))%mi)+mi)%mi;
cur=(ll)((cur*pi[last1+1]))%mi;
cur1=(now[i]-now[last]+mi)%mi;
}
else{
cur=(now1[n-i-1]);
cur1=(now[i]);
}
if(cur==cur1){
if(last!=INF)kobr-=(i-last)*2;
else kobr-=(i+1)*2;
last=i;
last1=i;
ans+=2;
}
}
if(kobr)ans++;
cout<<ans<<'\n';
}
return 0;
}
//code the AC sol !
// BS/queue/map
Compilation message (stderr)
# | 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... |