Problem Solution

1 solutions

  • 1
    @ 2024-12-12 16:44:38

    Problem setter: Kamonasish Roy (Bullet)
    Problem tag : Bit mask DP
    Tutorial (Prepared by Abu Sufian Rafi (ASRafi) : To solve this problem, we use a Bit-masking Dynamic Programming (DP) approach.
    The DP state is defined as dp[i][last][mask], where i is the current index in the string, last represents the last vowel group used, and mask is a bitmask that tracks which vowel groups have been used.
    The transitions involve either extending the current group by transforming S[i] to match last, or starting a new group by transforming S[i] to a different vowel type not yet used (tracked via mask).
    Time Complexity: O(N * 5 * 2^5).
    Note : You can solve it using the permutation method, which is also accepted. Time complexity: O( 5! * 5 * N).

    **Permutation method Code **:

    #include<bits/stdc++.h>
    using namespace std;
    const long long M=2e5+10,MOD=1000000007;
    typedef long long ll;
    int cost[26][26];
    vector<string>all;
    void precal(){
    	string vowel="aeiou";
    	for(int i=0;i<(int)vowel.size();i++){
    		int l=i;
    		int cnt=0;
    		while(cnt<5){
    			cost[vowel[i]-'a'][vowel[l]-'a']=cnt;
    			l++;
    			cnt++;
    			if(l==5)l=0;
    			
    		}
    		
    	}
    	all.push_back(vowel);
    	while(next_permutation(vowel.begin(),vowel.end())){
    		all.push_back(vowel);
    	}
    	
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t=1;
        precal();
        cin>>t;
        while(t--){
        	int n;
        	cin>>n;
        	string s;
        	cin>>s;
        	int mx=1e9;
        	for(auto it:all){
        		string p=it;
        		vector<vector<int>>dp(n+1,vector<int>(5,0));
        		for(int i=0;i<5;i++){
        			for(int j=1;j<=n;j++)
        			{
        				int pro=cost[s[j-1]-'a'][p[i]-'a'];
        				if(i==0){
        					dp[j][i]=dp[j-1][i]+pro;
        				}
        				else{
        					dp[j][i]=dp[j-1][i]+pro;
        					dp[j][i]=min(dp[j][i],dp[j][i-1]);
        				}
        			}
        			mx=min(mx,dp[n][i]);
        		}
        	}
        	cout<<mx<<"\n";
        	
        	
       
         
    
        
        
    }
    
        
        return 0;
     
    }
    

    Using Bit mask ( _Ash__ ):

    #include <bits/stdc++.h>
    using namespace std;
    
    int get_index(char ch) {
        string vowels = "aeiou";
        for(int i = 0; i < vowels.size(); i++) {
            if(vowels[i] == ch) {
                return i;
            }
        }
        assert(false);
    }
    
    // i -> j
    int get_cost(int i, int j) {
        if(j >= i) return j - i;
        return j - i + 5;
    }
    
    int solve(string s) {
        int n = s.size();
        vector<int> v(n);
        for(int i = 0; i < n; i++) {
            v[i] = get_index(s[i]);
        }
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(1<<5, vector<int>(5, 1e9)));
        for(int i = 0; i < n; i++) {
            for(int mask = 0; mask < (1<<5); mask++) {
                for(int j = 0; j < 5; j++) {
                    if((mask >> j) & 1) {
                        continue;
                    }
                    // 
                    int prev_best = (i > 0 ? dp[i - 1][mask][j] : 0);
                    for(int k = 0; k < 5; k++) {
                        if((mask >> k) & 1) {
                            prev_best = min(prev_best, (i > 0 ? dp[i - 1][mask ^ (1<<k)][k] : 0));
                        }
                    }
                    dp[i][mask][j] = prev_best + get_cost(v[i], j);
                }
            }
        }
    
        int ans = 1e9;
        for(int mask = 0; mask < (1<<5); mask++) {
            for(int j = 0; j < 5; j++) {
                ans = min(ans, dp[n - 1][mask][j]);
            }
        }
        return ans;
    }
    
    
    int main() {
        int tc;
        cin >> tc;
        while(tc--) {
            int n;
            cin >> n;
            string s;
            cin >> s;
            cout << solve(s) << endl;
        }
    }
    
    
  • 1

Information

ID
1140
Difficulty
5
Category
(None)
Tags
(None)
# Submissions
70
Accepted
23
Accepted Ratio
33%
Uploaded By