基本上我有一个问题,有2个子问题。 第一个问题是:给定2个字符串,确定它们是否是anagrams。 其次是有点困难。你有N个字符串,并且必须确定它们是否是对方的字典。查找N个字符串是否为对方的字典
我解决了第一个问题,我会写下面的代码,但对于第二个问题我不知道。我认为可以通过从字符串数组中读取N个字符串来做到这一点,然后使用序列来读取每个字符串并对其进行比较,但我不知道如何完成。
#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'绝对是彼此的昵称 – Kunal
哦,是的。对不起。我迷失在所有的东西里。即使是这样。我无法弄清楚如何读取字符串数组。我搜索的所有东西都使用了指针,但我还没有学习。 –
@DragoşPaulMarinescu排序字符串检查是非常低效的,没有必要检查我的答案 – aaronman