Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

新人泪奔求助,求教育~~

Posted by lonely_fresh_meat at 2011-06-17 23:26:02
刚注册滴帐号,试着做了P1002,测例输出正确,系统WRONG ERROR,不知道有啥边界值没考虑到还是输出有问题?求教育啊,下面贴出代码,哪位大虾看出问题来了,麻烦EMAIL我一下,不胜感激:hdchild@163.com
/**C代码*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>

int map[128] = {-1}; 

typedef struct node_struct node_t;
struct node_struct{
    char    str[9];
    int     num;
};

node_t**     node_slot;

node_t*     node_cache = NULL;
int         node_cnt = 0;

void
init_map()
{
    int     i;
    
    for (i = 0; i < 100; i++)
        map[i] = -1;

    for (i = '0'; i <= '9'; i++)
        map[i] = i;

    for (i = 'A'; i <= 'Z'; i++)
        map[i] = (i - 'A')/3 + 50;

    map['P'] = map['R'] = map['S'] = '7';
    map['T'] = map['U'] = map['V'] = '8';
    map['W'] = map['X'] = map['Y'] = '9';

    map['Q'] = map['Z'] = -1;

    for (i = 'a'; i <= 'z'; i++)
        map[i] = (i - 'a')/3 + 50;
    
    map['p'] = map['r'] = map['s'] = '7';
    map['t'] = map['u'] = map['v'] = '8';
    map['w'] = map['x'] = map['y'] = '9';
    
    map['q'] = map['z'] = -1;
}

void
format_str(
    char*   s
)
{
    int     a;
    char    local[9] = {0};
    int     i = 0;
    char*   start = s;

    while(*s)
    {
        a = map[(int)(*s)];
        if (a != -1)
        {
           local[i++] = a;
           if(i == 3)
               local[i++] = '-';
        }
        s++;
    }
    
    strcpy(start, local);
}

int
find_num_pos(
    char*   s,
    int*    cmp
)
{
    int min, max, mid;
    int cmp_ret = -1;

    if (node_cnt ==0)
        return 0;
    
    min = 0;
    max = node_cnt -1;
  
    while (min <= max)
    {
        mid = (min + max) /2;
        cmp_ret = strcmp(s, node_slot[mid]->str);
        if(cmp_ret == 0)
            break;
        if(cmp_ret > 0)
        {
            min = mid + 1;
        }
        else
        {
            max = mid - 1;
        }
    }

    *cmp = cmp_ret;
    return mid;
}

void
deal_num(
    char*   s    
)
{
    int     i;
    int     pos, cmp_ret;

    pos = find_num_pos(s, &cmp_ret);
    
    if (node_slot[pos] == NULL || 
        (cmp_ret != 0))
    {
        strcpy(node_cache[node_cnt].str, s);
        node_cache[node_cnt].num = 1;

        if (node_slot[pos] && cmp_ret > 0)
            pos++;
        
        for (i = node_cnt; i > pos; i--)
        {
            node_slot[i] = node_slot[i-1];
        }
        node_slot[pos] = &node_cache[node_cnt];
        node_cnt++;
    }
    else
    {
        node_slot[pos]->num++;
    }
}


void
print_test()
{

    int i = 0;
    node_t*  node; 
   
   for (i = 0; i < node_cnt; i++)
    {
        node = node_slot[i];
        printf("%s %d\n", node->str, node->num);
    }
}

void
print_result(        
)
{
    int     i = 0;
    node_t* node;
    int     has_dup = 0;

    for (i = 0; i < node_cnt; i++)
    {
        node = node_slot[i];
        if (node->num > 1)
        {
            has_dup = 1;
            printf("%s %d\n", node->str, node->num);
        }
    }

    if (!has_dup)
    {
        printf("No duplicates\n");
    }
}

void
init(int n)
{
    int     i;
    init_map();
    
    for (i = 0; i < n; i++)
    {
        node_slot[i] = NULL;
    }
}

int main(void)
{
    int     n;
    char    str[20];

    scanf("%d", &n);

    node_cache  = malloc(n * sizeof(node_t));
    node_slot   = malloc(n * sizeof(node_t*));
   
    init(n);

    while (n--)
    {
        scanf("%s", str);
        format_str(str);
        deal_num(str);
    }

    print_result();

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator