10062 - Tell me the frequencies!

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

Moderator: Board moderators

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

Re: 10062 - Tell Me the Frequencies!

Post by brianfry713 » Sat Jun 30, 2012 12:21 am

Don't print a blank line at the end of the output.
Check input and AC output for thousands of problems on uDebug!

sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

Re: 10062 - Tell Me the Frequencies!

Post by sith » Sat Jun 30, 2012 9:54 am

My solution doesn't print last line!

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

Re: 10062 - Tell Me the Frequencies!

Post by brianfry713 » Mon Jul 02, 2012 9:51 pm

http://ideone.com/uh8sz

For the sample input, your output is:

Code: Select all

67 1
66 2
65 3

49 1
50 2
51 3

AC output is:

Code: Select all

67 1
66 2
65 3

49 1
50 2
51 3
Check input and AC output for thousands of problems on uDebug!

sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

Re: 10062 - Tell Me the Frequencies!

Post by sith » Wed Jul 04, 2012 11:29 am

There is no extra line in my IDE(IntellijIDEa) for this solution, but I still get WA

Code: Select all

import java.io.*;
import java.util.*;

class Main {


    private static final char NEW_LINE = '\n';

    public static void main(String[] args) {


        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

        int intchr;
        try {
            Map<Integer, Entry> map = new HashMap<Integer, Entry>();
            while ((intchr = reader.read()) != -1) {
                if (intchr == NEW_LINE || intchr == '\r') {
                    if (map.isEmpty()) {
                        continue;
                    }
                    printResult(writer, map, true);
                    writer.write("\n");
                    map = new HashMap<Integer, Entry>();
                    continue;

                }

                Entry integer = map.get(intchr);
                if (integer == null) {
                    integer = new Entry(intchr);
                    map.put(intchr, integer);
                }
                integer.frequency++;
            }
            printResult(writer, map, false);
            writer.flush();
        } catch (IOException e) {

        }
    }

    private static void printResult(BufferedWriter writer, Map<Integer, Entry> map, boolean printLastN) throws IOException {
        Entry[] values = map.values().toArray(new Entry[map.size()]);

        Arrays.sort(values, new Comparator<Entry>() {
            public int compare(Entry o1, Entry o2) {
                int compare = o1.compareTo(o2);
                if (compare == 0) {
                    return o2.value - o1.value;
                }
                return compare;
            }
        });

        for (int i = 0; i < values.length; i++) {
            Entry value = values[i];
            writer.write(String.valueOf(value.value));
            writer.write(" ");
            writer.write(String.valueOf(value.frequency));

            if (i + 1 == values.length && !printLastN) {
                continue;
            }
            writer.write("\n");
        }

    }

    static class Entry implements Comparable<Entry> {
        int frequency;
        final int value;

        Entry(int value) {
            this.value = value;
        }


        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Entry entry = (Entry) o;

            if (value != entry.value) return false;

            return true;
        }

        @Override
        public int hashCode() {
            return value;
        }

        public int compareTo(Entry o) {
            return frequency - o.frequency;
        }
    }

}

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

Re: 10062 - Tell Me the Frequencies!

Post by brianfry713 » Thu Jul 05, 2012 7:08 am

Try putting a newline at the end of the last input line.
Check input and AC output for thousands of problems on uDebug!

efrshuvo
New poster
Posts: 4
Joined: Thu Nov 08, 2012 9:25 am

Re: 10062 - Tell Me the Frequencies!

Post by efrshuvo » Thu Nov 08, 2012 9:35 am

HI, I have tried all the test case every time my code passed all the test case i found here also the all other i can make including the sample input . Every time My code generate the correct answer and I also take care about the blank line between every output sets and also not to print a blank line for the last output set.

But am getting WA every time I submitted the code. I am now undone i can not found my mistake . If any body can help me to find my mistake then this will be very much appreciable. My Code is as bellow:

Code: Select all


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

#define MAXLEN 1001

#define ASCIISTART 32
#define ASCIIEND 128


int frequency_table[ASCIIEND+1];

int output_table[101];	

