Submission #62711

# Submission time Handle Problem Language Result Execution time Memory
62711 2018-07-29T21:26:41 Z eriksuenderhauf Palindromic Partitions (CEOI17_palindromic) C++11
100 / 100
364 ms 33012 KB
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <fstream>
#define ni(n) scanf("%d", &n)
#define nl(n) scanf("%I64d", &n)
#define nai(a,n) for (int i = 0; i < (n); i++) ni((a)[i])
#define nal(a,n) for (int i = 0; i < (n); i++) nl((a)[i])
#define case(t) printf("Case #%d: ", (t))
#define pii pair<int, int>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define f first
#define s second
typedef long long ll;
const double pi = acos(-1);
const ll MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
const int pr1 = 137;
const int pr2 = 2017;
using namespace std;
ll h1[MAXN], h2[MAXN];
ll power[2][MAXN];
char str[MAXN];

ll binpow(ll a, ll b)
{
    ll ret = 1;
    while (b)
    {
        if (b & 1)
            ret = (ret * a) % MOD;
        a = (a * a) % MOD;
        b /= 2ll;
    }
    return ret;
}

ll _h1(int a, int b)
{
    if (a < 0)
        return h1[b];
    ll tmp = (h1[a] * power[0][b - a]) % MOD;
    return (h1[b] - tmp + MOD) % MOD;
}

ll _h2(int a, int b)
{
    if (a < 0)
        return h2[b];
    ll tmp = (h2[a] * power[1][b - a]) % MOD;
    return (h2[b] - tmp + MOD) % MOD;
}

void solve()
{
    scanf("%s", str);
    int n = strlen(str);
    h1[0] = h2[0] = str[0];
    for (int i = 1; i < n; i++)
    {
        h1[i] = (str[i] + h1[i - 1] * pr1 % MOD) % MOD;
        h2[i] = (str[i] + h2[i - 1] * pr2 % MOD) % MOD;
    }
    int l = -1, r = n - 1, ans = 1;
    for (int i = 0; i < n - 1 - i; i++)
    {
        if (_h1(l, i) == _h1(n - 2 - i, r) && _h2(l, i) == _h2(n - 2 - i, r))
        {
            ans += 2;
            l = i;
            r = n - 2 - i;
        }
    }
    if (l == r)
        --ans;
    printf("%d\n", ans);
}

int main()
{
    power[0][0] = power[1][0] = 1;
    for (int i = 1; i < MAXN; i++)
        power[0][i] = (power[0][i - 1] * pr1) % MOD,
        power[1][i] = (power[1][i - 1] * pr2) % MOD;
    int t;
    ni(t);
    while (t--)
        solve();
    return 0;
}

Compilation message

palindromic.cpp: In function 'void solve()':
palindromic.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", str);
     ~~~~~^~~~~~~~~~~
palindromic.cpp: In function 'int main()':
palindromic.cpp:9:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &n)
               ~~~~~^~~~~~~~~~
palindromic.cpp:95:5: note: in expansion of macro 'ni'
     ni(t);
     ^~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15992 KB Output is correct
2 Correct 23 ms 15992 KB Output is correct
3 Correct 22 ms 16048 KB Output is correct
4 Correct 21 ms 16096 KB Output is correct
5 Correct 21 ms 16252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15992 KB Output is correct
2 Correct 23 ms 15992 KB Output is correct
3 Correct 22 ms 16048 KB Output is correct
4 Correct 21 ms 16096 KB Output is correct
5 Correct 21 ms 16252 KB Output is correct
6 Correct 23 ms 16252 KB Output is correct
7 Correct 23 ms 16252 KB Output is correct
8 Correct 25 ms 16252 KB Output is correct
9 Correct 23 ms 16292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15992 KB Output is correct
2 Correct 23 ms 15992 KB Output is correct
3 Correct 22 ms 16048 KB Output is correct
4 Correct 21 ms 16096 KB Output is correct
5 Correct 21 ms 16252 KB Output is correct
6 Correct 23 ms 16252 KB Output is correct
7 Correct 23 ms 16252 KB Output is correct
8 Correct 25 ms 16252 KB Output is correct
9 Correct 23 ms 16292 KB Output is correct
10 Correct 27 ms 16376 KB Output is correct
11 Correct 24 ms 16376 KB Output is correct
12 Correct 24 ms 16376 KB Output is correct
13 Correct 24 ms 16376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15992 KB Output is correct
2 Correct 23 ms 15992 KB Output is correct
3 Correct 22 ms 16048 KB Output is correct
4 Correct 21 ms 16096 KB Output is correct
5 Correct 21 ms 16252 KB Output is correct
6 Correct 23 ms 16252 KB Output is correct
7 Correct 23 ms 16252 KB Output is correct
8 Correct 25 ms 16252 KB Output is correct
9 Correct 23 ms 16292 KB Output is correct
10 Correct 27 ms 16376 KB Output is correct
11 Correct 24 ms 16376 KB Output is correct
12 Correct 24 ms 16376 KB Output is correct
13 Correct 24 ms 16376 KB Output is correct
14 Correct 364 ms 32904 KB Output is correct
15 Correct 167 ms 32908 KB Output is correct
16 Correct 265 ms 33012 KB Output is correct
17 Correct 206 ms 33012 KB Output is correct