使用map,将字典中的每个单词和其自身排序后的单词一一对应。
将要搜索的单词自身排序后,和字典中的单词一一比较,如果两者排序后相等,则输出字典中的原单词。
#include #include #include #include using namespace std; int main() { map dic; //第一个string为原单词,第二个string为排序后的单词 string s1, s2; //原单词,排序后的单词 while (cin >> s1) { if (s1 == "XXXXXX") break; s2 = s1; sort(s2.begin(), s2.end()); //将单词各字母按字典序排序 dic[s1] = s2; //建立map对应关系 } string word; //乱序单词 while (cin >> word) { if (word == "XXXXXX") break; bool flag = false; //乱序单词在字典中有无对应的单词 sort(word.begin(), word.end()); map::iterator it; //map迭代器,用于遍历map for (it = dic.begin(); it != dic.end(); it++) //遍历整个字典 { if (word == it->second) //比较乱序单词排序后是否和字典中的单词相同 { cout <first << endl; //若相同,则输出排序前的字典单词 flag = true; } } if (flag == false) { cout << "NOT A VALID WORD" << endl; } cout << "******" << endl; } return 0; } 继续加油。 作者:Intelligence1028 oj C++ c+
#include #include #include #include using namespace std; int main() { map dic; //第一个string为原单词,第二个string为排序后的单词 string s1, s2; //原单词,排序后的单词 while (cin >> s1) { if (s1 == "XXXXXX") break; s2 = s1; sort(s2.begin(), s2.end()); //将单词各字母按字典序排序 dic[s1] = s2; //建立map对应关系 } string word; //乱序单词 while (cin >> word) { if (word == "XXXXXX") break; bool flag = false; //乱序单词在字典中有无对应的单词 sort(word.begin(), word.end()); map::iterator it; //map迭代器,用于遍历map for (it = dic.begin(); it != dic.end(); it++) //遍历整个字典 { if (word == it->second) //比较乱序单词排序后是否和字典中的单词相同 { cout <first << endl; //若相同,则输出排序前的字典单词 flag = true; } } if (flag == false) { cout << "NOT A VALID WORD" << endl; } cout << "******" << endl; } return 0; }
继续加油。
oj C++ c+
帮助他人,成就自己。
人生最大成功就是伸出热情而温暖的双手,尽自己所能去帮助身边的每一个人,只要无私的奉献,就会收获到美好的生活。
1024问感谢每一位朋友的帮助和支持。