void init_frequensy()
{
	int i=0;
	for(i=ASCIISTART;i<ASCIIEND-1;i++)
	{
		frequency_table[i]=0;
	}
}

int make_output()
{
	int i=0,j=0,size=0,temp=0;;
	for(i=ASCIISTART;i<ASCIIEND;i++)
	{
		if(frequency_table[i]==0)
				continue;
		output_table[j++]=i;
	}
	size=j;
	for(i=0;i<size-1;i++)
	{
		for(j=i+1;j<size;j++)
		{
			if(frequency_table[output_table[i]]>frequency_table[output_table[j]])
			{
				temp=output_table[i];
				output_table[i]=output_table[j];
				output_table[j]=temp;
			}
			else if(frequency_table[output_table[i]]==frequency_table[output_table[j]])
			{
				if(output_table[i]<output_table[j])
				{
					temp=output_table[i];
					output_table[i]=output_table[j];
					output_table[j]=temp;
				}
			}
		}
	}
	return size;
}

int main() 
{
	char str[MAXLEN];
	int size=0;
	/*freopen("c:/10062_in.txt","r",stdin);
	freopen("c:/10062_out.txt","w",stdout);*/
	while(gets(str)!=NULL)
	{
	
		int i=0;
		
	
		if(strlen(str)>1000)
			continue;
		if(size!=0)	
			printf("\n");
		init_frequensy();
		
		for(i=0;i<strlen(str);i++)
		{
			//if(str[i]>=ASCIISTART && str[i]<ASCIIEND)
				frequency_table[str[i]]++;
		}
		size=make_output();
		for(i=0;i<size;i++)
		{
			printf("%d %d\n",output_table[i],frequency_table[output_table[i]]);
		}
		
		
	}
	return 0;
}


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

Re: 10062 - Tell Me the Frequencies!

Post by brianfry713 » Fri Nov 09, 2012 2:02 am

There are a few things it could be:
For input:

Code: Select all

AAABBC

122333
The output should be:

Code: Select all

67 1
66 2
65 3


49 1
50 2
51 3
You don't clear ASCII code 127.
It doesn't really make sense to me to continue if strlen(str)>1000.
This line is going to be slow: for(i=0;i<strlen(str);i++)
Check input and AC output for thousands of problems on uDebug!

gr81
New poster
Posts: 46
Joined: Wed Sep 26, 2012 7:52 pm

Re: 10062 - Tell Me the Frequencies!

Post by gr81 » Wed Dec 05, 2012 9:11 pm

Code: Select all


Removed.

Last edited by gr81 on Fri Dec 14, 2012 7:31 am, edited 1 time in total.

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

Re: 10062 - Tell Me the Frequencies!

Post by brianfry713 » Sat Dec 08, 2012 5:45 pm

It looks like you figured it out.
Check input and AC output for thousands of problems on uDebug!

nova
New poster
Posts: 4
Joined: Thu Dec 13, 2012 1:42 pm

Re: 10062 - Tell Me the Frequencies!

Post by nova » Thu Dec 13, 2012 1:51 pm

hi
somebody please tell me why my code is getting WA?

Code: Select all

Removed after ACC
Last edited by nova on Fri Dec 14, 2012 12:37 am, edited 1 time in total.

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

Re: 10062 - Tell Me the Frequencies!

Post by brianfry713 » Fri Dec 14, 2012 12:29 am

Change line 8 to:
char input[1001];
and change line 14 to:
while(cin.getline(input, 1001))
Check input and AC output for thousands of problems on uDebug!

nova
New poster
Posts: 4
Joined: Thu Dec 13, 2012 1:42 pm

Re: 10062 - Tell Me the Frequencies!

Post by nova » Fri Dec 14, 2012 12:37 am

oh!
i got WA 11 times! finally accepted!
thanks, really!

shuvrothpol1
New poster
Posts: 17
Joined: Wed Aug 15, 2012 12:37 pm

Re: 10062 - Tell Me the Frequencies!

Post by shuvrothpol1 » Sat Dec 15, 2012 9:03 pm

