华为OJ——查找兄弟单词

查找兄弟单词

题目描述

输入描述:

先输入字典中单词的个数,再输入n个单词作为字典单词。
输入一个单词,查找其在字典中兄弟单词的个数
再输入数字n

输出描述:

根据输入,输出查找到的兄弟单词的个数

输入例子:

3

abc

bca

cab

abc

1

输出例子:

2

bca

解答代码

方法一:
#include<iostream>
#include<cstdio>
#include<fstream>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

int judge(string findString,string v)
{
    sort(findString.begin(),findString.end());
    sort(v.begin(),v.end());
    if(findString==v)
        return 1;
    else
        return 0;
}

int main()
{
    int n,i,index;
    vector<string> words;
    vector<string> bother;
    string findWord;
    string temp;
    //freopen("input.txt", "r", stdin);
    while((cin>>n)!=0)
    {
        words.clear();
        bother.clear();
        for(i=0; i<n; i++)
        {
            cin>>temp;
            words.push_back(temp);
        }
        cin>>findWord;
        cin>>index;

        for(i=0; i<n; i++)
        {
            if(findWord!=words[i] && judge(findWord,words[i]))
            {
                bother.push_back(words[i]);
            }
        }
        sort(bother.begin(),bother.end());
        cout<<bother.size()<<endl;
        if(index<bother.size())
            cout<<bother[index-1]<<endl;
    }
    return 0;
}

方法二:
#include<iostream>
#include<cstdio>
#include<fstream>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

int judge(string findString,string v)
{
    sort(findString.begin(),findString.end());
    sort(v.begin(),v.end());
    if(findString==v)
        return 1;
    else
        return 0;
}

int main()
{
    int n,i,index;
    vector<string> words;
    vector<string> bother;
    string findWord;
    string temp;
    //freopen("input.txt", "r", stdin);
    while((cin>>n)!=0)
    {
        words.clear();
        bother.clear();
        int count=0;
        string result;
        for(i=0; i<n; i++)
        {
            cin>>temp;
            words.push_back(temp);
        }
        sort(words.begin(),words.end());

        cin>>findWord;
        cin>>index;

        for(i=0; i<n; i++)
        {
            if(findWord!=words[i] && judge(findWord,words[i]))
            {
                count++;
                if(count==index)
                    result=words[i];
            }
        }
        cout<<count<<endl;
        if(count>=index)
            cout<<result<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值