Leetcode 691. 贴纸拼词 (DAY 67)---- 动态规划学习期(C++初回手感 感觉比昨天好得多了)

原题题目

在这里插入图片描述


代码实现(首刷小部分看解大部分自解)

class Solution {
    
    
public:
    int minStickers(vector<string>& stickers, string target) {
    
    
        int strl = target.size(),temp;
        vector<int> dp (1<<strl,-1);
        dp[0] = 0;
        for(int i=0;i< 1<<strl;i++)
        {
    
    
            if(dp[i] == -1) 
                continue;
            for(string str:stickers)
            {
    
    
                temp = i;
                for(char chr:str)
                {
    
    
                    for(int j=0;j<strl;j++)
                    {
    
    
                        if(chr != target[j] || temp & (1<<j))
                            continue;
                        temp |= 1<<j;
                        if(dp[temp] == -1) dp[temp] = dp[i] + 1;
                        else    dp[temp] = fmin(dp[temp],dp[i] + 1);
                        break;
                    }
                }
            }
        }
        return dp[(1<<strl)-1];
    }
};

猜你喜欢

转载自blog.csdn.net/qq_37500516/article/details/115112367
今日推荐