【发布时间】:2013-10-26 08:11:14
【问题描述】:
基本上我有 2 个子问题。 第一个问题是:给定 2 个字符串,确定它们是否是字谜。 二是有点难。您有 N 个字符串,必须确定它们是否是彼此的字谜。
我已经解决了第一个问题,我将在下面编写代码,但对于第二个我不知道。我在想有可能通过从字符串数组中读取 N 个字符串来以某种方式做到这一点,然后使用 for 序列来读取每个字符串并进行比较,但我不知道该怎么做。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string word1; string word2;
getline(cin,word1);
getline(cin,word2);
if(word1.length()==word2.length()){
sort(word1.begin(), word1.end());
sort(word2.begin(), word2.end());
if(word1==word2) cout<<"The words are anagrams of each other"<<endl;
else cout<<"The words are not anagrams of each other"<<endl;
}
else cout<<"The words are not the same length"<<endl;
return 0;
}
【问题讨论】:
-
如果
string1是string2和string3的字谜,string2和string3肯定是彼此的字谜 -
哦,是的。对此感到抱歉。我迷失了所有的东西。即使是这样。我不知道如何读取字符串数组。我搜索的所有内容都使用指针,我还没有学习。
-
@DragoşPaulMarinescu 对要检查的字符串进行排序效率极低,没有必要检查我的答案