Submission #863863

# Submission time Handle Problem Language Result Execution time Memory
863863 2023-10-21T09:26:15 Z catlover Palinilap (COI16_palinilap) C++17
0 / 100
1000 ms 9048 KB
// cre: Nguyen Hoang Hung - Train VOI 2024

#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define debug(x) { cerr << #x << " = "; cerr << (x) << endl; }
#define PR(a,n) { cerr << #a << " = "; FOR(_,1,n) cerr << a[_] << ' '; cerr << endl; }
#define PR0(a,n) { cerr << #a << " = "; REP(_,n) cerr << a[_] << ' '; cerr << endl; }

#define bit(X, i) ((X >> i) & 1)
#define cntbit(X) __builtin_popcountll(X)
#define fi first
#define se second
#define pb push_back
#define all(X) begin(X), end(X)
#define SZ(X) ((int)(X).size())
#define RR(X) X.resize(unique(all(X)) - begin(X))
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

using namespace std;

template <typename T>
bool maximize(T &x, T y)
{
  if(x < y)
    x = y;
  else
    return 0;
  return 1;
}
template <typename T>
bool minimize(T &x, T y)
{
  if(x > y)
    x = y;
  else
    return 0;
  return 1;
}

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll Rand(ll l, ll r) { return l + rand() % (r - l + 1); }

const int maxN = 1e6;
const ll inf = 1e18+7;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
const int base = 31;

/*
-------------------------------------------------------------------------------------
    END OF TEMPLATE
-------------------------------------------------------------------------------------
    Nguyen Hoang Hung - catlover
    Training for VOI24 gold medal
-------------------------------------------------------------------------------------
*/

string S;
int N;
int Hash[maxN+5], rHash[maxN+5];
int pw[maxN+5];

void in()
{
    cin >> S;
    N = SZ(S);
}

int getHash(int l, int r, int H[maxN+5])
{
    return (H[r] - H[l - 1] * pw[r - l + 1] + 1ll * MOD * MOD) % MOD;
}

bool check_palin(int i, int j)
{
    int tmp1 = getHash(i, j, Hash);
    int tmp2 = getHash(N - j + 1, N - i + 1, rHash);
    return tmp1 == tmp2;
}

void init()
{
    string tmp = S;

    S = ' ' + S;
    reverse(all(tmp));
    tmp = ' ' + tmp;

    pw[0] = 1;
    Hash[0] = 0;
    rHash[0] = 0;

    FOR(i, 1, N)
    {
        Hash[i] = (Hash[i - 1] * base + S[i] - 'a') % MOD;
        rHash[i] = (rHash[i - 1] * base + tmp[i] - 'a') % MOD;
        pw[i] = pw[i - 1] * base % MOD;
    }
}

int calc()
{
    int res = 0;
    FOR(i, 1, N)
    {
        FOR(j, i, N)
        {
            if(check_palin(i, j)) ++res;
        }
    }
    return res;
}

void trau()
{
    string o = S;
    int ans = 0;
    FOR(i, 0, N - 1)
    {
        FOR(j, 0, 25)
        {
            S = o;
            S[i] = char(j + 'a');
            init();
            maximize(ans, calc());
        }
    }
    cout << ans;
}

void solve() {
    in();
    trau();
}

void solve_TestCase() {
  int testcase; cin >> testcase;

  while(testcase --> 0)
  {
    solve();
  }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    file("PALIVAL");

//    solve_TestCase();

    solve();

    cerr << "\nTime : " << clock()/1000.0 << "s\n";

    return 0;
}

Compilation message

palinilap.cpp: In function 'int main()':
palinilap.cpp:22:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palinilap.cpp:156:5: note: in expansion of macro 'file'
  156 |     file("PALIVAL");
      |     ^~~~
palinilap.cpp:22:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
palinilap.cpp:156:5: note: in expansion of macro 'file'
  156 |     file("PALIVAL");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 4440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1070 ms 4444 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1024 ms 9048 KB Time limit exceeded
2 Halted 0 ms 0 KB -