Files
python/TangDou/LanQiaoBei/LanQiao14STEMA202208/3_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

32 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}