基本上我有一個問題,有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