[NYOJ] [acm-186] Letter Counting

Problem Description

Loading...

Problem Analysis

My brain isn’t in gear today
so I’ll just do something easy…

Use an array to count the letters
then find the index with the highest count

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<string.h>
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int a[26];
        memset(a,0,sizeof(a));
        char s[1020];
        scanf("%s",s);
        for(int i=0; i<strlen(s); i++) {
            a[s[i]-'a']++;
        }
        int maxi=0;
        for(int i=0; i<26; i++) {
            if(a[i]>a[maxi])
                maxi=i;
        }
        printf("%c\n",maxi+'a');
    }
    return 0;
}

The reference solution uses the same idea~

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<string.h>
main() {
    int x,i,max,q;
    char a[1011];
    scanf("%d",&x);
    getchar();
    while(x--) {
        int s[26]= {0};
        gets(a);
        for(i=strlen(a)-1; i>=0; i--)
            s[a[i]-97]++;
        max=0;
        for(i=0; i<26; i++)
            if(max<s[i]) max=s[i],q=i;
        printf("%c\n",q+97);
    }
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...