1132F - Clear the String - CodeForces Solution


dp *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h> 
 
#define int long long 
#define pii pair <int,int> 
#define st first 
#define nd second 
#define vi vector <int> 
#define vpii vector <pii> 
#define pb push_back 
#define pf push_front 
#define FOR(a, b, c) for (int i = a; i <= b; i += c) 
#define ROS(a, b, c) for (int i = a; i >= b; i -= c) 
#define lb lower_bound 
#define ub upper_bound 
#define FILE "" 
#define trii pair <int, pii> 
using namespace std;
 
const int oo = 1e18;
const int N = 500 + 9;
const int mod = 1e9 + 7;
const int blocks = 448;
 
int n, k, a[N];
int res, f[N][N];
string s;

int dp(int l, int r) 
{
	int &res = f[l][r];
	if (res != -1) return res;
	if (l > r) return res = 0;
	if (l == r) return res = 1;
	res = dp(l + 1, r) + 1;
	for (int i = l + 1; i <= r; i++) 
	{
		if (s[i] == s[l]) 
		{
			res = min(res, dp(l + 1, i - 1) + dp(i, r));
		}
	}	
	return res;
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen(FILE".inp","r", stdin);
	//freopen(FILE".out","w", stdout);
	cin >> n >> s;
	memset(f, -1, sizeof(f));
	int ans = dp(0, n - 1);
	cout << ans;
	return 0;
}


Comments

Submit
0 Comments
More Questions

1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations