您的位置:首页 > 其它

算法竞赛入门 5.3.2 字母重排

2014-06-27 15:44 92 查看
重点:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: