You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 26;
unordered_set<char> S;
bool st[N];
string res;
string s;
void dfs(int u, string t) {
if (u == s.size()) { //走完全程
if (t.size() == S.size()) { //是不是本路径取得的是最小字串的字符串长度比如是不是4
if (res == "") //如果是第一次
res = t; // t就成为大王
else if (t < res) //否则t需要击败现在的大王res
res = t;
}
return;
}
if (!st[s[u] - 'a']) { //如果当前字符没有在本路径中出现过,则可以尝试用上当前的字符
st[s[u] - 'a'] = true; //标识已用过
dfs(u + 1, t + s[u]); //下一个位置
st[s[u] - 'a'] = false; //回溯
}
dfs(u + 1, t); //放弃当前位置
}
int main() {
cin >> s;
for (int i = 0; i < s.size(); i++) S.insert(s[i]); //去重后的最小子串长度为S.size()
dfs(0, "");
cout << res << endl;
return 0;
}