`
猫太的鱼
  • 浏览: 239983 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

1002 bad one - Time Limit Exceeded

 
阅读更多
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* It is not used.
 *
char dict[8][3] = {
        {'A', 'B', 'C'},
        {'D', 'E', 'F'},
        {'G', 'H', 'I'},
        {'J', 'K', 'L'},
        {'M', 'N', 'O'},
        {'P', 'R', 'S'},
        {'T', 'U', 'V'},
        {'W', 'X', 'Y'}
};
*/


/* Return:
 * 0: for str1 is before str2.
 * 1: for str2 is before str1.
 */
int compare_sort(char *str1, char *str2)
{
        int i;

        for (i = 0;i < 9;++i) {
                if (str1[i] < str2[i])
                        return 0;
                else if (str2[i] < str1[i])
                        return 1;
                else if (str1[i] == str2[i])
                        continue;
        }

        /* should not go into this subroutine. */
        return -1;
}

/* Return:
 * 0: for str1 is equal to str2.
 * non-0: for not equal.
 */
int compare(char *str1, char *str2)
{
        int i;

        for (i = 0;i < 9;++i) {
                if (str1[i] != str2[i])
                        return 1;
        }

        return 0;
}

/* It is stupid way. ^_^ */
char myitoa(int i)
{
        switch(i) {
        case 1:
                return '1';
        case 2:
                return '2';
        case 3:
                return '3';
        case 4:
                return '4';
        case 5:
                return '5';
        case 6:
                return '6';
        case 7:
                return '7';
        case 8:
                return '8';
        case 9:
                return '9';
        case 0:
                return '0';
        default:
                return '0';
        }
}

/* A two dimension array for storing standard form phone number,
 * Also used for counting duplicates. For instance,
 * (Note, the first one is a pointer to phone number, the second
 *  one is the duplicate times.
 *
 * {&"333-5555", 3},
 * {&"444-6666", 4},
 * etc...
 */
char ***phone = NULL;

void format_to_standard(char *phone_number, int count)
{
        int len = 0, i;
        char *number = NULL;
        int cnt_of_digit = 0;

        len = strlen(phone_number);
        if (len < 7)
                return;

        number = (char*)malloc(9 * sizeof(char));
        if (number == NULL)
                exit(-2);

        for (i = 0;i < len;++i) {
                if (phone_number[i] == '-')
                        continue;

                /* Digit validation check.
                 * It cannot be Q or Z.
                 * And it must be 1~9 or A~Y.
                 */
                if (phone_number[i] < '0' || (phone_number[i] > '9' && phone_number[i] < 'A')
                        || phone_number[i] >= 'Z' || phone_number[i] =='Q')
                        break;

                /* If it is a number. */
                if (phone_number[i] < 'A') {
                        number[cnt_of_digit++] = phone_number[i];
                } else {
                /* It must be a upper character */
                        if (phone_number[i] < 'Q') {
                                number[cnt_of_digit++] = myitoa((phone_number[i] - 'A') / 3 + 2);
                        } else if (phone_number[i] == 'R' || phone_number[i] == 'S') {
                                number[cnt_of_digit++] = '7';
                        } else if (phone_number[i] == 'U' || phone_number[i] == 'V' || phone_number[i] == 'T') {
                                number[cnt_of_digit++] = '8';
                        } else if (phone_number[i] == 'W' || phone_number[i] == 'X' || phone_number[i] == 'Y') {
                                number[cnt_of_digit++] = '9';
                        }
                }

                if (cnt_of_digit == 3) {
                        number[cnt_of_digit++] = '-';
                }
        }
        number[cnt_of_digit] = '\0';

        /* Assign mem of storing phone number to Array */
        phone[count][0] = number;
        phone[count][1] = (char*)1;

        return;
}

void count_duplicate(int count)
{
        int flag_loop = 0;              /* This flag is used in i-loop for determining if dup exists. */
        int flag_all = 0;               /* This flag is used for indicating duplicate, and whether need to print */
        int *index_dup = NULL;
        int i, j, ret = -1;
        int cnt_dup = 0;
        int tmp;

        index_dup = (int*)malloc(count * sizeof(int));
        if (index_dup == NULL)
                exit(-2);

        for (i = 0;i < count;++i) {
                if ((int)phone[i][1] == -1)
                        continue;
                for (j = i + 1;j < count;++j) {
                        ret = compare(phone[i][0],phone[j][0]);
                        if (ret == 0) {
                                phone[i][1]++;
                                phone[j][1] = (char*)-1;
                                flag_loop = 1;
                        }
                }
                if (flag_loop == 1) {
                        index_dup[cnt_dup++] = i;
                        flag_all = 1;
                }

                flag_loop = 0;
        }

        if (flag_all == 0) {
                printf("No duplicates.\n");
        } else {
                for (i = 0;i < cnt_dup;++i) {
                        for (j = i + 1;j < cnt_dup;++j) {
                                ret = compare_sort(phone[index_dup[i]][0],phone[index_dup[j]][0]);
                                if (ret == 1) {
                                        tmp = index_dup[i];
                                        index_dup[i] = index_dup[j];
                                        index_dup[j] = tmp;
                                }
                        }
                }

                /* This is used for printing phone number in ascending order. */
                for (i = 0;i < cnt_dup;++i) {
                        printf("%s %d\n",phone[index_dup[i]][0],(int)phone[index_dup[i]][1]);
                }
        }
}

void free_mem(int len)
{
        int i;

        for (i = 0;i < len;++i) {
                if (phone[i][0] != NULL) {
                        free(phone[i][0]);
                        phone[i][0] = NULL;
                }
                free(phone[i]);
                phone[i] = NULL;
        }
}

int main(void)
{
        char number[10];
        char phone_number[16];
        int count = 0, i;

        scanf("%s", number);
        count = atoi(number);
        if (count <= 0 || count > 100000)
                exit(-1);

        phone = (char***)malloc(count * sizeof(char**));
        if (phone == NULL)
                exit(-2);

        /* Malloc the array of char pointer, and initialize */
        for (i = 0;i < count;++i) {
                phone[i] = (char**)malloc(2 * sizeof(char*));
                if (phone[i] == NULL)
                        exit(-2);

                phone[i][0] = NULL;
                phone[i][1] = NULL;
        }

        for (i = 0;i < count;++i) {
                scanf("%s", phone_number);
                format_to_standard(phone_number,i);
        }

        count_duplicate(count);

        free_mem(count);

        return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics