版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JackZhang_123/article/details/82226976
直接递归即可
需要注意的地方是set是有序的,所以DFS得到第一个结果one就是最终答案
代码如下:
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
#include <cstdlib>
using namespace std;
string res="";
bool dfs(vector<set<char>>& dict, set<string> all, int index, string one)
{
if (index == dict.size())
{
if (all.find(one) == all.end())
{
res = one;
return true;
}
return false;
}
else
{
for (char i : dict[index])
{
bool tmp = dfs(dict, all, index + 1, one + i);
if (tmp == true)
return true;
}
return false;
}
}
int main()
{
int n, l;
cin >> n >> l;
vector<string> all(n, "");
for (int i = 0; i < n; i++)
cin >> all[i];
vector<set<char>> dict(l, set<char>());
for (int i = 0; i < l; i++)
{
for (string one : all)
dict[i].insert(one[i]);
}
set<string> t(all.begin(), all.end());
dfs(dict, t, 0, "");
if (res.length()<=0)
cout << "-" << endl;
else
cout << res << endl;
}