10226 - Hardwood Species

All about problems in Volume 102. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Because streams (and esp. iomanip) were implemented in a different way in old version of g++. The flags were integers, so cout << an_ios_flag simply prints (and printed) an integer. That's why setf function is necessary to set a flag.

This was changed so that flags have their own class now (std::ios_base). operator<<, overloaded for this type, calls setf.

Flags like std::ios::right (which is equal 128 in g++ 3.3), for compatibility, are still integers. However, std::right is a std::ios_base object and can be passed as argument to operator<<.

PunyTroll
New poster
Posts: 3
Joined: Thu Jun 17, 2004 8:03 pm
Location: Berlin, Germany
Contact:

Post by PunyTroll »

hm, actually it was std::cout << std::right which caused the error message in the first place.

Code: Select all

02603660_24.c: In function `int main()':
02603660_24.c:127: `::right' undeclared (first use here)
or did I misinterpret your response :)

Hagen.

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Read again. Error message that you quoted comes from OJ, which works on g++ 2.95. The old version. It has no std::right. It has no ability to use cout << a_flag neither, for the reasons that I already mentioned.

PunyTroll
New poster
Posts: 3
Joined: Thu Jun 17, 2004 8:03 pm
Location: Berlin, Germany
Contact:

Post by PunyTroll »

hm, reading again, I still can't see it but that doesn't matter now. I see what you mean and additionally I finally found the information which version of gcc OJ uses. Thx in any case :)

Hagen.

cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

I used STL hash_map
But dunno why
I still got TLE... :cry:

can anybody help me?

[cpp]#include <hash_set.h>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>

using namespace std;

int main()
{
int p;
cin>>p;
char h[32];
cin.getline(h,32);
cin.getline(h,32);
for (int y=0;y<p;y++)
{
if (y)
cout<<endl;
hash_multiset <char*> sky;
cin.getline(h,32);
while (strlen(h))
{
sky.insert(h);
cin.getline(h,32);
cout<<h<<endl;
cout<<sky.count(h)<<endl;
}
double i=sky.size()/100.0;
string s="";
cout<<i<<endl;
copy(sky.begin(), sky.end(), ostream_iterator<char*>(cout, " "));
for (hash_multiset <char*>::iterator l=sky.begin();l!=sky.end();l++)
{
if (*l!=s)
cout<<(*l)<<' '<<setprecision(4)<<setiosflags(ios::fixed)<<sky.count(*l)/i<<endl;
s=*l;
}
}
return 0;
} [/cpp]

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity »

First I used STL map - TLE.
Then I implemented my own hash, which made my program ran in linear time - TLE.
I changed my code to process only first 3 inputs - WA in 8.5 sec...

Then I sent:
[cpp]string s;
while(getline(cin, s));[/cpp] - TLE !!!!!????
I guess this is input problem, not algorithmic problem... :x

cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

Yea, that's the input prob
I did this before too.
It seems that we can only use C-styled character array instead of C++ strings as input.
However, we can't use this for STL map :cry:
Impossible is Nothing.

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

Where t is the name of the map, and a is the name of the char array.

t[ string( a ) ] = whatever;

(Though, of course, it takes time..)

I used STL's map and got AC in about 5 secs..

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity »

Larry wrote:Where t is the name of the map, and a is the name of the char array.

t[ string( a ) ] = whatever;

(Though, of course, it takes time..)

I used STL's map and got AC in about 5 secs..
Who did you read char arrays? gets()?

cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

minskcity wrote:
Larry wrote:Where t is the name of the map, and a is the name of the char array.

t[ string( a ) ] = whatever;

(Though, of course, it takes time..)

I used STL's map and got AC in about 5 secs..
Who did you read char arrays? gets()?
yea
i used sth like this:

char y[size];
cin>>y;
set<char *> t;
t[y]=...;

but when i call t.count(y) it is always 0

my code is shown above
the hash set throw away all data
Impossible is Nothing.

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity »

char y[size];
cin>>y;
set<char *> t;
t[y]=...;
1) STL "set" is not hashset, optional second parameter in the template is comparator.
2) STL "set" does not look to support []. STL "map" does.
3) cin >> y will only read until space.
4) set < char * > might create a set of pointers, not strings.

---- According to Larry, it's possible to get AC by using STL map ( not hashed ) by reading char* and converting them into strings when accessing the map.

I was asking Larry which function he was using to read char arrays fast enough...

cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike »

I used this:

[cpp]hash_multiset <char*> sky;
cin.getline(h,32);
while (strlen(h))
{
sky.insert(h);
cin.getline(h,32);
}[/cpp]

a set of pointers? why? what should i do then? convert c-styled char array to c++ string first?
Impossible is Nothing.

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

I don't have the code in front of me, but I almost never use cin, (since I am still mostly a C programmer)

I do something like this:

Code: Select all

char a[MAX_L];
map<string, int> t;

while ( gets( a ) ) {
    t[ string( a ) ]++;
}
Of course, you have to clear and stuff, but that's how I handle it..

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity »

Larry wrote:I don't have the code in front of me, but I almost never use cin, (since I am still mostly a C programmer)

I do something like this:

Code: Select all

char a[MAX_L];
map<string, int> t;

while ( gets( a ) ) {
    t[ string( a ) ]++;
}
Of course, you have to clear and stuff, but that's how I handle it..
THANKS A LOT!!!! :D
The only change I made to my program was getline->gets...
That corresponded to 10+sec->1.77sec, AC, 7th in ranking... (looks like my hashing works)

PS: You might be a mostly a C programmer, but I bet you are using STL map whenever is possible. :wink:

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

I am indeed a C programmer.. but I was learning the STL at some point.. no point in reinventing the wheels, eh?

Post Reply

Return to “Volume 102 (10200-10299)”