POJ1256 全排列函数

一开始想用数字映射字母
超时了
然后直接对字符串操作就可以了
需要写一个字符串的比较函数

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define ll long long
using namespace std;
char s[20];
bool cmp(char a,char b)
{
    int t1,t2;
    if(a>='A'&&a<='Z')
    {t1=(a-'A'+1)*2-1;}
    else
    {t1=(a-'a'+1)*2;}
    
    if(b>='A'&&b<='Z')
    {t2=(b-'A'+1)*2-1;}
    else
    {t2=(b-'a'+1)*2;}
    return t1<t2;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",&s);
        sort(s,s+strlen(s),cmp);
        printf("%s\n",s);
        while(next_permutation(s,s+strlen(s),cmp))
        {
            printf("%s\n",s);
        }
    }
    return 0;
}

发布了25 篇原创文章 · 获赞 4 · 访问量 1059

猜你喜欢

转载自blog.csdn.net/qq_43781431/article/details/104786873