题意
给出单词集合,再给错序单词,如果集合中有则输出,无则说明
思路
将集合中的单词作为索引,再用sort()排好序,作为值。
乱序中的单词也用sort()排序,查找map值中是否有相等的,有就输出索引
代码
#include <iostream>
#include <string>
#include <map>
#include<algorithm>
using namespace std;
int main(int argc, char *argv[])
{
map<string,string> m;
string s,t;
while(cin>>s&&s!="XXXXXX"){
t=s;
sort(s.begin(),s.end());
m[t]=s;
}
while(cin>>s&&s!="XXXXXX"){
int flag=0;
t=s;
sort(s.begin(),s.end());
map<string,string>::iterator it;//遍历查找
for(it=m.begin();it!=m.end();it++){
if(it->second==s){//map的值相等
cout<<it->first<<endl;//输出map的索引值
flag=1;
}
}
if(flag==0){
cout<<"NOT A VALID WORD"<<endl;
}
cout<<"******"<<endl;
}
return 0;
}