Page 7 of 7

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

Posted: Sat Nov 16, 2013 11:49 pm
by DanDanR
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;
	}
}

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

Posted: Mon Nov 18, 2013 10:10 pm
by brianfry713
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.

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

Posted: Fri Jun 06, 2014 7:34 am
by nguyencaothong
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);
}

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

Posted: Fri Jun 06, 2014 8:58 pm
by lbv
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.