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!
A blank line should separate each set of output.
Don't print an extra blank line at the end.
Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 16
- Joined: Thu Oct 10, 2013 8:06 am
Re: 10062 - Tell Me the Frequencies!
thanks brianfry713.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell Me the Frequencies!
Input:AC output:
Code: Select all
A
B
Code: Select all
65 1
66 1
32 1
Check input and AC output for thousands of problems on uDebug!
Re: 10062 - Tell Me the Frequencies!
Thank you very much for this test case, brianfry713. I hadn't considered that there might be spaces before the alphabets or punctuation.brianfry713 wrote:Input:AC output:Code: Select all
A B
Code: Select all
65 1 66 1 32 1
Re: 10062 - Tell Me the Frequencies!
Can anybody please tell me what am I doing wrong here:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
struct ascii_freq
{
int ascii;
int freq;
};
bool compare (const ascii_freq &a, const ascii_freq &b)
{
if(a.freq != b.freq)
return a.freq < b.freq;
return a.ascii > b.ascii;
}
int main()
{
// freopen("input.txt", "r", stdin);
bool firsttime = true;
char line[1001];
int i;
vector<ascii_freq> V;
while(gets(line))
{
if(!firsttime)
puts("\n");
int address[128];
int vindex = 0;
memset(address, -1, sizeof(address));
for(i=0; line!=0; i++)
{
if(address[line] == -1)
{
ascii_freq temp;
temp.ascii = line;
temp.freq = 1;
V.push_back(temp);
address[line] = vindex;
vindex++;
}
else
{
V[address[line]].freq++;
}
}
sort(V.begin(), V.end(), compare);
for(i=0; i<V.size(); i++)
{
printf("%d %d", V.ascii, V.freq);
if(i < V.size()-1)
puts("");
}
V.clear();
firsttime = false;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
struct ascii_freq
{
int ascii;
int freq;
};
bool compare (const ascii_freq &a, const ascii_freq &b)
{
if(a.freq != b.freq)
return a.freq < b.freq;
return a.ascii > b.ascii;
}
int main()
{
// freopen("input.txt", "r", stdin);
bool firsttime = true;
char line[1001];
int i;
vector<ascii_freq> V;
while(gets(line))
{
if(!firsttime)
puts("\n");
int address[128];
int vindex = 0;
memset(address, -1, sizeof(address));
for(i=0; line!=0; i++)
{
if(address[line] == -1)
{
ascii_freq temp;
temp.ascii = line;
temp.freq = 1;
V.push_back(temp);
address[line] = vindex;
vindex++;
}
else
{
V[address[line]].freq++;
}
}
sort(V.begin(), V.end(), compare);
for(i=0; i<V.size(); i++)
{
printf("%d %d", V.ascii, V.freq);
if(i < V.size()-1)
puts("");
}
V.clear();
firsttime = false;
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell Me the Frequencies!
Print a newline char at the end of the last line.
Check input and AC output for thousands of problems on uDebug!
Re: 10062 - Tell Me the Frequencies!
AC.........
tnx v1n1t![:D](./images/smilies/icon_biggrin.gif)
tnx v1n1t
![:D](./images/smilies/icon_biggrin.gif)
Last edited by TLEorWA on Mon Mar 24, 2014 7:08 pm, edited 1 time in total.
Re: 10062 - Tell Me the Frequencies!
Your code seg faults on the sample input. You might want to begin there.TLEorWA wrote:Why Runtime Error,what is the problem![]()
![]()
10062 - Tell me the frequencies!
whats wrong in my code?i got wrong answer
Code: Select all
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool cmp(const pair<int, int> &p1, const pair<int, int> &p2)
{
return p1.second < p2.second;
}
int main()
{
string n;
int ascii[1001],i,j,m,l,temp,f=1;
map <int, int> c;
vector<pair<int, int> > v;
std::map < int, int >::iterator it;
while(getline(cin,n))
{
l=n.length();
m=0;
c.clear();
v.clear();
for(i=0;i<l;i++)
{
if(n[i]!=' '&&((int)n[i]>=32)&&((int)n[i]<=128))
{
c[n[i]]=1;
for(j=i+1;j<l;j++)
{
if(n[i]==n[j])
{
c[(int)n[i]]++;
n[j]=' ';
}
}
}
}
copy(c.begin(), c.end(), back_inserter(v));
sort(v.begin(), v.end(), cmp);
if(f++>1)
cout<<endl;
for(int i = 0; i < v.size(); ++i)
cout<<v[i].first <<" "<<v[i].second<<endl;
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10062 - Tell me the frequencies!
You should count space = ASCII 32
Check input and AC output for thousands of problems on uDebug!