leetcode5520


//给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目。


//字符串 s 拆分后可以得到若干 非空子字符串 ,这些子字符串连接后应当能够还原为原字符串。但是拆分出来的每个子字符串都必须是 唯一的 。


/注意:子字符串 是字符串中的一个连续字符序列。


class
Solution { public: int maxUniqueSplit(string s) { unordered_set<string> st; int count = 0; using fdd = function<void(int,int)>; fdd dfs = [&](int i,int size) { if (size-i+st.size()<=count) return; if (i == size) { count = max(count, (int)st.size()); return; } string temp =""; for (int j = i; j < s.size(); j++) { temp += s[j]; if (st.find(temp)==st.end()) { st.insert(temp); dfs(j+1,size); st.erase(temp); } } }; dfs(0,s.size()); return count; } };

1.用回溯搜素所有可能!!

2.没想到剪枝方法,用剪枝和不用剪枝差距很大

posted @ 2020-09-21 09:44  十里坡剑神—》》》》  阅读(124)  评论(0编辑  收藏  举报