Submission #549398

#TimeUsernameProblemLanguageResultExecution timeMemory
549398toloraiaCopy and Paste 3 (JOI22_copypaste3)C++14
62 / 100
3091 ms220388 KiB
#include<bits/stdc++.h> #define ll long long #define ull unsigned ll #define f first #define s second #define pii pair<int,int> #define pll pair<ll,ll> #define pb push_back #define epb emplace_back using namespace std; const int NMAX = 2501; const int inf = 1e9 + 1; const ll linf = 1e18 + 1; ll mod[] ={1000000007, 1000000009, 998244353}; ll pr[] = {29, 31, 37}; ll po[NMAX][2]; ll h[NMAX][2]; void hashing(string s){ h[0][0] = 0; h[0][1] = 0; h[0][2] = 0; int n = s.size(); for(int i = 1; i <= n; i++){ ll x = s[i - 1] - 'a' + 1; for(int j = 0; j < 2; j++){ h[i][j] = h[i - 1][j] * pr[j] + x; h[i][j] %= mod[j]; } } } int get(int l, int r, int ind){ ll x = h[r][ind] - h[l - 1][ind] * po[r - l + 1][ind]; x %= mod[ind]; if (x < 0) x += mod[ind]; return x; } ll dp[NMAX][NMAX]; short adj[NMAX][NMAX]; map<pii, int> mp; vector <pii> vec[NMAX]; vector <int> v[NMAX]; int main(){ po[0][0] = po[0][1] = 1; ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int k = 0; k < 2; k++){ for(int i = 1; i <=n; i++){ po[i][k] = po[i - 1][k] * pr[k]; po[i][k] %= mod[k]; } } for(int i = 0; i <= n; i++){ for(int j = 0; j <= n; j++){ dp[i][j] = linf; } } string s; cin >> s; ll a, b, c; cin >> a >> b >> c; hashing(s); for(int i = 1; i <= n; i++){ for(pii to : vec[i - 1]) mp[to] = i - 1; for(int x,x1,v,j = i; j <= n; j++){ x = get(i, j, 0); x1 = get(i, j, 1); v = mp[{x, x1}]; if(v != 0) adj[i][j - i + 1] = v - (j - i); vec[j].pb({x, x1}); } } ll cnt[n + 1]; for(ll r = 1; r <= n; r++){ for(int i = 1; i <= n; i++)v[i].clear(); for(int i = 0; i <= n; i++)cnt[i] = 0; for(int cur,i = 1; i <= r; i++){ cur = r - i + 1; while(cur){ v[cur].pb(i); cur = adj[cur][i]; } } ll mn = 0; dp[r][r] = a; for(int l = r; l >= 1; l--){ ll val; for(int to, ind = 0; ind < v[l].size(); ind++){ to = v[l][ind]; cnt[to]++; val = dp[r - to + 1][r] + b; val += (c - (ll)to * a) * cnt[to]; mn = min(mn, val); } dp[l][r] = mn + (ll)(r - l + 1) * a; dp[l][r] = min(dp[l][r], dp[l][r - 1] + a); } } cout << dp[1][n]; return 0; }

Compilation message (stderr)

copypaste3.cpp: In function 'int get(int, int, int)':
copypaste3.cpp:38:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   38 |   if (x < 0)
      |   ^~
copypaste3.cpp:40:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   40 |     return x;
      |     ^~~~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:95:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |             for(int to, ind = 0; ind < v[l].size(); ind++){
      |                                  ~~~~^~~~~~~~~~~~~
copypaste3.cpp: In function 'void hashing(std::string)':
copypaste3.cpp:24:11: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
   24 |     h[0][2] = 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...