Решил чуть отложить вторую и решить задачу на технику (она же задача А), как уже говорил, отсюда: http://narod.ru/disk/33108589001/task.zip.html. Пока написал крутой класс и формат "find" в нём. В общем там дерево хорошее главное написать. Можно и плохое, но позориться не хочется. Так что пишу красиво более-менее.
Вот так круто работает(вводит и выводит то же самое XD если серьёзно - это хорошо):
А вот и код:
#include <iostream>
#include <string>
#include <set>
#include <map>
using namespace std;
class vertex
{
private:
string name;
unsigned long id;
vertex* parent;
public:
map <unsigned long, vertex*> childs;
map < string, unsigned long > identifiers;
vertex(vertex* _parent = NULL)
:parent(_parent)
{}
vertex(unsigned long _id, vertex* _parent = NULL)
:
id(_id),
parent(_parent)
{}
vertex(string _name, vertex* _parent = NULL)
:
name(_name),
parent(_parent)
{}
vertex(string _name, unsigned long _id, vertex* _parent = NULL)
:
name(_name),
id(_id),
parent(_parent)
{}
bool operator <(const vertex& operand) const
{
return (GetId() < operand.GetId());
}
void SetName(const string& new_name)
{
name = new_name;
}
void SetId(const unsigned long& new_id)
{
id = new_id;
}
string GetName() const
{
return name;
}
unsigned long GetId() const
{
return id;
}
void AddChild(vertex* new_child)
{
identifiers[new_child->GetName()] = new_child->GetId();
childs[new_child->GetId()] = new_child;
}
vertex* NextChild(string _name)
{
return childs[ identifiers[_name] ];
}
void SetParent(vertex * _parent)
{
parent = _parent;
}
vertex* GetParent()
{
return parent;
}
};
class Dir
{
private:
vertex *root;
int size;
public:
Dir()
{
root = NULL;
size = 0;
}
int GetSize()
{
return size;
}
void SetSize(int new_size)
{
size = new_size;
}
void Read(string format)
{
if (format == "find")
{
ReadInFind();
}
if (format == "python")
{
ReadInPython();
}
if (format == "acm1")
{
ReadInAcm1();
}
if (format == "acm2")
{
ReadInAcm1();
}
if (format == "acm3")
{
ReadInAcm1();
}
if (format == "xml")
{
ReadInXML();
}
}
void Write(string format)
{
if (format == "find")
{
WriteInFind(root);
}
if (format == "python")
{
WriteInPython();
}
if (format == "acm1")
{
WriteInAcm1();
}
if (format == "acm2")
{
WriteInAcm1();
}
if (format == "acm3")
{
WriteInAcm1();
}
if (format == "xml")
{
WriteInXML();
}
if (format == "easy")
{
WriteEasy(root);
}
}
void ReadInFind();
void ReadInPython(){}
void ReadInAcm1(){}
void ReadInAcm2(){}
void ReadInAcm3(){}
void ReadInXML(){}
void WriteInFind(vertex*);
void WriteInPython(){}
void WriteInAcm1(){}
void WriteInAcm2(){}
void WriteInAcm3(){}
void WriteInXML(){}
void WriteEasy(vertex*, int level = -1);
void AddChildInFind(vertex* parent, string path, unsigned long & id);
};
void Dir::WriteEasy(vertex* parent, int level)
{
for (int i = 0; i < level; i++)
{
cout << " ";
}
if (parent->GetName() != "")
{
cout << parent->GetName() << ' ' << parent->GetId() << endl;
}
for (map <unsigned long, vertex*>::iterator i = parent->childs.begin(); i != parent->childs.end(); i++)
{
WriteEasy(i->second, level+1);
}
}
void Dir::WriteInFind(vertex* parent)
{
vertex* tmp = parent;
string out;
while (tmp != NULL)
{
if (out == "" || tmp == root)
{
out = out + tmp->GetName();
}
else
{
out = tmp->GetName() + "/" + out;
}
tmp = tmp->GetParent();
}
if (parent != root)
{
cout << out << " " << parent->GetId() << endl;
}
else
{
cout << GetSize() << endl;
}
for (map <unsigned long, vertex*>::iterator i = parent->childs.begin(); i != parent->childs.end(); i++)
{
WriteInFind(i->second);
}
}
void Dir::ReadInFind()
{
int n;
cin >> n;
SetSize(n);
string path;
unsigned long id;
if (root != NULL)
{
delete root;
}
root = new vertex("", (unsigned long)(-1));
for (int i = 0; i < n; i++)
{
cin >> path >> id;
AddChildInFind(root, path, id);
}
}
void Dir::AddChildInFind(vertex* parent, string path, unsigned long & id)
{
size_t pos = path.find('/');
if (pos == string::npos)
{
parent->AddChild(new vertex(path, id, parent));
return;
}
string new_path(path, pos+1, path.length()-pos-1);
string child_name(path, 0, pos);
AddChildInFind(parent->NextChild(child_name), new_path, id);
}
int main()
{
Dir a;
string in_format;
string out_format;
cin >> in_format;
cin >> out_format;
a.Read(in_format);
a.Write(out_format);
system("pause");
}
Вот так круто работает(вводит и выводит то же самое XD если серьёзно - это хорошо):
А вот и код:
#include <iostream>
#include <string>
#include <set>
#include <map>
using namespace std;
class vertex
{
private:
string name;
unsigned long id;
vertex* parent;
public:
map <unsigned long, vertex*> childs;
map < string, unsigned long > identifiers;
vertex(vertex* _parent = NULL)
:parent(_parent)
{}
vertex(unsigned long _id, vertex* _parent = NULL)
:
id(_id),
parent(_parent)
{}
vertex(string _name, vertex* _parent = NULL)
:
name(_name),
parent(_parent)
{}
vertex(string _name, unsigned long _id, vertex* _parent = NULL)
:
name(_name),
id(_id),
parent(_parent)
{}
bool operator <(const vertex& operand) const
{
return (GetId() < operand.GetId());
}
void SetName(const string& new_name)
{
name = new_name;
}
void SetId(const unsigned long& new_id)
{
id = new_id;
}
string GetName() const
{
return name;
}
unsigned long GetId() const
{
return id;
}
void AddChild(vertex* new_child)
{
identifiers[new_child->GetName()] = new_child->GetId();
childs[new_child->GetId()] = new_child;
}
vertex* NextChild(string _name)
{
return childs[ identifiers[_name] ];
}
void SetParent(vertex * _parent)
{
parent = _parent;
}
vertex* GetParent()
{
return parent;
}
};
class Dir
{
private:
vertex *root;
int size;
public:
Dir()
{
root = NULL;
size = 0;
}
int GetSize()
{
return size;
}
void SetSize(int new_size)
{
size = new_size;
}
void Read(string format)
{
if (format == "find")
{
ReadInFind();
}
if (format == "python")
{
ReadInPython();
}
if (format == "acm1")
{
ReadInAcm1();
}
if (format == "acm2")
{
ReadInAcm1();
}
if (format == "acm3")
{
ReadInAcm1();
}
if (format == "xml")
{
ReadInXML();
}
}
void Write(string format)
{
if (format == "find")
{
WriteInFind(root);
}
if (format == "python")
{
WriteInPython();
}
if (format == "acm1")
{
WriteInAcm1();
}
if (format == "acm2")
{
WriteInAcm1();
}
if (format == "acm3")
{
WriteInAcm1();
}
if (format == "xml")
{
WriteInXML();
}
if (format == "easy")
{
WriteEasy(root);
}
}
void ReadInFind();
void ReadInPython(){}
void ReadInAcm1(){}
void ReadInAcm2(){}
void ReadInAcm3(){}
void ReadInXML(){}
void WriteInFind(vertex*);
void WriteInPython(){}
void WriteInAcm1(){}
void WriteInAcm2(){}
void WriteInAcm3(){}
void WriteInXML(){}
void WriteEasy(vertex*, int level = -1);
void AddChildInFind(vertex* parent, string path, unsigned long & id);
};
void Dir::WriteEasy(vertex* parent, int level)
{
for (int i = 0; i < level; i++)
{
cout << " ";
}
if (parent->GetName() != "")
{
cout << parent->GetName() << ' ' << parent->GetId() << endl;
}
for (map <unsigned long, vertex*>::iterator i = parent->childs.begin(); i != parent->childs.end(); i++)
{
WriteEasy(i->second, level+1);
}
}
void Dir::WriteInFind(vertex* parent)
{
vertex* tmp = parent;
string out;
while (tmp != NULL)
{
if (out == "" || tmp == root)
{
out = out + tmp->GetName();
}
else
{
out = tmp->GetName() + "/" + out;
}
tmp = tmp->GetParent();
}
if (parent != root)
{
cout << out << " " << parent->GetId() << endl;
}
else
{
cout << GetSize() << endl;
}
for (map <unsigned long, vertex*>::iterator i = parent->childs.begin(); i != parent->childs.end(); i++)
{
WriteInFind(i->second);
}
}
void Dir::ReadInFind()
{
int n;
cin >> n;
SetSize(n);
string path;
unsigned long id;
if (root != NULL)
{
delete root;
}
root = new vertex("", (unsigned long)(-1));
for (int i = 0; i < n; i++)
{
cin >> path >> id;
AddChildInFind(root, path, id);
}
}
void Dir::AddChildInFind(vertex* parent, string path, unsigned long & id)
{
size_t pos = path.find('/');
if (pos == string::npos)
{
parent->AddChild(new vertex(path, id, parent));
return;
}
string new_path(path, pos+1, path.length()-pos-1);
string child_name(path, 0, pos);
AddChildInFind(parent->NextChild(child_name), new_path, id);
}
int main()
{
Dir a;
string in_format;
string out_format;
cin >> in_format;
cin >> out_format;
a.Read(in_format);
a.Write(out_format);
system("pause");
}

Ооо, Ярька молодец) Такой красивый код для вывода того, что введено :D
ОтветитьУдалитькрасавчик!)))
ОтветитьУдалить