题解 | #查找兄弟单词#

查找兄弟单词

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

int cmp(int *a,int*b)//判断字母数量是否相同
{
    for(int i=0;i<26;i++)
    {
        if(a[i]!=b[i])
            return 1;
    }
    return 0;
}

int main()
{
    char arr[1000][10]={0};
    int n=0;
    int count=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%s",&arr[i]);
    char word[10]={0};
    scanf("%s",word);
    int k=0;
    scanf("%d",&k);
    int len_word=strlen(word);
    int hashmap_word[26]={0};
    int hashmap[26]={0};
    for(int i=0;i<len_word;i++)
    {
        hashmap_word[word[i]-'a']++;
    }
    for(int i=0;i<n;i++)
    {
        int len=strlen(arr[i]);
        if( len==len_word && strcmp(word,arr[i])!=0)
        {
            for(int j=0;j<len;j++)
            {
                hashmap[arr[i][j]-'a']++;
            }
        }

        
        if(cmp(hashmap,hashmap_word))
            arr[i][0]='~';//只要比z大就行,目的是让字符串排到兄弟单词的后面
        else
            count++;
        memset(hashmap,0,sizeof(int)*26);
    }
    
    for(int i=0;i<n;i++)//排序,交换
    {
        for(int j=0;j<n-i-1;j++)
        {
            if(strcmp(arr[j],arr[j+1])>0)
            {
                char tmp[10]={0};
                memcpy(tmp, arr[j], 10);
                memcpy(arr[j], arr[j+1], 10);
                memcpy(arr[j+1], tmp, 10);
            }
        }
    }
    
    printf("%d\n",count);
    if(k<count+1)
    printf("%s",arr[k-1]);
    return 0;
}

全部评论

相关推荐

07-10 14:08
已编辑
江西农业大学 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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