#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef tuple<ll,int,int> t3;
const int N=2505;
const int mod=1e9+7;
int n;
ll a,b,c;
string s;
ll dp[N][N];
bool vis[N][N];
ll hsh[N];
ll sub(int l,int r){
return (hsh[r]-hsh[l-1]+mod)%mod;
}
priority_queue<t3,vector<t3>,greater<t3>> pq;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> s >> a >> b >> c;
s=" "+s;
for(int i=1;i<=n;i++)hsh[i]=(hsh[i-1]*29%mod+s[i]-'a')%mod;
for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)dp[i][j]=1e18;
dp[0][0]=0;
pq.emplace(0,0,0);
while(!pq.empty()){
auto [d,i,j]=pq.top();
pq.pop();
if(vis[i][j])continue;
vis[i][j]=true;
if(i==n){
cout << d;
return 0;
}
if(i+1<=n&&d+a<dp[i+1][j]){
dp[i+1][j]=d+a;
pq.emplace(dp[i+1][j],i+1,j);
}
if(d+b<dp[0][i]){
dp[0][i]=d+b;
pq.emplace(dp[0][i],0,i);
}
if(i+j<=n&&sub(1,j)==sub(i+1,i+j)){
if(d+c<dp[i+j][j]){
dp[i+j][j]=d+c;
pq.emplace(dp[i+j][j],i+j,j);
}
}
}
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:33:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | auto [d,i,j]=pq.top();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
191 ms |
45172 KB |
Output is correct |
4 |
Correct |
110 ms |
47088 KB |
Output is correct |
5 |
Correct |
489 ms |
80104 KB |
Output is correct |
6 |
Correct |
24 ms |
50900 KB |
Output is correct |
7 |
Correct |
496 ms |
120168 KB |
Output is correct |
8 |
Correct |
27 ms |
54320 KB |
Output is correct |
9 |
Correct |
24 ms |
55636 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |