1660C - Get an Even String - CodeForces Solution


dp greedy strings

Please click on ads to support us..

Python Code:

dp = [0]*200000
for _ in range(int(input())):
    S = list(input())
    N = len(S)

    dp[0] = 1
    prev = [-1]*26
    prev[ord(S[0])-97] = 0

    for i in range(1, N):
        dp[i] = dp[i-1]+1
        if prev[ord(S[i])-97] != -1:
            j = prev[ord(S[i])-97]
            dp[i] = min(dp[i], (dp[j-1] if (j-1>=0) else 0)+i-j-1)
        prev[ord(S[i])-97] = i
    print(dp[N-1])

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll      long long int 
#define vt      vector
#define pb      push_back
#define all(x)  (x).begin(), (x).end()
#define lb      lower_bound
#define ub      upper_bound
typedef pair<int, int> pairs;
const ll N = 1e7 + 1;
const int M=1e9 + 7;
#define INF 1e18
// bool isPrime(ll n)
// {
// 	if (n == 1)
// 		return false;

// 	for (ll i = 2; i * i <= n; i++) {
// 		if (n % i == 0)
// 			return false;
// 	}
// 	return true;
// }
// ll factorial(ll n)
// {
//     return (n==1 || n==0) ? 1: n * factorial(n - 1);
// }
// string bin(ll num)
// {
//     string str;
//       while(num)
//       {
//       if(num & 1) 
//         str+='1';
//       else 
//         str+='0';
//       num>>=1; // Right Shift by 1 
//     }   
//       return str;
// }
// string db(int n)
// {
//     // Size of an integer is assumed to be 32 bits
//     string ans;
//     for (int i = 64; i >= 0; i--)
//     {
//         int k = n >> i;
//         if (k & 1)
//             ans += '1';
//         else
//             ans += '0';
//     }
//     return ans;
// }

// string rev(string x)
// {
// 	string ans=x;
// 	reverse(all(ans));
// 	return ans;
// }
// ll step(ll x, ll p) 
// {
// 	ll ans = 1;
// 	while(p) 
// 	{
// 		if(p&1)
// 			ans=ans*x%M;
// 		p/=2;
// 		x=x*x%M;
// 	}
// 	return ans;
// }
int main()
{
	int t=1;
	cin>>t;
	while(t--)
	{
		ll n,m=1,j=0,z,h,c=0,d=0,ans=0,mx=0,mn=0,sum=0,f=0,f1=0,k,l,r,x,y,y1,y2,x1,x2;
		string s;
		cin>>s;
		// ll a[n];
		// vt<ll> v,v1,v2;
		// for(ll i=0;i<n;i++)
		//     cin>>a[i];
		unordered_map<char,ll> pending;
        for(ll i=0;i<s.size();i++)
        {
            if(pending[s[i]])
            {
                ans+=(pending.size()-1);
                pending.clear();
            }
            else
            {
                pending[s[i]]=1;
            }
        }
        ans+=pending.size();
        
	    cout<<ans<< endl;
		
		
		
		
		
		
		
		
		

        
        // for(auto x:v)
        // 	cout<<x<<" ";
        // cout<<endl;
        // for(auto x:v2)
        // 	cout<<x<<" ";
        // cout<<endl;
        
		
		
		// if(c>d1)
		// 	cout<<"ALEX"<<endl;
		// else if(c<d1)
        //     cout<<"BOB"<<endl;
        // else
        // 	cout<<"EQUAL"<<endl;
           
	

	}         
   
}
    

		
		



		
		
			
			
		
	

		
		

		
		
		
		



Comments

Submit
0 Comments
More Questions

1692B - All Distinct
1156C - Match Points
1675A - Food for Animals
1328C - Ternary XOR
1689A - Lex String
1708B - Difference of GCDs
863A - Quasi-palindrome
1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES