10062 - Tell me the frequencies!
Moderator: Board moderators
But still I get WA.
Here is my JAVA code:
=============================================
class Main {
public static void main(String[] args){
String line = null;
int[] frequencies_arr = null;
char[] chars_arr = null;
while ( (line = readLn(1005)) != null ){
frequencies_arr = new int[96];
chars_arr = new char[96];
for (int i=0; i<line.length(); i++){
char ch = line.charAt(i);
if (ch == '\r' || ch == '\n') continue;
frequencies_arr[((byte)ch) - 32]++;
chars_arr[((byte)ch) - 32] = ch;
}
bubble_sort_arr(frequencies_arr, chars_arr);
int i = 0;
while (frequencies_arr == 0) i++;
for (int k=i; k<=frequencies_arr.length-1; k++){
System.out.println( ((byte)chars_arr[k]) + ( " " + frequencies_arr[k] ) );
}
System.out.println();
}
}
public static String readLn (int maxLg){
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try{
while (lg < maxLg){
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}catch (java.io.IOException e){
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
private static void bubble_sort_arr(int[] v, char[] char_arr){
int n = v.length;
int int_tmp = 0;
char ch_temp = ' ';
for (int i=0; i< n-1; i++) {
for (int j=0; j<n-1-i; j++){
if (v[j+1] < v[j] ) {
int_tmp = v[j];
v[j] = v[j+1];
v[j+1] = int_tmp;
ch_temp = char_arr[j];
char_arr[j] = char_arr[j+1];
char_arr[j+1] = ch_temp;
}
}
}
for (int i=0; i< n-1; i++) {
for (int j=0; j<n-1-i; j++){
if ( ( v[j+1] == v[j] && char_arr[j+1] > char_arr[j] ) ) {
ch_temp = char_arr[j];
char_arr[j] = char_arr[j+1];
char_arr[j+1] = ch_temp;
}
}
}
}
}
=============================================
Here is my JAVA code:
=============================================
class Main {
public static void main(String[] args){
String line = null;
int[] frequencies_arr = null;
char[] chars_arr = null;
while ( (line = readLn(1005)) != null ){
frequencies_arr = new int[96];
chars_arr = new char[96];
for (int i=0; i<line.length(); i++){
char ch = line.charAt(i);
if (ch == '\r' || ch == '\n') continue;
frequencies_arr[((byte)ch) - 32]++;
chars_arr[((byte)ch) - 32] = ch;
}
bubble_sort_arr(frequencies_arr, chars_arr);
int i = 0;
while (frequencies_arr == 0) i++;
for (int k=i; k<=frequencies_arr.length-1; k++){
System.out.println( ((byte)chars_arr[k]) + ( " " + frequencies_arr[k] ) );
}
System.out.println();
}
}
public static String readLn (int maxLg){
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try{
while (lg < maxLg){
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}catch (java.io.IOException e){
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
private static void bubble_sort_arr(int[] v, char[] char_arr){
int n = v.length;
int int_tmp = 0;
char ch_temp = ' ';
for (int i=0; i< n-1; i++) {
for (int j=0; j<n-1-i; j++){
if (v[j+1] < v[j] ) {
int_tmp = v[j];
v[j] = v[j+1];
v[j+1] = int_tmp;
ch_temp = char_arr[j];
char_arr[j] = char_arr[j+1];
char_arr[j+1] = ch_temp;
}
}
}
for (int i=0; i< n-1; i++) {
for (int j=0; j<n-1-i; j++){
if ( ( v[j+1] == v[j] && char_arr[j+1] > char_arr[j] ) ) {
ch_temp = char_arr[j];
char_arr[j] = char_arr[j+1];
char_arr[j+1] = ch_temp;
}
}
}
}
}
=============================================
10062 W.A
I got a W.A
but I don't know why,please help me~~>"<
here is my code.
#include "iostream.h"
#include "stdlib.h"
#include "string.h"
struct output
{
int ascii;
int number;
};
void main()
{
int i,j;
char input[1001];
int length,index=0,buffer_ascii=0,buffer_number=0;
int answer[1001]={0};
output out[1001];
int test=0;
while(cin>>input)
{
length=strlen(input);
for(i=0;i<length;i++)
{
if(input>32 &&input<=128)
answer[input]++;
}
for(i=0;i<1001;i++)
{
if(answer!=0)
{
out[index].ascii=i;
out[index].number=answer;
index++;
}
}
for(i=0;i<1000 && out.number>0;i++)
{
for(j=i;j<1001 && out[j].number>0;j++)
{
if(out.number>out[j].number)
{
buffer_ascii=out.ascii;
buffer_number=out.number;
out.ascii=out[j].ascii;
out[i].number=out[j].number;
out[j].ascii=buffer_ascii;
out[j].number=buffer_number;
}
}
}
for(i=0;i<1000 && out[i].number>0;i++)
{
for(j=i;j<1001 && out[j].number>0;j++)
{
if(out[i].number==out[j].number)
{
if(out[i].ascii<out[j].ascii)
{
buffer_ascii=out[i].ascii;
buffer_number=out[i].number;
out[i].ascii=out[j].ascii;
out[i].number=out[j].number;
out[j].ascii=buffer_ascii;
out[j].number=buffer_number;
}
}
}
}
for(i=0;i<1001 && out[i].number>0;i++)
{
cout<<out[i].ascii<<' '<<out[i].number<<endl;
}
cout<<endl;
for(i=0;i<1001;i++)
answer[i]=0;
buffer_ascii=0,buffer_number=0;
length=0;
index=0;
for(i=0;i<1001;i++)
{
out[i].ascii=-1;
out[i].number=-1;
}
}
}
but I don't know why,please help me~~>"<
here is my code.
#include "iostream.h"
#include "stdlib.h"
#include "string.h"
struct output
{
int ascii;
int number;
};
void main()
{
int i,j;
char input[1001];
int length,index=0,buffer_ascii=0,buffer_number=0;
int answer[1001]={0};
output out[1001];
int test=0;
while(cin>>input)
{
length=strlen(input);
for(i=0;i<length;i++)
{
if(input>32 &&input<=128)
answer[input]++;
}
for(i=0;i<1001;i++)
{
if(answer!=0)
{
out[index].ascii=i;
out[index].number=answer;
index++;
}
}
for(i=0;i<1000 && out.number>0;i++)
{
for(j=i;j<1001 && out[j].number>0;j++)
{
if(out.number>out[j].number)
{
buffer_ascii=out.ascii;
buffer_number=out.number;
out.ascii=out[j].ascii;
out[i].number=out[j].number;
out[j].ascii=buffer_ascii;
out[j].number=buffer_number;
}
}
}
for(i=0;i<1000 && out[i].number>0;i++)
{
for(j=i;j<1001 && out[j].number>0;j++)
{
if(out[i].number==out[j].number)
{
if(out[i].ascii<out[j].ascii)
{
buffer_ascii=out[i].ascii;
buffer_number=out[i].number;
out[i].ascii=out[j].ascii;
out[i].number=out[j].number;
out[j].ascii=buffer_ascii;
out[j].number=buffer_number;
}
}
}
}
for(i=0;i<1001 && out[i].number>0;i++)
{
cout<<out[i].ascii<<' '<<out[i].number<<endl;
}
cout<<endl;
for(i=0;i<1001;i++)
answer[i]=0;
buffer_ascii=0,buffer_number=0;
length=0;
index=0;
for(i=0;i<1001;i++)
{
out[i].ascii=-1;
out[i].number=-1;
}
}
}
10062 P.E.
Code: Select all
#include<stdio.h>
void bubbleSort(int *table,int *index,int n)
{
int i,j,t;
for(i=1;i<n-1;i++)
for(j=2;j<=n-1;j++)
if(table[j]>table[j-1])
{
table[j]=table[j]+table[j-1];
table[j-1]=table[j]-table[j-1];
table[j]=table[j]-table[j-1];
index[j]=index[j]+index[j-1];
index[j-1]=index[j]-index[j-1];
index[j]=index[j]-index[j-1];
}
}
main()
{
int table[129],index[129],copy_table[129];
int i;
char ch,first;
while(scanf("%c",&first)!=EOF)
{
for(i=1;i<129;i++)
{
table[i]=0;
index[i]=i;
}
table[(int)first]++;
while((ch=getchar())!='\n')
table[(int)ch]++;
for(i=0;i<129;i++)
copy_table[i]=table[i];
bubbleSort(copy_table,index,129);
for(i=129-1;i>=1;i--)
if(table[index[i]]!=0 && index[i]>=32 && index[i]<=128)
printf("%d %d\n",index[i],table[index[i]]);
//putchar('\n');
}
}
Could someone teach me.
Thanks a million...^^
ibrahim wrote:From the sample output we see
After output of every case there should be one new line (blank line).67 1
66 2
65 3
<=== A blank line
49 1
50 2
51 3
Code: Select all
aaa
97 3
<==this is a blank line
abc
99 1
98 1
97 1
<==this is a blank line
cde
101 1
100 1
99 1
<==but this is also a blank line
I think there should not be any blank line below 99 1...
is it right...
but it is hard to do..
i have no idea to implement..
could anyone give me some ideas...
thank you ^^
-
- Experienced poster
- Posts: 149
- Joined: Mon Feb 07, 2005 10:28 pm
- Location: Northern University, Bangladesh
- Contact:
Yea, there should not any blank line.
You can do it very easyly.
just make change in your code.

You can do it very easyly.
Code: Select all
char ch,first;
int flag=0;
while(scanf("%c",&first)!=EOF)
{
if(flag!=0)
printf("\h");
flag=1;



No..ibrahim wrote:Yea, there should not any blank line.
You can do it very easyly.
just make change in your code.Code: Select all
char ch,first; int flag=0; while(scanf("%c",&first)!=EOF) { if(flag!=0) printf("\h"); // \n ?? flag=1;
![]()
![]()
I work out a solution, and it works...
but the online judge give me a compile error
Q______Q
oh my god...
you could excute my program
then you would know your idea doesn't work..
thank you very much
welcome your new solution anytime...^^
Code: Select all
#include<stdio.h>
#include<conio.h>
void bubbleSort(int *table,int *index,int n)
{
int i,j,t;
for(i=1;i<n-1;i++)
for(j=2;j<=n-1;j++)
if(table[j]>table[j-1])
{
table[j]=table[j]+table[j-1];
table[j-1]=table[j]-table[j-1];
table[j]=table[j]-table[j-1];
index[j]=index[j]+index[j-1];
index[j-1]=index[j]-index[j-1];
index[j]=index[j]-index[j-1];
}
}
main()
{
int table[129],index[129],copy_table[129];
int i;
char ch,first;
int flag=0;
while((first=getch())!=EOF)
{
if(flag!=0)
printf("\n");
if(first)
printf("%c",first);
for(i=1;i<129;i++)
{
table[i]=0;
index[i]=i;
}
table[(int)first]++;
while((ch=getchar())!='\n')
table[(int)ch]++;
for(i=0;i<129;i++)
copy_table[i]=table[i];
bubbleSort(copy_table,index,129);
for(i=129-1;i>=1;i--)
if(table[index[i]]!=0 && index[i]>=32 && index[i]<=128)
printf("%d %d\n",index[i],table[index[i]]);
flag=1;
}
}
-
- Experienced poster
- Posts: 154
- Joined: Sat Apr 17, 2004 9:34 am
- Location: EEE, BUET
ibrahim wrote:Dear lonelyone, It's my typing mistake.if(flag!=0)
printf("\h"); // \n ??
Code: Select all
#include<stdio.h>
void bubbleSort(int *table,int *index,int n)
{
int i,j,t;
for(i=1;i<n-1;i++)
for(j=2;j<=n-1;j++)
if(table[j]>table[j-1])
{
table[j]=table[j]+table[j-1];
table[j-1]=table[j]-table[j-1];
table[j]=table[j]-table[j-1];
index[j]=index[j]+index[j-1];
index[j-1]=index[j]-index[j-1];
index[j]=index[j]-index[j-1];
}
}
main()
{
int table[129],index[129],copy_table[129];
int i;
char ch,first;
int flag=0;
while(scanf("%c",&first)!=EOF)
{
if(flag!=0)
printf("\n");
flag=1;
for(i=1;i<129;i++)
{
table[i]=0;
index[i]=i;
}
table[(int)first]++;
while((ch=getchar())!='\n')
table[(int)ch]++;
for(i=0;i<129;i++)
copy_table[i]=table[i];
bubbleSort(copy_table,index,129);
for(i=129-1;i>=1;i--)
if(table[index[i]]!=0 && index[i]>=32 && index[i]<=128)
printf("%d %d\n",index[i],table[index[i]]);
}
}
i tried, but it still failed..
it's output just like following message...
Code: Select all
123
51 1
50 1
49 1
456
<==this is a blank line
54 1
53 1
52 1
789
<==this is a blank line
57 1
56 1
55 1
abc
<==this is a blank line
99 1
98 1
97 1
|<==the input cursor
you could see the differences between them...
Code: Select all
123
51 1
50 1
49 1
<==this is a blank line
456
54 1
53 1
52 1
<==this is a blank line
789
57 1
56 1
55 1
<==this is a blank line
abc
99 1
98 1
97 1
|<==the input cursor
next, always put a blank line before the input...
but my program is hard to do so..
as you can see, i read a character first...
and after read, put a new line before the output...
that's the problem..
so, there's a solution, and i use getch() of conio.h...
cause, it can read a character and not do anything,
it means, no character could appear in the screen,
and then i could tell this character is equal to NULL or not,
if not then i put a charater and read following message...
but acm online judge can't accept conio.h, because it's not
ANSI standard...
please help me solve this problem...
thank to ibrahim and Mohammad Mahmudur Rahman
-
- New poster
- Posts: 33
- Joined: Tue Jun 29, 2004 1:38 pm
- Location: IITM,chennai,Tamil Nadu,India
- Contact:
10062 - WA - someone help
here's my code for 10062....can neone temme y i get WA? like some xtreme cases....
Code: Select all
#include<iostream>
#include<vector>
using namespace std;
struct asdf
{
char unique;
int frequency;
};
int alreadyseen(vector<asdf> &seensymbols,char a)
{
for(int i=0;i<seensymbols.size();i++)
{
if(seensymbols[i].unique==a)
{
seensymbols[i].frequency++;
return 1;
}
}
return 0;
}
void sort(vector<asdf> &freq)
{
struct asdf temp;
int n=freq.size();
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
{
if(freq[j].frequency<freq[i].frequency)
{
temp=freq[i];
freq[i]=freq[j];
freq[j]=temp;
}
else if(freq[j].frequency==freq[i].frequency)
{
if(freq[j].unique>freq[i].unique)
{
temp=freq[i];
freq[i]=freq[j];
freq[j]=temp;
}
}
}
}
main()
{
char text[1000];
while(cin>>text)
{
vector<asdf> seensymbols;
for(int i=0;i<strlen(text);i++)
{
if(alreadyseen(seensymbols,text[i])==0)
{
asdf temp;
temp.unique=text[i];
temp.frequency=1;
seensymbols.push_back(temp);
}
}
sort(seensymbols);
for(int i=0;i<seensymbols.size();i++)
{
int temp=seensymbols[i].unique;
cout<<temp<<" "<<seensymbols[i].frequency<<'\n';
}
cout<<'\n';
}
}
Karthe