题解 | #LUCKY STRING#
LUCKY STRING
https://www.nowcoder.com/practice/9ef1046e746248fe93751e37126bb9e0
朴素的思路,用unordered_set去重。代码如下:
#include "bits/stdc++.h" using namespace std; int main(){ string s; cin>>s; unordered_set<string> res; //记录答案并去掉重复答案 for(int i=0;i<s.length();++i){ int a = 1, b = 1, n = 1, n_pre = 1; string tmp; //用set存储string,计数时去重 unordered_set<char> st; for(int j=i;j<s.length();++j){ //以j为起点,遍历后续所有元素 tmp += s[j]; st.insert(s[j]); //如果st中数量 == n,st.size() == n_pre说明加了一个新元素,不重复的元素没变。 if(st.size() == n || st.size() == n_pre){ if(st.size() == n){ n_pre = n; n = a + b; a = b; b = n; res.insert(tmp); }else{ res.insert(tmp); } } } } vector<string> vec; for(auto ch : res){ vec.push_back(ch); } //按照字典序排序 sort(vec.begin(), vec.end()); for(string str : vec){ cout<<str<<endl; } return 0; }