zoukankan      html  css  js  c++  java
  • 1593. 拆分字符串使唯一子字符串的数目最大

    az,又是一个感觉知道咋做但就是不会做的题呀

    emmmm是个暴力吧QAQ

    回溯dfs可,但是当时傻乎乎感觉不知道怎么分字符串,知道我知道还有那么个函数给分割字符串(以前不知道怎么用,现在可算是知道了)

    别忘了储存一下最大~

    class Solution {
        int maxs = 1;
        unordered_set<string> set;
    public:
        int maxUniqueSplit(string s) {
            dfs(s, 0);
            return maxs;
        }
    
        void dfs(string s, int count)
        {
            if(s == "")
            {
                maxs = max(maxs, count);
                return;
            }
            int n = s.size();
            for(int len = 1; len <= n; ++len)
            {
                string tmp = s.substr(0, len);
                string rest = s.substr(len);
                if(set.find(tmp) == set.end())//不重复
                {
                    set.insert(tmp);//记录下来
                    dfs(rest, count+1);
                    set.erase(tmp);//回溯
                }
            }
        }
    };
  • 相关阅读:
    上下界网络流——概念解析与快速入门(待修改)
    maomao的现在与未来
    exgcd证明和最基础应用
    快速入门Splay
    luogu 2515
    bzoj 1996
    *51nod 1409
    51nod 1412
    51nod 1503
    51nod 1020
  • 原文地址:https://www.cnblogs.com/zhmlzhml/p/13746460.html
Copyright © 2011-2022 走看看