10062 - Tell me the frequencies!
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell Me the Frequencies!
Don't print a blank line at the end of the output.
Check input and AC output for thousands of problems on uDebug!
Re: 10062 - Tell Me the Frequencies!
My solution doesn't print last line!
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell Me the Frequencies!
http://ideone.com/uh8sz
For the sample input, your output is:AC output is:
For the sample input, your output is:
Code: Select all
67 1
66 2
65 3
49 1
50 2
51 3
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!
Re: 10062 - Tell Me the Frequencies!
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;
}
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell Me the Frequencies!
Try putting a newline at the end of the last input line.
Check input and AC output for thousands of problems on uDebug!
Re: 10062 - Tell Me the Frequencies!
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:
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell Me the Frequencies!
There are a few things it could be:
For input:The output should be: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++)
For input:
Code: Select all
AAABBC
122333
Code: Select all
67 1
66 2
65 3
49 1
50 2
51 3
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!
Re: 10062 - Tell Me the Frequencies!
Code: Select all
Removed.
Last edited by gr81 on Fri Dec 14, 2012 7:31 am, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell Me the Frequencies!
It looks like you figured it out.
Check input and AC output for thousands of problems on uDebug!
Re: 10062 - Tell Me the Frequencies!
Last edited by nova on Fri Dec 14, 2012 12:37 am, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell Me the Frequencies!
Change line 8 to:
char input[1001];
and change line 14 to:
while(cin.getline(input, 1001))
char input[1001];
and change line 14 to:
while(cin.getline(input, 1001))
Check input and AC output for thousands of problems on uDebug!
Re: 10062 - Tell Me the Frequencies!
oh!
i got WA 11 times! finally accepted!
thanks, really!
i got WA 11 times! finally accepted!
thanks, really!
-
- New poster
- Posts: 17
- Joined: Wed Aug 15, 2012 12:37 pm
Re: 10062 - Tell Me the Frequencies!
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;
}
}
}
}
}
#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;
}
}
}
}
}
Re: 10062 - Tell Me the Frequencies!
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
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

Code: Select all
enjoying life .....
-
- New poster
- Posts: 17
- Joined: Wed Aug 15, 2012 12:37 pm
Re: 10062 - Tell Me the Frequencies!
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;
}
}
}
}
}

#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;
}
}
}
}
}