Submission #1284672

#TimeUsernameProblemLanguageResultExecution timeMemory
1284672ereonzisPalindromic Partitions (CEOI17_palindromic)C++20
0 / 100
0 ms332 KiB
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
namespace debug {
template <class c> struct rge { c b, e; };
template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; }
template <class c> char spk(...);
template <class c> auto spk(c *a) -> decltype(cerr << *a, 0);
struct stream {
  ~stream() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof spk<c>(0) != 1, stream &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof spk<c>(0) == 1, stream &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class a, class b> stream &operator<<(pair<a, b> p) {
    return *this << "(" << p.first << ", " << p.second << ")";
  }
  template <class c> stream &operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; it++)
      *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
  stream &_dbg(const string &s, int i, int b) { return *this; }
  template <class c, class... cs>
  stream &_dbg(const string &s, int i, int b, c arg, cs... args) {
    if (i == (int)(s.size()))
      return (*this << ": " << arg);
    b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') -
         (s[i] == ']') - (s[i] == '}');
    return (s[i] == ',' && b == 0)
               ? (*this << ": " << arg << "     ")._dbg(s, i + 1, b, args...)
               : (s[i] == ' ' ? *this : *this << s[i])
                     ._dbg(s, i + 1, b, arg, args...);
  }
};
} // namespace debug
#ifdef DEBUG
#define dout debug::stream()
#define dbg(...) ((dout << "line:" << __LINE__ << " >> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__))
#else
#define dout
#define dbg(...)
#endif
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;

const int MOD = 1e9 + 7;
const int K = 2;

int base[K];

void init_hash() {
    random_device rd {};
    mt19937 engine(rd());
    rep(i, 0, K-1) {
        base[i] = uniform_int_distribution<int>(1, MOD-1)(engine);
    }
}

struct Hash {
    int vals[K], pows[K];
    Hash() {
        reset();
    }

    void reset() {
        FOR(i, 0, K) vals[i] = 0;
        FOR(i, 0, K) pows[i] = 1;
    }

    void push_back(char c) {
        FOR(i, 0, K) vals[i] = (vals[i] + 1ll * pows[i] * c) % MOD;
        FOR(i, 0, K) pows[i] = 1ll * pows[i] * base[i] % MOD;
    }

    void push_front(char c) {
        FOR(i, 0, K) vals[i] = (1ll * vals[i] * base[i] + c) % MOD;
        FOR(i, 0, K) pows[i] = 1ll * pows[i] * base[i] % MOD;
    } 

    bool operator==(const Hash& other) const {
        FOR(i, 0, K) {
            if (vals[i] != other.vals[i]) return false;
        }
        return true;
    }

    string to_string() const {
        string res = "(";
        FOR(i, 0, K) {
            res.append(std::to_string(vals[i]));
            if (i != K-1) res.append(", ");
        }
        res.push_back(')');
        return res;
    }
};

void solve() {
    string s;
    cin >> s;
    Hash f, b;
    int n = s.size();
    int ans = 0;
    for (int i = 0, j = n-1; i < n; i++, j--) {
        f.push_back(s[i]);
        b.push_front(s[j]);
        if (f == b) {
            ans += 2;
            if (j <= i) {
                ans--;
                break;
            }
            f.reset();
            b.reset();
        }
    }
    cout << ans << '\n';
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    init_hash();
    int t;
    cin >> t;
    while (t--) {
        solve();
    }


    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...