499 - What's The Frequency, Kenneth?

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

Moderator: Board moderators

sunny
Experienced poster
Posts: 124
Joined: Sun Sep 11, 2005 10:22 pm
Location: Civil-BUET

Post by sunny » Thu Oct 12, 2006 2:47 pm

if u print a blank line u will get PE. u dont need 2 print anything if there is no lower/uppercase letter in the input.[/quote]

sklitzz
New poster
Posts: 32
Joined: Fri Dec 03, 2004 5:19 pm

Post by sklitzz » Fri Oct 13, 2006 10:49 am

This is what I coded. And I keep getting WA. What did I forget?

Code: Select all

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;

#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define SQ(x) (x)*(x)

string solve( string s ) {
 	int cnt[52]; memset( cnt, 0, sizeof( cnt ) );
	for( int i = 0; i < s.size(); ++i ) {
		if( s[i] >= 'a' && s[i] <= 'z' ) cnt[ s[i] - 'a' + 26 ]++;
		if( s[i] >= 'A' && s[i] <= 'Z' ) cnt[ s[i] - 'A' ]++;
	}
	
	int x = *(max_element( cnt, cnt + 52 ) );
	if( !x ) return "";	
	
	string ret = "";
	for( int i = 0; i < 26; ++i ) if( cnt[i] == x ) ret += (i + 'A');
	for( int i = 26; i < 52; ++i ) if( cnt[i] == x ) ret += (i + 'a' - 26);
	
	ret += " "; ret += ( x + '0' );
	
	return ret;	
}

int main() {
	string s;
	while( getline( cin, s ) ) {
		string o = solve( s );
		if( o.size() ) cout << o << endl;
	}
		
	return 0;
}

razor_blue
New poster
Posts: 27
Joined: Mon Nov 27, 2006 4:44 am
Location: Indonesia

Post by razor_blue » Wed Nov 29, 2006 4:47 am

Does anyone know why my code is judged WA??
Give me critical I/O, please....

Code: Select all

REMOVED
Sorry, wrong thread....
Last edited by razor_blue on Wed Nov 29, 2006 7:15 am, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Wed Nov 29, 2006 5:13 am

I think you have posted in a wrong thread. The problem number should be 449.
Ami ekhono shopno dekhi...
HomePage

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

499 - What's The Frequency, Kenneth?

Post by Jan » Tue Sep 11, 2007 9:47 pm

I submitted my accepted code and got WA. Can anybody verify this by submitting his/her code?

Thanks..
Ami ekhono shopno dekhi...
HomePage

User avatar
warenix
New poster
Posts: 5
Joined: Sun Aug 12, 2007 1:17 pm

Post by warenix » Wed Sep 12, 2007 3:57 am

Hi Jan,

I've submitted my AC code, system responses "Solved".

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Wed Sep 12, 2007 9:30 am

Well, the problem is that, I got accepted by ignoring blank lines. But the output should be ' 0' as the problem specified. So, the judge input file shouldn't contain blank lines.
Ami ekhono shopno dekhi...
HomePage

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

499-why TLE

Post by sazzadcsedu » Fri Jan 30, 2009 11:20 pm

I GOT TLE.
MY CODE

Code: Select all

  # include<stdio.h>

        int  main()

     {
        char ar[52]={'A','a','B','b','C','c','D','d','E','e','F','f','G','g',
        'H','h','I','i','J','j','K','k','L','l','M','m',
		'N','n','O','o','P','p','Q','q','R','r','S','s','T','t',
		'U','u','V','v','W','w','X','x','Y','y','Z','z'};
        int  count[52];
        char ch;
        int i;
        int max;
        char c;
       

        while(ch=getchar())

		{
          
			for(i=0;i<52;i++)
			{ 

            count[i]=0;
			}

        while(ch!='\n')
      {
         for(i=0;i<52;i++){
         
         if(ch==ar[i])  
         
         count[i]=count[i]+1;
    
      }
         ch=getchar();
   } 
   
   
                max=0;


    for(i=0;i<52;i++)
	 {   
             if(max<count[i])
		{
                     max=count[i];
                      c=ar[i];
	     }
    }

  

   
        for(i=0;i<52;i++)
    { 
               if(count[i]==max)
                  printf("%c",ar[i]);
  }  
       
			   printf(" %d\n",max);
		}

	   return 0;
		}   
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

moiseserg
New poster
Posts: 1
Joined: Wed Jun 17, 2009 8:20 pm

Re: 499-why TLE

Post by moiseserg » Wed Jun 17, 2009 8:38 pm

check your while, compare to EOF

let's say: while((ch=getchar())!=EOF)

you can use a bigger array to avoid several comparations to increase speed

angrad
New poster
Posts: 3
Joined: Tue Aug 17, 2010 9:58 am

499Could you image the input datas to make my program wrong?

Post by angrad » Wed Aug 18, 2010 4:40 am

Here is my code for 494 Kindergarten Counting Game.I'd seen all the posts about the problem 494, but still I got WA after the judge.
Will The judge system give a test single 'a'? If so, how can I output? My answer is '1'.
So everyone who can tell me the key point, Thanx ! :D

Code: Select all

#include <stdio.h>

