Submission #1307102

#TimeUsernameProblemLanguageResultExecution timeMemory
1307102reginoxCopy and Paste 3 (JOI22_copypaste3)C++20
62 / 100
2583 ms216580 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(v) begin(v), end(v)
#define pi pair<int, int>
#define vi vector<int>
using namespace std;
const int N = 2503;
const ll mod = 1e9+7, base = 311;
int n; ll a, b, c;
ll h[N], pw[N] = {1};
string s;
map<int, int> rc[N];
int nx[N][N];

ll get(int l, int r){
  return (h[r] - (h[l-1] * pw[r-l+1] % mod) + mod) % mod;
}

void init(){
  for(int i = 1; i <= n; i++){
    h[i] = (h[i-1] * base + s[i]) % mod;
  }
  for(int i = 1; i < N; i++) pw[i] = (pw[i-1] * base) % mod;
  for(int i = n; i >= 1; i--){
    for(int j = i; j <= n; j++){
      int hc = get(i, j);
      if(rc[j-i].count(hc)) nx[i][j] = rc[j-i][hc];
      else nx[i][j] = n+1;
    }
    for(int j = i; j <= n; j++){
      int hc = get(j, j+j-i);
      rc[j-i][hc] = j;
    }
  }
}

ll dp[N][N];

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> n >> s >> a >> b >> c;
  s = ' ' + s;
  init();
  memset(dp, 0x3f, sizeof(dp));
  for(int i = 1; i <= n; i++) dp[i][i+1] = a+b;
  for(int len = 1; len < n; len++){
    for(int i = 1; i + len - 1 <= n; i++){
      int j = i + len;
      if(i > 1){
        dp[i-1][j] = min(dp[i-1][j], dp[i][j] + a);
      }
      if(j <= n){
        dp[i][j+1] = min(dp[i][j+1], dp[i][j] + a);
      }
      ll cur = i, cnt = 1;
      while(nx[cur][cur + len-1] <= n){
        if(nx[cur][cur+len-1] == 0) break;
        cur = nx[cur][cur+len-1];
        cnt++;
        dp[i][cur + len] = min(dp[i][cur + len], dp[i][j] + c * cnt + b + a * (cur + len - i - 1ll * cnt * len));
      }
    }
  }
  // for(int i = 1; i <= n; i++){
  //   for(int j = 1; i + j - 1 <= n; j++){
  //     cout << i << " " << i+j << " ";
  //     cout << (dp[i][i+j] >= 1e18? -1:dp[i][i+j]) << " ";
  //   cout << "\n";
  //   }
  // }
  cout << dp[1][n+1] - b;
  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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...