Submission #20992

# Submission time Handle Problem Language Result Execution time Memory
20992 2017-03-27T09:35:38 Z model_code Palinilap (COI16_palinilap) C++11
100 / 100
146 ms 25284 KB
// Autor: Ivan Katanic

#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long llint;

const int MAX = 100100;
const int H = 9973;
const int mod = 1e9 + 7;

int pw[MAX];

int h[MAX];
int hr[MAX];
char s[MAX];
char sr[MAX];
int n;

void calcHashes(char* s, int* h) {
  h[0] = 1;
  for (int i = 0; i < n; ++i)
    h[i+1] = (h[i]*1LL*H + s[i]) % mod;
}

int getHash(int* h, int a, int b) {
  return ((h[b+1] - h[a]*1LL*pw[b-a+1]) % mod + mod) % mod;
}

bool revEqual(int a, int b, int c, int d) {
  return getHash(h, a, b) == getHash(hr, n-d-1, n-c-1);
}

int main(void) {
  scanf("%s", s);
  n = strlen(s);

  pw[0] = 1;
  for (int i = 0; i < n; ++i)
    pw[i+1] = (pw[i]*1LL*H) % mod;
  
  for (int i = 0; i < n; ++i)
    sr[i] = s[n-i-1];
  calcHashes(s, h);
  calcHashes(sr, hr);

  static llint f[MAX][26]; // created 
  static llint fk[MAX], fl[MAX]; // destroyed

  llint base = 0;
  for (int i = 0; i < n; ++i)
    for (int j = i; j <= i+1; ++j) {
      int lo = 0, hi = min(i+1, n-j);
      while (lo < hi) {
        int mid = (lo + hi + 1) / 2;
        if (revEqual(i-mid+1, i, j, j+mid-1)) lo = mid;
        else hi = mid - 1;
      }
      int r = lo;
      base += r;

      if (i == j) { // odd
        fk[j+1] += -1, fk[j+r] -= -1;
        fl[j+1] += j+r, fl[j+r] -= j+r;
        
        fk[i-r+1] += 1, fk[i] -= 1;
        fl[i-r+1] += -(i-r), fl[i] -= -(i-r);
      } else { // even
        fk[j] += -1, fk[j+r] -= -1;
        fl[j] += j+r, fl[j+r] -= j+r;
        
        fk[i-r+1] += 1, fk[i+1] -= 1;
        fl[i-r+1] += -(i-r), fl[i+1] -= -(i-r);
      }
      
      int x = i-r, y = j+r;
      if (x < 0 || y >= n) continue;
    
      r++;
      lo = 0, hi = min(i+1, n-j) - r;
      while (lo < hi) {
        int mid = (lo + hi + 1) / 2;
        if (revEqual(x-mid, x-1, y+1, y+mid)) lo = mid;
        else hi = mid - 1;
      }

      f[x][s[y]-'a'] += lo+1;
      f[y][s[x]-'a'] += lo+1;
    }
  
  llint best_change = 0;
  llint k = 0, l = 0;
  for (int i = 0; i < n; ++i) {
    k += fk[i], l += fl[i];
    for (int j = 0; j < 26; ++j)
      if (j+'a' != s[i]) {
        best_change = max(best_change, f[i][j] - (llint(i)*k + l));
      }
  }

  printf("%lld\n", base + best_change);
  return 0;
}

Compilation message

palinilap.cpp: In function 'int main()':
palinilap.cpp:39:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", s);
                 ^

# Verdict Execution time Memory Grader output
1 Correct 0 ms 25284 KB Output is correct
2 Correct 0 ms 25284 KB Output is correct
3 Correct 0 ms 25284 KB Output is correct
4 Correct 0 ms 25284 KB Output is correct
5 Correct 0 ms 25284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 25284 KB Output is correct
2 Correct 3 ms 25284 KB Output is correct
3 Correct 3 ms 25284 KB Output is correct
4 Correct 0 ms 25284 KB Output is correct
5 Correct 3 ms 25284 KB Output is correct
6 Correct 0 ms 25284 KB Output is correct
7 Correct 3 ms 25284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 25284 KB Output is correct
2 Correct 79 ms 25284 KB Output is correct
3 Correct 79 ms 25284 KB Output is correct
4 Correct 106 ms 25284 KB Output is correct
5 Correct 96 ms 25284 KB Output is correct
6 Correct 93 ms 25284 KB Output is correct
7 Correct 93 ms 25284 KB Output is correct
8 Correct 59 ms 25284 KB Output is correct
9 Correct 99 ms 25284 KB Output is correct
10 Correct 113 ms 25284 KB Output is correct
11 Correct 83 ms 25284 KB Output is correct
12 Correct 146 ms 25284 KB Output is correct
13 Correct 119 ms 25284 KB Output is correct
14 Correct 103 ms 25284 KB Output is correct
15 Correct 93 ms 25284 KB Output is correct
16 Correct 103 ms 25284 KB Output is correct
17 Correct 106 ms 25284 KB Output is correct
18 Correct 113 ms 25284 KB Output is correct
19 Correct 106 ms 25284 KB Output is correct