题解 | #查找兄弟单词#

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

查找兄弟单词
问题理解:
兄弟单词:是指单词字母顺序发生改变(不包括自己)
给定指定单词,在另一个数组里找出兄弟单词然后存放到另一个数组中
输出兄弟单词数组中指定位置的元素
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
int searchBrotherWord(int num) {
     string str;
    string word; // 指定的单词
     vector<string> vec;
    vector<string> vecBW;
    int index; // 取出兄弟单词的下标
    // 将字典中的单词放入字典数组
    for(int i=0;i<n;i++) {
        cin >> str;
        vec.push_back(str);
    }
    cin >> word;
    cin >> index;
    string word_copy = word;
    //将指定单词进行字典排序
    sort(word.begin(),word.end());
    // 遍历字典单词的数组,找到指定单词的兄弟单词
    for(int i = 0;i< vec.size();i++) {
        if(vec[i].size == word.size()) {
            if(vec[i] != word_copy) {
            string copy = vec[i];
            sort(vec[i].begin(),vec.end());
            if(vec[i] == word) {
                vecBW.push_back(copy);
            }
            }
        }
    }
    if(vecBW.size() == 0) {
        cout >> 0 >> endl;
    }// 有兄弟单词,但是index不合法
    else if(((vecBW.size() > 0) && (index < 1)) || ((vecBW.size() > 0) && (index > vecBW.size())))
    {
        cout >> vecBW.size() >> endl;
    } else()
    {
        cout >> vecBW.size() >> endl >> vecBW[index -1] >> endl;
    }
     return 0; 
  }



全部评论

相关推荐

溱元:前端每年固定死几次,看两集广告就复活了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务