Submission #897194

#TimeUsernameProblemLanguageResultExecution timeMemory
897194PoPularPlusPlusCopy and Paste 3 (JOI22_copypaste3)C++17
30 / 100
831 ms116360 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(),x.end()
#define vf first 
#define vs second
const int mod = 998244353;

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int P = 71;

int convert(char c){
	return (c-'a') + 1;
}

void solve(){
	int n;
	cin >> n;
	string s;
	cin >> s;
	int a , b , c;
	cin >> a >> b >> c;
	ll dp[n][n];
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			dp[i][j] = 1e18;
		}
	}

	unordered_map<int , vector<int>> m;

	int hash_val[n][n];

	for(int i = 0; i < n; i++){
		ll pow = 1;
		ll hash = 0;
		for(int j = i; j < n; j++){
			hash += convert(s[j]) * pow;
			pow *= P;
			hash %= mod;
			pow %= mod;
			hash_val[i][j] = hash;
			m[hash].pb(i);
		}
	}

	int next[n][n];
	memset(next,-1,sizeof next);

	for(int i = 0; i < n; i++){
		for(int j = i; j < n; j++){
			int pos = upper_bound(all(m[hash_val[i][j]]),j) - m[hash_val[i][j]].begin();
			if(pos < m[hash_val[i][j]].size()){
				next[i][j] = m[hash_val[i][j]][pos];
			}
		}
	}

	for(int len = 0; len < n; len++){
		for(int i = 0; i + len < n; i++){
			int j = i + len;
			dp[i][j] = min(dp[i][j],(len+1LL)*a);
			if(len > 0){
				dp[i][j] = min(dp[i][j] , min(dp[i+1][j] , dp[i][j-1]) + a);
			}
			ll val = dp[i][j] + b;
			val += c;
			int x = i , y = j;
			while(next[x][y] != -1){
				int pos = next[x][y];
				val += c;
				val += (pos - (y + 1LL))*a;
				dp[i][pos + len] = min(dp[i][pos+len],val);
				x = pos;
				y = pos + len;
			}
		}
	}

	cout << dp[0][n-1] << '\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//int t;cin>>t;
	//while(t--)
		solve();
	return 0;
}

Compilation message (stderr)

copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:56:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |    if(pos < m[hash_val[i][j]].size()){
      |       ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...