Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Task on Задача C. Белочка
//Title of this code #include <iostream> #include <string> #include <vector> #include <sstream> #include <set> #include <map> #include <assert.h> const std::string g_fname = "file.txt"; const std::string g_belka_in = "5\n" "stepan1\n" "vasiliy4\n" "stepan4\n" "stepan3\n" "vladimir1000000\n" "3\n" "stepan\n" "vladimir\n" "stepan\n"; #if 0 #define DUMP(__x) { std::cout << #__x << " " << __x << std::endl; } #else #define DUMP(__x) #endif typedef std::set<int> BelkaTree; typedef std::map<std::string, BelkaTree*> BelkaForest; void ReadBelkaTree(const std::string& FileName, BelkaForest& out) { // clear forest out.clear(); // prepare stream with input file std::stringstream ss(g_belka_in); char buf[256]; // obtain number of lines with tree info ss.getline(buf, 256); int nlineWithTrees = atoi(buf); DUMP(nlineWithTrees); // iterate over line with tree info for(int i=0;i<nlineWithTrees;++i) { ss.getline(buf, 256); std::string line(buf); // extract from line tree name // extract from line hole coord std::string treeName; std::string strHoleCoord; for ( std::string::const_iterator it=line.begin(); it!=line.end(); ++it) { if( *it >= 'a' && *it <= 'z' ) treeName += *it; else if(*it >= '0' && *it <= '9') strHoleCoord += *it; } // cvt to int int holeCoord = atoi( strHoleCoord.c_str() ); // if no such tree with this name - create it if(out.find(treeName) == out.end()) { out.insert(make_pair(treeName, new BelkaTree)); } // insert hole in tree out[treeName]->insert(holeCoord); DUMP(line); DUMP(treeName); DUMP(strHoleCoord); DUMP(holeCoord); } { // obtain number of lines with tree info ss.getline(buf, 256); int nlineWithNewComers = atoi(buf); DUMP(nlineWithNewComers); // iterate over line with tree info for(int i=0;i<nlineWithNewComers;++i) { // get tree Number char buf[256]; ss.getline(buf, 256); std::string treeName(buf); assert(out.find(treeName) != out.end() && "tree with name not found"); BelkaTree& tree(*out[treeName]); DUMP(treeName); int num2Insert = -1; // find minimal which has next item free int numcnt = 1, prevnum = 1; for(auto it = tree.begin(); it != tree.end(); ++it, ++numcnt){ DUMP(*it); DUMP(numcnt); DUMP(prevnum); // if there is no 1 in set - then start with it if(it == tree.begin() && *it > 1) { num2Insert = 1; break; } // if element counter is different from current number in set // then insert previous element number + 1. there was a gap a if(*it != numcnt){ num2Insert = prevnum + 1; break; } prevnum = *it; } num2Insert = (num2Insert != -1) ? num2Insert : prevnum + 1; DUMP(num2Insert); assert(tree.find(num2Insert) == tree.end() && "Inserting duplicate"); tree.insert(num2Insert); std::cout << "Inserted " << num2Insert << std::endl; } } } int main() { BelkaForest bf; // read and init the table with trees and holes ReadBelkaTree(g_fname, bf); std::cout << "Hello, world!\n"; return 0; }
run
|
edit
|
history
|
help
0
Example
virtual members
PrintAddress
numeric_limits
non-template template parameters for container stream insertion: SFINAE
Math1
001
auto Keyword Example
Non type template argument
isnan_constexpr