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

DanDanR
New poster
Posts: 2
Joined: Sat Nov 16, 2013 11:43 pm

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

Post by DanDanR » Sat Nov 16, 2013 11:49 pm

Hey guys,
I've written this code in java, it produces the correct output for the given example input, it also handles an input like "A." correctly. Does anybody know where the problem is with my code?
Somebody has written sth about blank lines, but I didn't really get it... I keep getting wrong answer. WTF?!?
Thanks

Code: Select all

import java.util.*;

public class Main {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		boolean foundFlag = false;
		char c;
		int i, j, maxCount = 0;
		String input;
		
		Scanner sc = new Scanner(System.in);
		ArrayList<Character> charList = new ArrayList<Character>();
		ArrayList<Integer> counterList = new ArrayList<Integer>();
		ArrayList<Boolean> caseList = new ArrayList<Boolean>();
		
		while(sc.hasNextLine()) {
			maxCount = 0;
			foundFlag = false;
			charList.clear();
			counterList.clear();
			caseList.clear();
			
			input = sc.nextLine();
			for(i=0; i<input.length(); i++) {
				c = input.charAt(i);
				if(Character.isLetter(c)) {
					for(j=0; j<charList.size(); j++) {
						if(charList.get(j) == c) {
							counterList.set(j, counterList.get(j)+1);
							if(counterList.get(j) > maxCount) maxCount = counterList.get(j);
							foundFlag = true;
							break;
						}
						else
							foundFlag = false;
					}
					if(!foundFlag) {
						charList.add(c);
						counterList.add(1);
						caseList.add(Character.isUpperCase(c));
						if(maxCount == 0) maxCount = 1;
					}
				}
			}
			for(i=0; i<charList.size(); i++) {
				if((counterList.get(i) == maxCount) && (caseList.get(i) == true))
					System.out.print(charList.get(i).toString());					
			}
			for(i=0; i<charList.size(); i++) {
				if((counterList.get(i) == maxCount) && (caseList.get(i) == false))
					System.out.print(charList.get(i).toString());					
			}
			
			System.out.print(" " + maxCount + "\n");
		}
		sc.close();
		return;
	}
}

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

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

Post by brianfry713 » Mon Nov 18, 2013 10:10 pm

Doesn't match the sample I/O.
The list of letters should be an alphabetical list of upper case letters followed by an alphabetical list of lower case letters.
Check input and AC output for thousands of problems on uDebug!

nguyencaothong
New poster
Posts: 1
Joined: Fri Jun 06, 2014 7:31 am

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

Post by nguyencaothong » Fri Jun 06, 2014 7:34 am

Could you please find me the reason why my code was WA? I'm stuck :(

Code: Select all

#include <stdio.h>
#include <algorithm>

using namespace std;

char s[100000];
int map[256];

int main() {
	int i, max;
	
	while (gets(s)) {
		i = 0;
		fill(map, map + 256 * 2, 0);
		while (s[i] != '\0') {
			map[s[i]] += 1;
			i++;
		}
		max = 0;
		for (i = 'A'; i <= 'Z'; i++) {
			if (map[i] > max) max = map[i];
		}
		for (i = 'a'; i <= 'z'; i++) {
			if (map[i] > max) max = map[i];
		}
		if (max == 0) {
			printf(" 0\n");
			continue;
		}
		for (i = 'A'; i <= 'Z'; i++) {
			if (map[i] == max) printf("%c", i);
		}
		for (i = 'a'; i <= 'z'; i++) {
			if (map[i] == max) printf("%c", i);
		}
		printf(" %d\n", max);
	}
	return(0);
}

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

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

Post by lbv » Fri Jun 06, 2014 8:58 pm

nguyencaothong wrote:Could you please find me the reason why my code was WA? I'm stuck :(
Take the time to read the previous messages of this thread and try the test cases given. For example, try the I/O posted by PromeNabid around Jun 28, 2012.

Post Reply

Return to “Volume 4 (400-499)”