10044 - Erdos Numbers
Moderator: Board moderators
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
Yes, it is possible. You can try to add
[cpp]
#include <cstdlib>
#define safe_insert(map,pair)\
try{map.insert(pair)}\
catch(bad_alloc)\
{\
cout<<"out of memory"<<endl;\
exit(1);\
}\
catch(...)\
{\
cout<<"something else wend bad"<<endl;\
exit(2);\
}
[/cpp]
and use safe_insert(map_name, pair_name) instead of map_name.insert(pair_name) . It will help you to avoid unhandled exceptions. If OJ sends the error code, you will know what exactly has happened.[/cpp]
[cpp]
#include <cstdlib>
#define safe_insert(map,pair)\
try{map.insert(pair)}\
catch(bad_alloc)\
{\
cout<<"out of memory"<<endl;\
exit(1);\
}\
catch(...)\
{\
cout<<"something else wend bad"<<endl;\
exit(2);\
}
[/cpp]
and use safe_insert(map_name, pair_name) instead of map_name.insert(pair_name) . It will help you to avoid unhandled exceptions. If OJ sends the error code, you will know what exactly has happened.[/cpp]
Thanks a lot. I'll try what you wrote. But before I saw the message I tried to organize my database (db) and map (m) more space-efficiently. However I could not compile the following function which is very strange to me:
[cpp]typedef set<string>::iterator iter;
set<string> allStrings;
void vectorToSet(int k, vector<string> &v, set< iter > &s) {
for(int i = 0; i < v.size(); i++)
if (i != k) {
iter it = allStrings.find(v);
s.insert(it); //compile error here
}
}[/cpp]
I decided that instead of keeping the strings in the containers (which I still think is not a problem since they are reference counted), I keep all the strings in the global set<string> variable allStrings and only iterators in the database (db), and the map (m). However, I could not compile the above excerpt. Any help please?
[cpp]typedef set<string>::iterator iter;
set<string> allStrings;
void vectorToSet(int k, vector<string> &v, set< iter > &s) {
for(int i = 0; i < v.size(); i++)
if (i != k) {
iter it = allStrings.find(v);
s.insert(it); //compile error here
}
}[/cpp]
I decided that instead of keeping the strings in the containers (which I still think is not a problem since they are reference counted), I keep all the strings in the global set<string> variable allStrings and only iterators in the database (db), and the map (m). However, I could not compile the above excerpt. Any help please?
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
You're trying to insert a vector iterator (which is actually an ordinary pointer to string) into set of strings. Won't work. T is not *T.
All the collections in STL _copy_ their elements. So you can have the following code working:
[cpp]
vector<string> v;
new string * s;
*s="asdf";
v.push_back(*s);
delete s;
cout<<v.front()<<endl;
[/cpp]
Now I see why you're getting out of memory
This version does what you need:
[cpp]
vector<string *> v;
new string * s;
*s="asdf";
v.push_back(s);
delete s;
cout<<v.front()<<endl;
[/cpp]
All the collections in STL _copy_ their elements. So you can have the following code working:
[cpp]
vector<string> v;
new string * s;
*s="asdf";
v.push_back(*s);
delete s;
cout<<v.front()<<endl;
[/cpp]
Now I see why you're getting out of memory

This version does what you need:
[cpp]
vector<string *> v;
new string * s;
*s="asdf";
v.push_back(s);
delete s;
cout<<v.front()<<endl;
[/cpp]
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
Try this:
[cpp]typedef set<string>::iterator iter;
struct mycomp {
bool operator()(iter a, iter b) {
return *a<*b;
}
};
set<string> allStrings;
void vectorToSet(int k, vector<string> &v, set< iter,mycomp> &s) {
for(int i = 0; i < v.size(); i++)
if (i != k) {
iter it = allStrings.find(v);
s.insert(it);
}
}
[/cpp]
[cpp]typedef set<string>::iterator iter;
struct mycomp {
bool operator()(iter a, iter b) {
return *a<*b;
}
};
set<string> allStrings;
void vectorToSet(int k, vector<string> &v, set< iter,mycomp> &s) {
for(int i = 0; i < v.size(); i++)
if (i != k) {
iter it = allStrings.find(v);
s.insert(it);
}
}
[/cpp]
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
You can make a set of objects only if they are comparable with <. Set iterators are not. Try something like (works if all the strings are different)
[cpp]
class cmp
{
public:
bool operator()(const set<string>::iterator &i1, const set<string>::iterator &i2)
{
return (*i1 < *i2);
}
};
void vectorToString(int k, const vector<string> &v, set<set<string>::iterator, cmp> &s);
[/cpp]
[cpp]
class cmp
{
public:
bool operator()(const set<string>::iterator &i1, const set<string>::iterator &i2)
{
return (*i1 < *i2);
}
};
void vectorToString(int k, const vector<string> &v, set<set<string>::iterator, cmp> &s);
[/cpp]
Thanks, really this was the problem. Now I compile the program but I get segment violation. I think it's because the set<string>::iterators get invalidated after an insertion in the set allStrings.
Since the problem statement (10044 - Erdos Numbers) does not specify any boundaries for the input, I thought that the perfect tool to use to solve the problem is STL. On the contrary it turned out a nightmare. Nevertheless I still want to make use of STL in at least one problem that I solved so please help me with any ideas.
Since the problem statement (10044 - Erdos Numbers) does not specify any boundaries for the input, I thought that the perfect tool to use to solve the problem is STL. On the contrary it turned out a nightmare. Nevertheless I still want to make use of STL in at least one problem that I solved so please help me with any ideas.
-
- Learning poster
- Posts: 54
- Joined: Sun Oct 28, 2001 2:00 am
- Location: Bangladesh
-
- Learning poster
- Posts: 54
- Joined: Sun Oct 28, 2001 2:00 am
- Location: Bangladesh
-
- Learning poster
- Posts: 54
- Joined: Sun Oct 28, 2001 2:00 am
- Location: Bangladesh
-
- Learning poster
- Posts: 54
- Joined: Sun Oct 28, 2001 2:00 am
- Location: Bangladesh