题解 | #查找兄弟单词#

查找兄弟单词

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

#include <stdio.h>

/* 
比较是否是兄弟单词
成功返回:1
失败返回:0
*/
static int isBrother(char *base, char *str)
{
    int count[26] = {0};
    if(strlen(base) != strlen(str) || strcmp(base, str) == 0)
    {
        //长度不同 或 字符串相同都不是兄弟单词
        return 0;
    }
    else
    {
        for(int i = 0; i < strlen(base); i++)
        {
            count[base[i] - 'a']++;
            count[str[i] - 'a']--;
        }
        for(int i = 0; i < 26; i++)
        {
            if(count[i] != 0)    //非全0,说明字符数量不同
            {
                return 0;
            }
        }
        return 1;
    }
}

int main()
{
    int n = 0;
    if(scanf("%d", &n) != EOF)
    {
        char str[n][12];
        char sort[n][12];
        char base[12] = {0};
        int k = 0;
        memset(str, 0, n*12*sizeof(char));
        memset(sort, 0, n*12*sizeof(char));
        char need_str[n];
        memset(need_str, 0, n*sizeof(char));
        for(int i = 0; i < n; i++)
        {
            scanf("%s", str[i]);
            //printf("str[%d] = [%s]\n", i, str[i]);
        }
        scanf("%s", base);
        //printf("base = [%s]\n", base);
        scanf("%d", &k);
        //printf("k = [%d]\n", k);
        int index = 0;
        for(int i = 0; i < n; i++)
        {
            if(isBrother(base, str[i]))
            {
                strcpy(sort[index], str[i]);
                index++;
            }
        }
        printf("%d\n", index);

        char temp[12] = {0};
        int min = 0;
        for(int i = 0; i < index; i++)
        {
            min = i;
            for(int j = i+1; j < index; j++)
            {
                if(strcmp(sort[min], sort[j]) > 0)
                {
                    min = j;
                }
            }
            if(min != i)
            {
                strcpy(temp, sort[min]);
                strcpy(sort[min], sort[i]);
                strcpy(sort[i], temp);
            }
        }
        
        printf("%s\n", sort[k-1]);
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 14:10
啊啊啊啊好幸福,妈妈是我找工作发疯前的一束光
黑皮白袜臭脚体育生:看了这篇帖子之后已经第一百次质问老妈,仍然没有得到我的老妈是老板的回答
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 10:39
一个证都没&nbsp;我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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