poj 1256
dfs +剪枝
// poj 1256
#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
int flag;
char s1[15], s2[15], A;
bool used[15];
// 排序,要求A<a<B<b<……<z<Z
bool cmp(char a, char b) {
return tolower(a) == tolower(b) ? a < b : tolower(a) < tolower(b);
}
void dfs(int count, int length)
{
if(count == length)
{
s2[length] = '\0';
printf("%s\n", s2);
return;
}
for (int i = 0; i < length; i++)
{
if (flag == count && s1[i] == A )
{
continue;
}
if (!used[i])
{
s2[count] = s1[i];
used[i] = true;
A = s1[i];
flag = count;
dfs(count + 1, length);
flag = count;
A = s1[i];
used[i] = false;
}
}
return;
}
int main()
{
int T;
string str;
scanf("%d", &T);
while (T--)
{
A = '.';
flag = -1;
for (int i = 0; i <= 13; i++)
{
used[i] = false;
}
cin>>str;
sort(str.begin(), str.end(),cmp);
int len = str.length();
for (int i = 0; i < len; i++)
{
s1[i] = str[i];
}
s1[len] = '\0';
dfs(0, len);
}
return 0;
}
poj 1256
最新推荐文章于 2020-03-06 12:26:55 发布