Submission #1370362

#TimeUsernameProblemLanguageResultExecution timeMemory
1370362pirmyratgCopy and Paste 3 (JOI22_copypaste3)C++20
62 / 100
300 ms49592 KiB
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second    
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
#define mm make_pair
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 2505;
const int md = 1e9+7;
const int B = 77777;
const ll inf = 1e18;
int n, a, b, c, pw[N], dv[N], pr[N];
ll dp[N][N];
char s[N];
int mul(int x, int y){
	return 1LL*x*y%md;
}
int ad(int x, int y){
	return x+y>=md?x+y-md:x+y;
}
int sb(int x, int y){
	return x-y<0?x-y+md:x-y;
}
int qp(int x, int k){
	int r=1;
	while(k){
		if(k&1)
			r=mul(r, x);
		x=mul(x, x);
		k>>=1;
	}
	return r;
}
int gH(int l, int r){
	return mul(sb(pr[r], pr[l-1]), dv[l]);
}
int main(){
	scanf("%d", &n);
	scanf("%s", s+1);
	scanf("%d%d%d", &a, &b, &c);
	pw[0]=1;
	for(int i = 1; i <= n; ++i)
		pw[i]=mul(pw[i-1], B);
	dv[n]=qp(pw[n], md-2);
	for(int i = n-1; i >= 0; --i)
		dv[i]=mul(dv[i+1], B);
	for(int i = 1; i <= n; ++i)
		pr[i]=ad(pr[i-1], mul(pw[i], s[i]-'a'));
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			dp[i][j]=inf;
	for(int l = 0; l < n; ++l){
		vector<int> to(n+1, -1);
		map<int, int> mp;
		for(int i = n-l; i >= 1; --i){
			int j=i+l;
			if(j+1+l<=n)
				mp[gH(j+1, j+1+l)]=j+1;
			int h=gH(i, j);
			if(mp.count(h))
				to[i]=mp[h];
			if(i==j)
				dp[i][j]=a;
			else 
				umin(dp[i][j], min(dp[i+1][j], dp[i][j-1])+a);
			int x=i, ct=1;
			while(to[x]!=-1){
				x=to[x], ct++;
				umin(dp[i][x+l], dp[i][j]+b+1LL*c*ct+1LL*a*((x+l-i+1)-ct*(l+1)));
			}
		}
	}
	printf("%lld\n", dp[1][n]);
	return 0;
}

Compilation message (stderr)

copypaste3.cpp: In function 'int main()':
copypaste3.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~
copypaste3.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         scanf("%s", s+1);
      |         ~~~~~^~~~~~~~~~~
copypaste3.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         scanf("%d%d%d", &a, &b, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...