NC255 最长有效的括号字符子序列

思路:选取字符和不选取字符

但是TLE了

需要加以下内容剪枝

     if (i != idx && s[i] == s[i - 1]) continue;//该行可以避免同一层选取同样的字符

    int c1 = 0, c2 = 0, c3 = 0;//c1是(总数,c2是)总数,c3是字母总数

        if (cnt > c2) return;//cnt代表还需要匹配的(数,即)数目不够

        if (!ans.empty() && path.size() + (n - idx) < ans[0].size()) return;//path和idx之后的都能匹配也小于最长长度

此外会有重复的path,需要标记下path
                if (mp.find(path) != mp.end()) return;

#include <unordered_map>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return string字符串vector
     */
    unordered_map<string, int>mp;
    int c1 = 0, c2 = 0, c3 = 0;//c1是(总数,c2是)总数,c3是字母总数
    void dfs(string& s, vector<string>& ans, string path,  int idx, int cnt,
             int& n) {
        if (cnt < 0) return;
        if (cnt > c2) return;//cnt代表还需要匹配的(数
        if (!ans.empty() && path.size() + (n - idx) < ans[0].size()) return;

        if (idx == n) {
            if (path.size() < c3) return;
            if (cnt || mp.find(path) != mp.end()) return;
            if (ans.empty() || ans[0].size() < path.size()) {

                ans.clear();
                ans.push_back(path);
                mp[path] = 1;
            } else if (ans[0].size() == path.size() ) {
                if (mp.find(path) != mp.end()) return;
                else mp[path] = 1;
                ans.push_back(path);
            }
            return;
        }
        for (int i = idx; i < n; i++) {
            if (i != idx && s[i] == s[i - 1]) continue;//该行可以避免同一层选取同样的字符

            dfs(s, ans, path, i + 1, cnt, n);
            char tmp = s[i];
            string path2 = "";
            switch (tmp) {
                case '(':
                    path2 = path + "(" ;
                    dfs(s, ans, path2, i + 1, cnt + 1, n);
                    break;
                case ')':
                    if (cnt > 0) {
                        path2 = path + ")";
                        dfs(s, ans, path2, i + 1, cnt - 1, n);
                    }
                    break;
                default:
                    if (cnt < 0) return;
                    path2 = path + tmp;
                    dfs(s, ans, path2, i + 1, cnt, n);
                    break;

            }

        }
    }
    vector<string> maxValidParenthesesStr(string s) {
        // write code here
        int n = s.size();
        if (n < 2) return {""};
        for (int i = 0; i < n; i++) {
            if (s[i] == '(')
                c1++;
            else if (s[i] == ')')
                c2++;
            else c3++;
        }
        vector<string> ans;
        string path;
        dfs(s, ans, path, 0, 0, n);

        return ans;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值