DP-Algo
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main(){
|
||||
int len, n;
|
||||
cin >> len >> n;
|
||||
vector<int> a(n + 2);
|
||||
vector<vector<int>> dp(n + 2, vector<int>(n + 2, 0));
|
||||
//<2F><><EFBFBD><EFBFBD>dp[i][j]Ϊ<>и<EFBFBD>ľ<EFBFBD><C4BE>[i, j]<5D><> 0|---1|-----2|----3|--4| <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD> a[0] = 0 a[1] = x,,, a[n + 1] = len
|
||||
a[0] = 0; a[n + 1] = len;
|
||||
for(int i = 1; i <= n; i++){
|
||||
cin >> a[i];
|
||||
}
|
||||
//a[i]<5D>Ƿֵ<C7B7>
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
for(int i = 0; i <= n + 1; i++){
|
||||
for(int j = i; j <= n + 1 ;j++){
|
||||
dp[i][j] = 114514;
|
||||
for(int k = i + 1; k < j; k++){
|
||||
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[j] - a[i]);
|
||||
}
|
||||
if(dp[i][j] == 114514) dp[i][j] = 0;
|
||||
}
|
||||
} */
|
||||
// <20><><EFBFBD><EFBFBD> DP<44><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>
|
||||
for(int l = 2; l <= n + 1; l++){ // <20><><EFBFBD>䳤<EFBFBD><E4B3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2
|
||||
for(int i = 0; i + l <= n + 1; i++){
|
||||
int j = i + l;
|
||||
dp[i][j] = INT_MAX;
|
||||
for(int k = i + 1; k < j; k++){
|
||||
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[j] - a[i]);
|
||||
}
|
||||
if(dp[i][j] == INT_MAX) dp[i][j] = 0;
|
||||
}
|
||||
}
|
||||
cout << dp[0][n + 1] << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int T;
|
||||
if(!(cin >> T)) return 0;
|
||||
while(T--){
|
||||
string s;
|
||||
cin >> s;
|
||||
int n = s.size();
|
||||
if(n == 0){
|
||||
cout << 0 << "\n";
|
||||
continue;
|
||||
}
|
||||
// isPal[i][j] whether s[i..j] is palindrome
|
||||
vector<vector<char>> isPal(n, vector<char>(n, 0));
|
||||
// center expand to fill isPal (or use DP)
|
||||
for(int center = 0; center < n; ++center){
|
||||
// odd length
|
||||
int l = center, r = center;
|
||||
while(l >= 0 && r < n && s[l] == s[r]){
|
||||
isPal[l][r] = 1;
|
||||
--l; ++r;
|
||||
}
|
||||
// even length
|
||||
l = center; r = center + 1;
|
||||
while(l >= 0 && r < n && s[l] == s[r]){
|
||||
isPal[l][r] = 1;
|
||||
--l; ++r;
|
||||
}
|
||||
}
|
||||
|
||||
// dp[i] = min cuts for s[0..i]
|
||||
const int INF = 1e9;
|
||||
vector<int> dp(n, INF);
|
||||
for(int i = 0; i < n; ++i){
|
||||
if(isPal[0][i]){
|
||||
dp[i] = 0;
|
||||
} else {
|
||||
for(int j = 0; j < i; ++j){
|
||||
if(isPal[j+1][i]){
|
||||
dp[i] = min(dp[i], dp[j] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << dp[n-1] << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
const int INF = 1e9;
|
||||
bool match(char a, char b){
|
||||
return (a == '(' && b == ')') || (a == '[' && b == ']');
|
||||
}
|
||||
int main(){
|
||||
string s;
|
||||
cin >> s;
|
||||
int n = s.size();
|
||||
vector<vector<int>> dp(s.size() + 2, vector<int>(s.size() + 2, 1));
|
||||
for(int l = 2; l <= n; l++){
|
||||
for(int i = 0; i < n - l + 1; i++){
|
||||
int j = i + l - 1;
|
||||
dp[i][j] = INF;
|
||||
for(int k = i; k < j; k++){
|
||||
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
|
||||
}
|
||||
if(match(s[i], s[j])) {
|
||||
if (i + 1 <= j - 1)
|
||||
dp[i][j] = min(dp[i][j], dp[i+1][j-1]);
|
||||
else
|
||||
dp[i][j] = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ƥ<EFBFBD><C6A5>
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << dp[0][n - 1] << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user