题解 | #查找兄弟单词#

查找兄弟单词

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

笨办法

#include <string.h>
#include <ctype.h>

void fun(char *str, int len)
{
    char strtmp;
    for (int i = 0; i < len; ++i)
    {
        for (int j = i + 1; j < len; ++j)
        {
            if (str[i] > str[j])
            {
                strtmp = str[j];
                str[j] = str[i];
                str[i] = strtmp;
            } else if (str[i] == str[j])
                continue;
        }
    }
}

int main(void)
{
    int n;
    scanf("%d", &n);

    char strr[n][10];
    memset(strr, '\0', sizeof(char) * n * 10);
    char (*sp)[10];
    sp = strr;

    for (int i = 0; i < n; ++i)
    {
        scanf("%s", *(sp + i));
    }

    char str1[10] = {'\0'};
    scanf("%s", str1);
    int len1 = (int) strlen(str1);

    char str2[len1];
    memcpy(str2, str1, sizeof(char) * len1);
    fun(str2, len1);
    int k;
    scanf("%d", &k);

    char tmp[n][len1];
    char strrcp[len1];
    int flag1 = 0, flag2 = 0, h = 0;
    memset(tmp, '\0', sizeof(char) * n * len1);

    for (int i = 0; i < n; ++i)
    {
        if (strlen(strr[i]) != len1)
            continue;
        memcpy(strrcp, sp + i, sizeof(char) * len1);
        fun(strrcp, len1);
        for (int j = 0; j < len1; ++j)
        {
            if (sp[i][j] == str1[j])
            {
                flag1++;
            }
            if (strrcp[j] == str2[j])
            {
                flag2++;
            }
        }
        if (flag1 != len1 && flag2 == len1)
        {
            memcpy(tmp[h], sp + i, sizeof(char) * len1);
            h++;
        }
        flag2 = flag1 = 0;
    }

    char temp[len1];
    memset(temp, '\0', sizeof(char) * len1);

    for (int i = 0; i < h; ++i)
    {
        if (tmp[i][0] == '\0')
            break;

        for (int j = i + 1; j < h; ++j)
        {
            if (tmp[j][0] == '\0')
                break;


            for (int b = 0; b < len1; ++b)
            {
                if (tmp[i][b] < tmp[j][b])
                {
                    break;
                } else if (tmp[i][b] == tmp[j][b])
                {
                    continue;
                } else
                {
                    memcpy(temp, &tmp[j][0], sizeof(char) * len1);
                    memcpy(&tmp[j][0], &tmp[i][0], sizeof(char) * len1);
                    memcpy(&tmp[i][0], temp, sizeof(char) * len1);
                    break;
                }
            }
        }
    }
    printf("%d\n", h);
    //printf("%s\n", tmp[k - 1]);
    for (int i = 0; i < len1; ++i)
    {
        printf("%c", tmp[k - 1][i]);
    }
    //printf("\n");
    return 0;
}
全部评论

相关推荐

11-15 17:19
湖南大学 Java
成果成果成果果:这是哪个公司的hr,这么离谱吗,我没见过用性别卡技术岗的,身边女性同学拿大厂offer的比比皆是
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务