KyrieSu / Code_cpp

0 stars 0 forks source link

003. What's The Frequency, Kenneth? #3

Open KyrieSu opened 8 years ago

KyrieSu commented 8 years ago

#include <stdio.h>

main()
{
  int i;
  char *suffix[]= { "st", "nd", "rd" };
  char *item[]= { "Unix" , "cat", "sed", "awk", "grep", "ed", "vi"};

  printf("In the beginning, there was nothing.\n");
  for (i= 0; i < 7; i++)
    printf("And on the %d%s day, God created %s. And it was good.\n",
           i + 1, (i < 3) ? suffix[i] : "th", item[i]);
}

But then God saw that vi led people into temptation. Instead of choosing the righteous ways of make, dbx, and RCS, people used long command lines, printf(), and tape backups.

So God decreed, ``I see that Engineers have thus defiled my vi. And so, I shall create emacs, an editor more powerful than words. Further, for each instantiation vi hitherto, the Engineer responsible shalt perform Penance. And lo, the Penance wilt be painful; there will be much wailing and gnushingof teeth. The Engineer will read many lines of text. For each line of text, the Engineer must tell me which letters occur the most frequently.''

``I charge you all with My Golden Rule: 'Friends shalt not let friends use vi'.''

Input and Output

Each line of output should contain a list of letters that all occured with the highest frequency in the corresponding input line, followed by the frequency.

The list of letters should be an alphabetical list of upper case letters followed by an alphabetical list of lower case letters.

Sample Input

When riding your bicycle backwards down a one-way street, if the wheel falls of a canoe, how many ball bearings does it take to fill up a water buffalo? Hello Howard.

Sample Output

e 6 al 7 a 3 Hlo 2

LarryLuTW commented 8 years ago

剛剛看了一下 感覺不難

LarryLuTW commented 8 years ago

剛剛仔細看了 code 還滿不錯的 感覺 STL 用得很熟XD 有幾個小地方可以再改一下


這邊取了兩次 ptr->second 雖然是常數時間 不過建議可以取一次 cache 起來 會省一點時間

for(ptr=m.begin();ptr!=m.end();ptr++)
        if(ptr->second > max)
                max = ptr->second;

這邊也是 可以先把 *ptr cache 起來

for(ptr=m.begin();ptr!=m.end();ptr++)
        if(ptr->second==max)
                sol+=ptr->first;
KyrieSu commented 8 years ago

OK 已修改