int main()
{
	char c;
	int count;
	int Is_Alph(char c);
	freopen("in.txt", "r", stdin);
	while((c=getchar())!=EOF)
	{
		count=0;
		while (c=='\n')
		{
			c=getchar();
		}
		while (c!='\n'&&c!=EOF)
		{
			if (Is_Alph(c))
			{
				count++;
			}
		    while((c=getchar())&&Is_Alph(c)&&c!='\n'&&c!=EOF);
		    if (c=='\n')
		    {
    			break;
    		}
			while((c=getchar())&&(!Is_Alph(c))&&c!='\n'&&c!=EOF);
		}
		printf("%d\n", count);
		if (c==EOF)
		{
			return 0;
		}
	}
	return 0;
}

int Is_Alph(char c)
{
	if (c>='A'&&c<='Z'||c>='a'&&c<='z')
	{
		return 1;
	}
	else
	{
		return 0;
	}	
}

rlucca
New poster
Posts: 1
Joined: Mon Mar 19, 2012 5:06 am

Re: 499Could you image the input datas to make my program wr

Post by rlucca » Mon Mar 26, 2012 2:15 pm

Hi people,

Same problem. Can someone help me suggesting input to fix the program?

Thanks

Code: Select all

#include <stdio.h>
#include <math.h>
#include <string>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <algorithm> 
#include <vector>
#include <map>

using namespace std;


int main() {
    char ch;
    int major;
    int reads;
    map<char, int> chs;

    chs.clear();
    major = 0;
    reads = 0;

    while ( (ch = fgetc(stdin)) != EOF ) {
        if (ch == '\n') {
            if (major > 0) {
                map<char,int>::iterator it = chs.begin();
                for (;it != chs.end(); it++) {
                    if (it->second == major)
                        printf("%c", it->first);
                }
                printf(" %d\n", major);
            } else if (major == 0) {
                printf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0\n");
            }

            chs.clear();
            major = 0;
            reads = -1;
        } else if (isalpha(ch) == true) {
            map<char,int>::iterator it = chs.find(ch);
            if (it == chs.end()) {
                chs[ch] = 1;
                if ( 1 > major ) major = 1;
            } else {
                it->second = it->second + 1;
                if ( it->second > major ) major = it->second;
            }
        }
        reads++;
    }

    if (reads > 0) {
            if (major > 0) {
                map<char,int>::iterator it = chs.begin();
                for (;it != chs.end(); it++) {
                    if (it->second == major)
                        printf("%c", it->first);
                }
                printf(" %d\n", major);
            } else if (major == 0) {
                printf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0\n");
            }
    }

    return 0;
}
EDIT: To Accept, I only changed fgetc to scanf.

PromeNabid
New poster
Posts: 21
Joined: Mon Jun 18, 2012 12:52 am
Location: Dhaka, Bangladesh.
Contact:

Re: 499 - What's The Frequency, Kenneth?

Post by PromeNabid » Thu Jun 28, 2012 1:11 pm

Some sample I/Os, this might help.
Sample Input:

Code: Select all

#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:

Code: Select all

di 2
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
aimn 1
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
i 2
dfrs 2
e 4
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
n 8
i 3
d 7
i 6
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
e 14
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
e 55
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
e 7
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
tu 3
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
e 15
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
e 15
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
p 2
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
e 6
al 7
a 3
Hlo 2

waleed.lutfi
New poster
Posts: 5
Joined: Thu Jul 19, 2012 1:02 am

Re: 499-why TLE

Post by waleed.lutfi » Wed Jul 25, 2012 3:07 pm

AC :D

Thanks to Brianfry
Last edited by waleed.lutfi on Wed Mar 20, 2013 4:29 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 499-why TLE

Post by brianfry713 » Wed Jul 25, 2012 11:40 pm

Your code doesn't terminate properly, it gets stuck in an infinite loop.
Check input and AC output for thousands of problems on uDebug!

sophi
New poster
Posts: 6
Joined: Tue Aug 14, 2012 6:00 pm

Re: 499 - What's The Frequency, Kenneth?

Post by sophi » Tue Aug 14, 2012 6:15 pm

But what's the problem with my code.I got WA

Code: Select all

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>


int main()
{
    char str[500];
    int arr[52];
    int index,k,j;
   // freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    while(gets(str))
        {
            int length = strlen(str);
                if(length==0) continue;
            for(k=0;k<=52;k++) arr[k]=0;

            for(j=0;j<length;j++)
                {
                   char ch =str[j];

                   if(ch>=65 && ch <=90)
                    {

                        index =ch-65+26;
                        arr[index]+=1;

                    }

                    if(ch>=97 && ch<=122)
                        {
                            index = ch-97;
                            arr[index]++;
                        }
                }


                 int mi=0;
                 int i;
                 char c;

                for(i=0;i<52;i++)
                {
                    if(arr[mi] < arr[i])
                        {
                           mi=i;
                        }

                }


                        for(i=26;i<52;i++)
                        if(arr[mi]==arr[i])
                            {

                                c=65+i-26;
                                printf("%c",c);


                           }


                            for(i=0;i<26;i++)
                            if(arr[mi]==arr[i])
                            {
                                c=97+i;
                                printf("%c",c);

                            }

                printf(" %d\n",arr[mi]);
           }

    return 0;
}

Post Reply

Return to “Volume 4 (400-499)”