499 - What's The Frequency, Kenneth?
Moderator: Board moderators
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;
}
-
- New poster
- Posts: 27
- Joined: Mon Nov 27, 2006 4:44 am
- Location: Indonesia
Does anyone know why my code is judged WA??
Give me critical I/O, please....
Sorry, wrong thread....
Give me critical I/O, please....
Code: Select all
REMOVED
Last edited by razor_blue on Wed Nov 29, 2006 7:15 am, edited 1 time in total.
I think you have posted in a wrong thread. The problem number should be 449.
Ami ekhono shopno dekhi...
HomePage
HomePage
499 - What's The Frequency, Kenneth?
I submitted my accepted code and got WA. Can anybody verify this by submitting his/her code?
Thanks..
Thanks..
Ami ekhono shopno dekhi...
HomePage
HomePage
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
HomePage
-
- Experienced poster
- Posts: 136
- Joined: Sat Nov 29, 2008 8:01 am
- Location: narayangong,bangladesh.
- Contact:
499-why TLE
I GOT TLE.
MY CODE
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
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
Re: 499-why TLE
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
let's say: while((ch=getchar())!=EOF)
you can use a bigger array to avoid several comparations to increase speed
499Could you image the input datas to make my program wrong?
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 !
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 !

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;
}
}
Re: 499Could you image the input datas to make my program wr
Hi people,
Same problem. Can someone help me suggesting input to fix the program?
Thanks
EDIT: To Accept, I only changed fgetc to scanf.
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;
}
-
- New poster
- Posts: 21
- Joined: Mon Jun 18, 2012 12:52 am
- Location: Dhaka, Bangladesh.
- Contact:
Re: 499 - What's The Frequency, Kenneth?
Some sample I/Os, this might help.
Sample Input:
Sample Output:
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.
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
-
- New poster
- Posts: 5
- Joined: Thu Jul 19, 2012 1:02 am
Re: 499-why TLE
AC 
Thanks to Brianfry

Thanks to Brianfry
Last edited by waleed.lutfi on Wed Mar 20, 2013 4:29 pm, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 499-why TLE
Your code doesn't terminate properly, it gets stuck in an infinite loop.
Check input and AC output for thousands of problems on uDebug!
Re: 499 - What's The Frequency, Kenneth?
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;
}