定义一个单词的“兄弟单词”为:交换该单词字母顺序(注:可以交换任意次),而不添加、删除、修改原有的字母就能生成的单词。
兄弟单词要求和原来的单词不同。例如: ab 和 ba 是兄弟单词。 ab 和 ab 则不是兄弟单词。
现在给定你 n 个单词,另外再给你一个单词 str ,让你寻找 str 的兄弟单词里,按字典序排列后的第 k 个单词是什么?
注意:字典中可能有重复单词。本题含有多组输入数据。
查找兄弟单词 查找兄弟单词
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注 //调试了很久,通过50%,因为忽视了一个关键点,字典元素一定要排序 #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; bool isBrother(string str, string s){ if(str.size() == s.size()){ if(str == s) return false; sort(str.begin(), str.end()); sort(s.begin(), s.end()); if(str == s) return true; } return false; } int main(){ int num; while(cin >> num){ string str; string word,s; int index; vector<string> vs; for(int i = 0; i < num; ++i){ cin >> str; vs.push_back(str); } sort(vs.begin(), vs.end()); // 因为是字典,一定要排序!! cin >> word; cin >> index; int counts = 0; for(int i = 0; i < num; ++i){ if(isBrother(word, vs[i])){ counts ++; if(counts == index) s = vs[i]; } } if(!vs.empty()) cout << counts << endl; if(counts >= index) cout << s << endl; } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 微带串馈天线阵列每个阵元宽度计算
- ¥15 keil的map文件中Image component sizes各项意思
- ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
- ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
- ¥15 划分vlan后,链路不通了?
- ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
- ¥15 Vue3 大型图片数据拖动排序
- ¥15 Centos / PETGEM
- ¥15 划分vlan后不通了
- ¥20 用雷电模拟器安装百达屋apk一直闪退