c++ - 字谜程序测试

标签 c++ anagram

我的 Anagram 程序在我的 dev-cpp 中运行良好,但在任何在线测试器中,任何测试 Anagram 都会抛出错误答案。有人可以帮助我吗?

#include<iostream>
#include<cstring>
using namespace std;

int main()
{

    char input1[10000];
    char input2[10000];
    cin >> input1;
    getchar();
    cin >> input2;
    getchar();

    int leng;
    leng = strlen(input1);
    bool output[leng];

    for(int i=0; i<leng; i++){
            for(int y=0; y<leng; y++){
                    if( input1[i] == input2[y] ){
                        output[i] = true;
                    }
            }
    }

    for(int o=0; o<leng; o++ ){
        if( (o+1) == leng){
            if( output[o] == true){
                 cout << "ano" << endl;
                 break;
            }
        }else if(output[o] == true) {
                 continue;
        }
        cout << "nie" << endl;
        break;
    }


    getchar();
    return 0;
}

最佳答案

与其尝试重新发明轮子,不如有一个简洁的函数 is_permutation<algorithm>这可以使这个问题变得微不足道。 .

#include <algorithm>

bool isAnagram(std::string a, std::string b) {
    if(a.size() == b.size()) {
        return std::is_permutation ( a.begin(), a.end(), b.begin(), [](char x, char y){return std::tolower(x) == std::tolower(y);} );
    }
    return false;
}

如果您想要区分大小写,只需删除二进制预测。 Try it here

关于c++ - 字谜程序测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33120501/

相关文章:

c++ - 检索有关预处理器指令的信息

c++ - 在一个字符串中搜索另一个字符串的变位词?

java - 字谜的改进

C++ 适用于小型个人项目(即爱好编程)

c++ - 在opencv中对图像进行 mask

c++ - 如何使用 if 语句检查字符串文字是否是某个单词或短语

java - 使用 Java 8 搜索字谜

java - 查找无序字谜对子串的数量

c - C 中的 Anagram : How do I know if every element of the int array is set to zero?

c++ - RVO 和复制省略是否只能在一个编译单元内工作?