重点:
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;
}