getting wa.why?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 99999

char str[N],asci[N],freq[N];
int len,l;
void bubsort(char str[]);
void mulbubsort();
int main ()
{
int i,j,flag,count;
while (gets(str)){
len=strlen(str);
bubsort(str);
j=0;
for (i=0;i<len;i++){
count=0;
flag=0;
asci[j]=str;
while (asci[j]==str){
count++;
freq[j]=count;
i++;
flag=1;
}
if (flag==1)
i-=1;
j++;
}
l=j;
mulbubsort();
for (i=0;i<j;i++)
printf ("%d %d\n",asci,freq);
printf ("\n");
}

return 0;
}

void bubsort(char str[])
{
int t=len-1,i,j,temp;
for (i=0;i<t;i++){
for (j=0;j<t-i;j++){
if (str[j]>str[j+1]){
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
}

void mulbubsort()
{
int t=l-1,i,j;
char temp1,temp2;
for (i=0;i<t;i++){
for (j=0;j<t-i;j++){
if (freq[j]>freq[j+1]){
temp1=freq[j];
temp2=asci[j];
freq[j]=freq[j+1];
asci[j]=asci[j+1];
freq[j+1]=temp1;
asci[j+1]=temp2;
}
else if (freq[j]==freq[j+1]){
if (asci[j]<asci[j+1]){
temp2=asci[j];
asci[j]=asci[j+1];
asci[j+1]=temp2;
}
}
}
}
}

shuvokr
Learning poster
Posts: 66
Joined: Tue Oct 02, 2012 8:16 pm
Location: Bangladesh

Re: 10062 - Tell Me the Frequencies!

Post by shuvokr » Sat Dec 15, 2012 9:12 pm

I hope that >>> there is a sely mistak <shuvrothpol>. your I/O format is not correct >>> see that

ACC Input/Output Format:

<input>
<answer>
<input>
<blank>
<answer>
<input>
<blank>
<answer>
<input>
<blank>
<answer>
........
........
Example :
2 2 2
32 2
50 3
1122333

50 2
49 2
51 3
2 2 2

32 2
50 3
1121112113

51 1
50 2
49 7

I hope that its help you >>>
Thinking easily && best of luck :P

Code: Select all

enjoying life ..... 

shuvrothpol1
New poster
Posts: 17
Joined: Wed Aug 15, 2012 12:37 pm

Re: 10062 - Tell Me the Frequencies!

Post by shuvrothpol1 » Sat Dec 15, 2012 9:43 pm

still wa :(
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 99999

char str[N],asci[N],freq[N];
int len,l;
void bubsort(char str[]);
void mulbubsort();
int main ()
{
int i,j,f=1,flag,count;
while (gets(str)){
if (f>1)
printf ("\n");
len=strlen(str);
bubsort(str);
j=0;
for (i=0;i<len;i++){
count=0;
flag=0;
asci[j]=str;
while (asci[j]==str && i<len){
count++;
freq[j]=count;
i++;
flag=1;
}
if (flag==1)
i-=1;
j++;
}
l=j;
mulbubsort();
for (i=0;i<j;i++)
printf ("%d %d\n",asci,freq);
f++;
}

return 0;
}

void bubsort(char str[])
{
int t=len-1,i,j,temp;
for (i=0;i<t;i++){
for (j=0;j<t-i;j++){
if (str[j]>str[j+1]){
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
}

void mulbubsort()
{
int t=l-1,i,j;
char temp1,temp2;
for (i=0;i<t;i++){
for (j=0;j<t-i;j++){
if (freq[j]>freq[j+1]){
temp1=freq[j];
temp2=asci[j];
freq[j]=freq[j+1];
asci[j]=asci[j+1];
freq[j+1]=temp1;
asci[j+1]=temp2;
}
else if (freq[j]==freq[j+1]){
if (asci[j]<asci[j+1]){
temp2=asci[j];
asci[j]=asci[j+1];
asci[j+1]=temp2;
}
}
}
}
}

Post Reply

Return to “Volume 100 (10000-10099)”