10352 - Count the eWords
Moderator: Board moderators
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
10352 - Count the eWords
Can someone please give me the correct output for:
abc ab abcd abe abd #
abc ab abcd abe abd #
-
- New poster
- Posts: 4
- Joined: Fri Oct 19, 2001 2:00 am
- Location: AIUB, BANGLADESH
- Contact:
[b]May Be This Is The Output:[/b]
ab 1
abd 3
abcd 1
abd 3
abcd 1
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
-
- New poster
- Posts: 27
- Joined: Sun Jul 07, 2002 6:46 pm
- Location: Campina Grande - Brazil
- Contact:
There is no especial cases.
There is no input problems, I use a array of size 30 to store each word in the input.
There is a statement in the problem that may be confuse.
"The lines are ordered in ascending (ASCII) order of the words. The 'ignored' (i.e. the 3rd) letter in the word should show the last 'ignored' letter. However, the sorting should be according to the first 'ignored' letter. For Example if the email contains the line
There is no input problems, I use a array of size 30 to store each word in the input.
There is a statement in the problem that may be confuse.
"The lines are ordered in ascending (ASCII) order of the words. The 'ignored' (i.e. the 3rd) letter in the word should show the last 'ignored' letter. However, the sorting should be according to the first 'ignored' letter. For Example if the email contains the line
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
WA...
I didnt use strcmp, just replaced all 3rd character with ~, and then use string operator >
wonder whats wrong, please help.
[cpp]
/* @JUDGE_ID: 3162CM 10352 C++ */
#include <stdio.h>
#include <string>
#include <map>
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
int n;
struct wtype{
string s,f;
int c;
};
void main (void)
{
string s;
int xxx=0;
while (cin >> s)
{
xxx++;
cout << "Set #" << xxx << endl;
map<string, wtype> a;
vector<wtype> b;
map<string, wtype>::iterator it;
while(s!="#")
{
char cs[200];
if (s.size()>5)s=s.substr(0,5);
string x=s;
if (x.size()>2)x[2]='~';
it=a.find(x);
if (it==a.end())
{
wtype w;
w.s = s;
w.c = 1;
w.f = x;
a.insert(make_pair(x,w));
} else{
(*it).second.s = s;
(*it).second.c ++;
}
cin>>s;
}
int i,j;
for (it=a.begin();it!=a.end();it++)
b.push_back((*it).second);
for (i=0;i<b.size();i++)
for (j=i+1;j<b.size();j++)
if (b.f>b[j].f)
{
swap (b, b[j]);
}
for (i=0;i<b.size();i++)
cout << b.s << " " << b.c << endl;
cout << endl;
}
}
[/cpp]
wonder whats wrong, please help.
[cpp]
/* @JUDGE_ID: 3162CM 10352 C++ */
#include <stdio.h>
#include <string>
#include <map>
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
int n;
struct wtype{
string s,f;
int c;
};
void main (void)
{
string s;
int xxx=0;
while (cin >> s)
{
xxx++;
cout << "Set #" << xxx << endl;
map<string, wtype> a;
vector<wtype> b;
map<string, wtype>::iterator it;
while(s!="#")
{
char cs[200];
if (s.size()>5)s=s.substr(0,5);
string x=s;
if (x.size()>2)x[2]='~';
it=a.find(x);
if (it==a.end())
{
wtype w;
w.s = s;
w.c = 1;
w.f = x;
a.insert(make_pair(x,w));
} else{
(*it).second.s = s;
(*it).second.c ++;
}
cin>>s;
}
int i,j;
for (it=a.begin();it!=a.end();it++)
b.push_back((*it).second);
for (i=0;i<b.size();i++)
for (j=i+1;j<b.size();j++)
if (b.f>b[j].f)
{
swap (b, b[j]);
}
for (i=0;i<b.size();i++)
cout << b.s << " " << b.c << endl;
cout << endl;
}
}
[/cpp]
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
-
- New poster
- Posts: 27
- Joined: Sun Jul 07, 2002 6:46 pm
- Location: Campina Grande - Brazil
- Contact:
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
Thanks for hints
))
I finally got Accepted too![:-)](./images/smilies/icon_smile.gif)
But I think, that I still can use strcmp() if I use struct like this
and set third letter to i.e. space ...
Greetings
![:-)](./images/smilies/icon_smile.gif)
I finally got Accepted too
![:-)](./images/smilies/icon_smile.gif)
But I think, that I still can use strcmp() if I use struct like this
Code: Select all
typedef struct { char word[10]; char last; } WORD;
Greetings
Hi,
I'm having problems understanding why my program won't run under 30 seconds under the same problem. Perhaps it's my input that doesn't end while (cin>>str). Could anyone please tell me if they find any problem or bad design. Thanks a lot.
[cpp]#include <iostream>
#include <list>
#include <string>
//#include <fstream>
using namespace std;
class wo{
public:
string str;
char let;
int amt;
};
void main(){
//ifstream in("in.txt");
//ofstream out("out.txt");
wo word;
string str;
list<wo>email;
int set=1;
while ((cin>>str){
if (str!="#"){ //old email
if (str.length()>5)str=str.substr(0,5);
word.let='#';
if (str.length()>=3){
word.let=str[2];
str[2]='#';
}
word.str=str;
word.amt=1;
list<wo>::iterator i=email.begin();
while (i!=email.end()){
if ((*i).str==str){
(*i).amt++;
(*i).let=word.let;
break;
}
else if ((*i).str>str){
email.insert(i,word);
break;
}
else i++;
}
if (i==email.end())email.push_back(word);
}
else {//new email found
if (set>1)cout<<endl;
cout<<"Set #"<<set<<":\n";
list<wo>::iterator i=email.begin();
while (i!=email.end()){
if ((*i).let!='#')(*i).str[2]=(*i).let;
cout<<(*i).str<<" "<<(*i).amt<<endl;
i++;
}
set++;
email.clear();
}
}
}
[/cpp][/cpp]
I'm having problems understanding why my program won't run under 30 seconds under the same problem. Perhaps it's my input that doesn't end while (cin>>str). Could anyone please tell me if they find any problem or bad design. Thanks a lot.
[cpp]#include <iostream>
#include <list>
#include <string>
//#include <fstream>
using namespace std;
class wo{
public:
string str;
char let;
int amt;
};
void main(){
//ifstream in("in.txt");
//ofstream out("out.txt");
wo word;
string str;
list<wo>email;
int set=1;
while ((cin>>str){
if (str!="#"){ //old email
if (str.length()>5)str=str.substr(0,5);
word.let='#';
if (str.length()>=3){
word.let=str[2];
str[2]='#';
}
word.str=str;
word.amt=1;
list<wo>::iterator i=email.begin();
while (i!=email.end()){
if ((*i).str==str){
(*i).amt++;
(*i).let=word.let;
break;
}
else if ((*i).str>str){
email.insert(i,word);
break;
}
else i++;
}
if (i==email.end())email.push_back(word);
}
else {//new email found
if (set>1)cout<<endl;
cout<<"Set #"<<set<<":\n";
list<wo>::iterator i=email.begin();
while (i!=email.end()){
if ((*i).let!='#')(*i).str[2]=(*i).let;
cout<<(*i).str<<" "<<(*i).amt<<endl;
i++;
}
set++;
email.clear();
}
}
}
[/cpp][/cpp]