10420 - List of Conquests

All about problems in Volume 104. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

jddantes
Learning poster
Posts: 73
Joined: Sat Mar 08, 2014 8:55 am

Re: 10420 - List of Conquests

Post by jddantes »

Hello,

This is already accepted, though I've a problem optimizing it.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct country
{
    char name[80];
    int count;
};

void update(struct country arr[], char country[]);
void print(struct country arr[]);
void initialize(struct country arr[]);
int compar(const void * p1, const void * p2);


int main()
{
    struct country arr[2000] = {};
    initialize(arr);


    char str[80];
    fgets(str,80,stdin);
    int n;
    sscanf(str,"%d",&n);
    int i;
    for(i=0; i<n; i++)
    {
        fgets(str,80,stdin);
        char country[80];
        sscanf(str,"%s",country);
        update(arr,country);
    }

    //print(arr);

    qsort(arr,2000,sizeof(struct country),compar);
    //printf("blablalbalab\n");
    print(arr);


    return 0;
}

void update(struct country arr[], char country[])
{
    int i;
    for(i=0; i<2000; i++)
    {
        if(!arr[i].count)
        {
            arr[i].count = 1;
            strcpy(arr[i].name,country);
            break;
        }

        if(!strcmp(arr[i].name, country))
        {
            arr[i].count++;
            break;
        }
    }
}

void print(struct country arr[])
{
    int i;
    for(i=0;i<2000;i++)
    {
        if(!(arr[i].count))
            continue;


        printf("%s %d\n",arr[i].name,arr[i].count);
    }

}

void initialize(struct country arr[])
{
    int i;
    for(i=0;i<2000;i++)
    {
        strcpy(arr[i].name,"");
        arr[i].count = 0;
    }
}

int compar(const void * p1, const void *p2)
{
    //printf("%d\n",((struct country *)p1)->count);
    //printf("%d\n",(*((struct country *)p1)).count);
    /*
    if((*((struct country *)p1)).count == 0)
        return 1;

    if((*((struct country *)p2)).count == 0)
        return 1;
    */
    return strcmp((*((struct country *)p1)).name, (*((struct country *)p2)).name);
}
Initially I wanted print to stop when it encounters zero (i.e. struct count = 0), but when I uncomment that block in compar (the return 1 conditions, which I put there to put the empty structs toward the end) qsort doesn't behave as expected.
It goes like:
0
0
0
somecountry
0
0
0
anothercountry
0
0

whereas I want it to be
somecountry
anothercountry
0
0
0
...
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10420 - List of Conquests

Post by brianfry713 »

Check input and AC output for thousands of problems on uDebug!
okokkook
New poster
Posts: 4
Joined: Wed Apr 30, 2014 1:55 pm

10420 List of Conquset WA

Post by okokkook »

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
    char name[100];
    int sum;
}country;
int main(){
    int n;
    scanf("%d",&n);
    country c[3000];
    int i,j,cnt=0,flag,tp;
    char s[100],tmp[100];
    for(i=0;i<3000;i++){
        c[i].sum=0;
    }
    for(i=0;i<n;i++){
        flag=1;
        scanf("%s%*s%*s",s);
            for(j=0;j<cnt && cnt!=0;j++){
                if(strcmp(s,c[j].name)==0){
                    c[j].sum++;
                    flag=0;
                    break;
                }
            }
                if(flag==1){
                    strcpy(c[cnt].name,s);
                    c[cnt].sum++;
                    cnt++;
                }

    }
        for(i=1;i<cnt;i++){
            for(j=0;j<cnt-i;j++){
                if(strcmp(c[j].name,c[j+1].name)>0){
                    strcpy(tmp,c[j].name);

                    strcpy(c[j].name,c[j+1].name);

                    strcpy(c[j+1].name,tmp);

                    tp=c[j].sum;
                    c[j].sum=c[j+1].sum;
                    c[j+1].sum=tp;
                }
            }
        }
        for(i=0;i<cnt;i++){
            printf("%s %d\n",c[i].name,c[i].sum);
        }
    return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10420 List of Conquset WA

Post by brianfry713 »

Don't assume the name of a woman is always two words.
Input:

Code: Select all

2
A b c d e
B f g h
AC output:

Code: Select all

A 1
B 1
Check input and AC output for thousands of problems on uDebug!
sampad74
New poster
Posts: 29
Joined: Wed Jun 18, 2014 3:57 pm
Location: Bangladesh

Re: 10420 - List of Conquests

Post by sampad74 »

Got AC.thanks brian.
Last edited by sampad74 on Sat Jan 31, 2015 5:34 am, edited 1 time in total.
sampad74
New poster
Posts: 29
Joined: Wed Jun 18, 2014 3:57 pm
Location: Bangladesh

Re: 10420 - List of Conquests

Post by sampad74 »

Got AC.Thanks brian.
Last edited by sampad74 on Sat Jan 31, 2015 5:34 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10420 - List of Conquests

Post by brianfry713 »

If a line has 75 chars then your array should be at least size 76.
Check input and AC output for thousands of problems on uDebug!
sampad74
New poster
Posts: 29
Joined: Wed Jun 18, 2014 3:57 pm
Location: Bangladesh

Re: 10420 - List of Conquests

Post by sampad74 »

Got AC.Thanks brian.
Last edited by sampad74 on Sat Jan 31, 2015 5:35 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10420 - List of Conquests

Post by brianfry713 »

Insert on line 28: s[kl] = '\0';
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 104 (10400-10499)”