sazzadcsedu wrote: for(j=256;j>=0;j--)
{
if(count[j]<min && count[j]>0)
{
min=count[j];
k=j;
}
}
In this section your program accesses count[256], which is undefined.
Also don't print the blank line at the end of output.
Moderator: Board moderators
sazzadcsedu wrote: for(j=256;j>=0;j--)
{
if(count[j]<min && count[j]>0)
{
min=count[j];
k=j;
}
}
sort me by bubblesort
std::stable_sort(), std::list<T>.sort(), std::stable_partition().
121 1
117 1
109 1
108 1
116 2
115 2
114 2
111 2
101 2
32 3
98 4
112 1
110 1
84 1
62 1
60 1
101 2
98 2
95 2
46 2
44 2
32 2
114 3
111 3
108 3
105 3
100 3
97 3
41 3
40 3
58 6
115 8
116 10
#include <iostream>
using namespace std;
int main()
{
string a;
char b[200];
int c,d,i,j,k,array[1000],l=0,count[1000],count2[1000],array2[1000],q=0,u,y,temp,temp2;
while(cin>>a)
{q=0;
for(i=0;a[i]!='\0';i++)
{
array[i]=a[i];
}
for(y=0;y<i;y++)
for(u=0;u<i-1;u++)
{
if(array[u]<array[u+1])
{
temp=array[u];
array[u]=array[u+1];
array[u+1]=temp;
}
}
for(j=0;j<i;j++)
count[j]=0;
for(j=0;j<i;j++)
{
if(array[j]!=0)
{count[j]=1;
for(k=j+1;k<i;k++)
{
if(array[j]==array[k])
{
count[j]++;
array[k]=0;
}
}
}
}
for(j=0;j<i;j++)
{
if(count[j]!=0 && array[j]!=0)
{
count2[q]=count[j];
array2[q]=array[j];
q++;
}
}
for(y=0;y<q;y++)
for(u=0;u<q-1;u++)
{
if(count2[u]>count2[u+1])
{
temp=count2[u];
count2[u]=count2[u+1];
count2[u+1]=temp;
temp2=array2[u];
array2[u]=array2[u+1];
array2[u+1]=temp2;
}
}
for(j=0;j<q;j++)
{
cout<<array2[j]<<" "<<count2[j]<<endl;
}
cout<<endl;
//printf("\r");
}
return 0;
}

#include<iostream>
//#include<conio.h>
using namespace std;
int flag=0;
void p(char a,int n,int cf[100],int c[100])
{
int j=0,i=0;
if(a=='\n')
{
n++;
for(i=0;i<96;i++)
{
for(j=0;j<95;j++)
{
if(cf[j]>cf[j+1])
{
swap(cf[j],cf[j+1]);
swap(c[j],c[j+1]);
}
else if((cf[j]==cf[j+1]&&c[j]<c[j+1]))
{
swap(cf[j],cf[j+1]);
swap(c[j],c[j+1]);
}
}
}
if(flag==1)
cout<<endl;
flag=1;
for(j=0;j<96;j++)
{
if(cf[j]&&c[j])
cout<<c[j]<<' '<<cf[j]<<endl;
c[j]=0;
}
int b=0;
}
}
int main()
{
freopen("in.txt","r",stdin);
char a;
int i=0;
int cf[100];
for(i=0;i<100;i++)
{
cf[i]=0;
}
int n=0;
while(1)
{
int c[100];
if((a=getchar())==EOF)
{
a='\n';
p(a,n,cf,c);
break;
}
i=0;
int x=(int)a;
if(x>=32&&x<=128)
{
i=x-32;
//cout<<i;
c[i]=i+32;
cf[i]++;
}
p(a,n,cf,c);
}
//getch();
return 0;
}
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
bool comp( pii a, pii b ) {
if( a.second == b.second ) return a.first > b.first;
else return a.second < b.second;
}
int main() {
char line[1005];
bool newln = 0;
while( gets( line ) ) {
char freq[500] = {};
int len = strlen( line );
REP(i,len) freq[ line[i] ] ++;
vector< pii > v;
REP(i,500) if( freq[i] ) v.pb( make_pair( i, freq[i] ) );
sort( ALL(v), comp );
if( newln ) cout << endl; newln = 1;
REP(i,v.sz) cout << v[i].first <<" " << v[i].second << endl;
}
return 0;
}

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), 1024 * 1024);
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;
}
}
}
67 1
66 2
65 3
49 1
50 2
51 3
67 1
66 2
65 3
49 1
50 2
51 3import 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;
}
}
}
Users browsing this forum: Bing [Bot] and 1 guest