我的 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/