重點:
1.用vector保存字典單詞;
2.用sort函數對字典單詞排序;
3.匹配每個單詞,遍歷一次字典(可以對字典的單詞和查找單詞按字符排序,或者用map統計每個字符出現的次數間接比較大小)
遺留問題:???
第24行代碼改爲:
for(vector<string>::size_type i = dicLength-1 ; i>=0 ; i--){
程序會發生運行錯誤,覺得代碼沒錯。。。
代碼如下:
#include <iostream>
#include <vector>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
freopen("test.in","r",stdin);
// freopen("test.in",stdin);
vector<string> dic;
string s;
while((cin >> s) && s != "******"){
dic.push_back(s);
}
sort(dic.begin() ,dic.end());
while(cin >> s){
int first = 1;
int dicLength = dic.size();
for(vector<string>::size_type i = 0 ; i<dicLength ; i++){
string temps = dic[i];
// cout << dicLength-1 << " " << i <<" " << temps << endl;
map<char,int> counter1,counter2;
if(temps.length() == s.length()){
for(string::size_type j=0 ; j<temps.length() ; j++){
counter1[temps[j]]++;
counter2[s[j]]++;
}
}
if(counter1 == counter2 && counter1.size()!=0){
if(first == 1){
cout << temps;
first = 0;
}
else{
cout << " " << temps;
}
}
}
if(first == 1){//not find
cout << ":(";
}
cout << endl;
}
return 0;
}