You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用字符数组实现全排列遇输出超限,求正确实现方案

问题:无重复字母全排列(仅用字符数组实现)

题目描述

编写程序,根据给定的字母集合生成所有可能的单词。示例:给定单词"abc",程序需遍历三个字母的所有组合,输出"abc"、"acb"、"bac"、"bca"、"cab"和"cba"。输入单词中可能存在重复字母,程序不能生成重复单词,且需按字典序升序输出。

输入

输入包含多个单词。第一行是单词数量,后续每行一个单词。单词由A-Z的大小写字母组成,大小写视为不同,每个单词长度小于13。

输出

对每个输入单词,输出所有由其字母生成的不重复单词,同一输入的生成单词需按字典序升序排列,大写字母优先于对应的小写字母。


使用字符数组且报错“output limit exceeded”的代码

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
bool cmp(char a, char b)
{
    double aa = a;
    double bb = b;
    double d = 'a' - 'A' - 0.5;
    if (aa >= 'a')
    {
        aa = a - d;
    }
    if (bb >= 'a')
    {
        bb = b - d;
    }
    return aa < bb;
}
void swap(char *s, int a, int b)
{
    int t = s[a];
    s[a] = s[b];
    s[b] = t;
}
void permutation(char *s, int m, int n)
{
    if (m == n)
    {
        printf("%s\n", s);
    }
    else
    {
        char t[100];
        for (int i = m; i < n + 1; i++)
        {
            if (i == m)
            {
                for (int j = 0; j < n + 1; j++)
                {
                    t[j] = s[j];
                }
            }
//prevent dumplication
            if (i != m && s[i] == s[m])
            {
                continue;
            }

            swap(s, i, m);
            permutation(s, m + 1, n);
            if (i == n)
            {
                for (int j = 0; j < n + 1; j++)
                {
                    s[j] = t[j];
                }
            }
        }
    }
}
int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    while (n--)
    {
        char s[100];
        cin >> s;
        int lenth = strlen(s);
        sort(s, s + lenth, cmp);
        permutation(s, 0, lenth - 1);
    }

    return 0;
}

使用string且通过的代码

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
void swap(string &s, int a, int b)
{
    char t = s[a];
    s[a] = s[b];
    s[b] = t;
}
bool cmp(char a, char b)
{
    double aa = a;
    double bb = b;
    double d = 'a' - 'A' - 0.5;
    if (aa >= 'a')
    {
        aa = a - d;
    }
    if (bb >= 'a')
    {
        bb = b - d;
    }
    return aa < bb;
}
void permutation(string s, int m, int n)
{
    if (m == n)
    {
        cout << s;
        cout << endl;
    }
    else
    {
        for (int i = m; i <= n; i++)
        {
            if (i != m && s[i] == s[m])
            {
                continue;
            }
            swap(s, i, m);
            permutation(s, m + 1, n);
        }
    }
}
int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        string str;
        cin >> str;
        sort(str.begin(), str.end(), cmp);
        permutation(str, 0, str.size() - 1);
    }
    return 0;
}

问题诉求

尝试将字符数组转为string输出,或用string接收输入再转为字符数组处理,均出现“output limit exceeded”错误;尝试将字符数组大小设为10000后,又出现Wrong Answer。希望仅使用字符数组解决该问题。


内容的提问来源于stack exchange,提问作者Meet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 20:12:04