POJ 1256 Anagram

sort与next_permutation函数在字符串中的应用

题目链接POJ 1256

题目大意

给你一个含有大小写英文字母的字符串,按照’A’<’a’<’B’<’b’<…<’Z’<’z’的方式排列,输出所有的排列。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
bool cmp(const char &a, const char &b)
{
if (a <= 'Z' && a >= 'A' && b <= 'Z' && b >= 'A')
{
return a < b;
}
if (a <= 'z' && a >= 'a' && b <= 'z' && b >= 'a')
{
return a < b;
}
if (a <= 'Z' && a >= 'A' && b <= 'z' && b >= 'a')
{
return a + 32 <= b;
}
if (a <= 'z' && a >= 'a' && b <= 'Z' && b >= 'A')
{
return a < (b + 32);
}
}
int main()
{
int num = 0;
cin >> num;
while (num--)
{
char str[100];
cin >> str;
int length = strlen(str);
sort(str, str + length, cmp);
cout << str << endl;
while (next_permutation(str, str + length, cmp))
{
cout << str << endl;
}
}
return 0;
}
坚持原创技术分享,您的支持将鼓励我继续创作!