Advertisement
ElenaR1

Podgotovka za kontrolno-8 gr

Dec 14th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. struct TNode
  10. {
  11.     string name;
  12.     vector<TNode*> children;
  13.     int level;
  14.     TNode(string name, int level = 0) :name(name), level(level) {}
  15. };
  16.  
  17. struct State
  18. {
  19.     TNode* node;
  20.     string currentFamily;
  21.     State(TNode* node, string name) :node(node), currentFamily(name) {};
  22. };
  23.  
  24. //клас Таксономия описва таксономията на животните. Всяко ниво на таксономията съответства на ниво в дърво.
  25. //Върховете на разст 3 представляват разред, а на 6 - видове.
  26.  
  27. class Taxonomy
  28. {
  29. private:
  30.     TNode* root;
  31.     void printRec(TNode* node, vector<string>& path) const;
  32.     int countNodes(bool(*p)(TNode* node), TNode* node);
  33.     int countSpeciesRec(string familyName, TNode* node);
  34. public:
  35.     Taxonomy()
  36.     {
  37.         root = new TNode("Animalia");
  38.     }
  39.     void addEntity(vector<string> taxonomy);
  40.     TNode* current = root;
  41.     /*for (vector<string>::iterator it = Taxonomy.begin(); it != Taxonomy.end();++it)
  42.     {
  43.         TNode* existing = findChild(current, *it);
  44.         if (!existing)
  45.         {
  46.             existing = addChild(current, *it);
  47.         }
  48.         current = existing;
  49.     }*/
  50.     TNode* findChild(TNode* node, string name);
  51.     TNode* addChild(TNode* node, string name);
  52.     void print() const;
  53.     int countSpeciesInFamily(string familyName);
  54.     bool isLeafComplexName(TNode* node);
  55.     bool isSpeciesLeaf(TNode* node)
  56.     {return node->children.empty() && node->level >= 6;}
  57.     map<string, int> speciesPerFamily()
  58.     {
  59.         map<string, int> result;
  60.         speciesPerFamilyRec(result, root," ");
  61.         return result;
  62.     }
  63.     void speciesPerFamilyRec(map<string, int>& results, TNode* node,string);
  64.     map<string, int> speciesPerFamilyStack();
  65.     vector<string> listSpecies();
  66.     void listSpeciesRec(TNode* node, vector<string>& res);
  67.     vector<string> listSpeciesStack();
  68.         //int countComplexNamedSpecies();
  69. };
  70.  
  71. TNode* findChild(TNode * node, string name)
  72. {
  73.     for (int i = 0; i < node->children.size(); i++)
  74.     {
  75.         if (node->children[i]->name == name) {
  76.             return node->children[i];
  77.         }
  78.     }
  79.     return NULL;
  80. }
  81.  
  82. TNode * addChild(TNode * node, string name)
  83. {
  84.     TNode* child = new TNode(name, node->level + 1);
  85.     node->children.push_back(child);
  86.     return child;
  87. }
  88.  
  89. int Taxonomy::countNodes(bool(*p)(TNode *node), TNode * node)
  90. {
  91.     int count = 0;
  92.     if(p(node))
  93.         count++;
  94.     for (int i = 0; node->children.size() > i; i++)
  95.         count += countNodes(p, node->children[i]);
  96.     return count;
  97. }
  98.  
  99. int Taxonomy::countSpeciesRec(string familyName, TNode * node)
  100. {
  101.     if (node->level == 4)
  102.     {
  103.         if (node->name != familyName)
  104.             return 0;
  105.         else
  106.             return countNodes(isSpeciesLeaf(), node);
  107.     }
  108.     else if(node->level<4)
  109.     {
  110.         int count = 0;
  111.         for (int i = 0; i < node->children.size(); i++)
  112.             count += countSpeciesRec(familyName, node->children[i]);
  113.         return count;
  114.     }
  115.     else
  116.     {
  117.         return 0;
  118.     }
  119.    
  120.  
  121. }
  122.  
  123. int Taxonomy::countSpeciesInFamily(string familyName)
  124. {
  125.     return countSpeciesRec(familyName, root);
  126. }
  127.  
  128. bool isLeafComplexName(TNode * node)
  129. {
  130.     return node->children.empty() && find(node->name, " ") != string::npos;
  131. }
  132.  
  133. void Taxonomy::speciesPerFamilyRec(map<string, int>& results, TNode * node,string currFamily)
  134. {
  135.     if (node->level == 4)
  136.         currFamily = node->name;
  137.     if (isSpeciesLeaf(node))
  138.     {
  139.         results[currFamily] = results[currFamily] + 1;
  140.     }
  141.     for (int i = 0; i < node->children.size(); i++)
  142.     {
  143.         speciesPerFamilyRec(results, child,currFamily);
  144.     }
  145. }
  146.  
  147. map<string, int> Taxonomy::speciesPerFamilyStack()
  148. {
  149.     map<string, int> results;
  150.     stack<State> operations;
  151.     operations.push(State(root, " "));
  152.     while (!operations.empty()) //Едно извикване на рекурсивната функция
  153.     {
  154.         State state = operations.top();
  155.         operations.pop();
  156.         if (isSpeciesLeaf(state.node))
  157.             results[state.currentFamily] += 1;
  158.         string nextFamily = state.currentFamily;
  159.         if (state.node->level == 4)
  160.             nextFamily = state.node->name;
  161.         for (int i = 0; i < state.node->children.size(); i++)
  162.         {
  163.             operations.push(State(state.node->children[i], nextFamily));
  164.         }
  165.     }
  166.    
  167.     return results;
  168. }
  169.  
  170. void Taxonomy::listSpeciesRec(TNode * node, vector<string>& res)
  171. {
  172.     if (isSpeciesLeaf(node))
  173.         res.push_back(node->name);
  174.     else
  175.         for (int i = 0; i < node->children.size(); i++)
  176.             listSpeciesRec(node->children[i], res);
  177. }
  178.  
  179. vector<string> Taxonomy::listSpeciesStack()
  180. {
  181.     vector<string> result;
  182.     stack<TNode*> operations;
  183.     operations.push(root);
  184.     while(!operations.empty())
  185.     {
  186.         TNode* top = operations.top();
  187.         operations.pop();
  188.         if (isSpeciesLeaf(top))
  189.         {
  190.             result.push_back(top->name);
  191.         }
  192.         else
  193.             for (int i = 0; i < top->children.size(); i++)
  194.                 operations.push(top->children[i]);
  195.     }
  196. }
  197.  
  198. vector<string> Taxonomy::listSpecies()
  199. {
  200.     vector<string> results;
  201.     listSpeciesRec(root, results);
  202.     return results;
  203. }
  204.  
  205. void Taxonomy::print() const
  206. {
  207.     vector<string> path;
  208.     printRec(root, path);
  209. }
  210.  
  211.  
  212.  
  213. void Taxonomy::printRec(TNode * node, vector<string>& path) const
  214. {
  215.     if (node->children.empty())
  216.     {
  217.         for (vector<string>::iterator it = path.begin(); it != path.end(); ++it)
  218.             cout << *it << "->";
  219.         cout << node->name << endl;
  220.     }
  221.     else
  222.     {
  223.         path.push_back(node->name);
  224.         for (vector<TNode*>::iterator it = node->children.begin(); it != node->children.end(); ++it)
  225.             printRec(*it, path);
  226.     }
  227.     path.pop_back();
  228. }
  229.  
  230.  
  231.  
  232. #include <iostream>
  233. #include <vector>
  234. #include<stack>
  235.  
  236. using namespace std;
  237.  
  238. void generatePerms(char characters[],int size,vector<char*>& results,char* perm, bool* used,int index) {
  239.     if (index == size) {
  240.         char* ready = new char[size + 1];
  241.         perm[size] = 0;
  242.         strcpy(ready, perm);
  243.         results.push_back(ready);
  244.     }
  245.     for (int i = 0; i < size; i++) {
  246.         if (!used[i]) {
  247.             used[i] = true;
  248.             perm[index] = characters[i];
  249.             generatePerms(characters, size, results, perm, used, index + 1);
  250.             used[i] = false;
  251.         }
  252.     }
  253. }
  254.  
  255. vector<char*> permute(char characters[]) {
  256.     vector<char*> results;
  257.     int numChars = strlen(characters);
  258.     bool* used = new bool[numChars];
  259.     char* permutation = new char[numChars + 1];
  260.     for (int i = 0; i < numChars; i++) {
  261.         used[i] = false;
  262.         permutation[i] = 0;
  263.     }
  264.     generatePerms(characters, numChars, results, permutation, used, 0);
  265.     return results;
  266. }
  267.  
  268. struct PermStep {
  269.     char* perm;
  270.     bool* used;
  271.     int index;
  272.     PermStep(char* perm, bool* used,int size,int idx) {
  273.         this->perm = new char[size + 1];
  274.         this->used = new bool[size];
  275.         for (int i = 0; i < size; i++) {
  276.             this->used[i] = used[i];
  277.             this->perm[i] = perm[i];
  278.         }
  279.         index = idx;
  280.     }
  281.     ~PermStep() {
  282.         delete[]used;
  283.         delete[]perm;
  284.     }
  285. };
  286. vector<char*> permuteStack(char characters[]) {
  287.     vector<char*> results;
  288.     int numChars = strlen(characters);
  289.     bool* used = new bool[numChars];
  290.     char* permutation = new char[numChars + 1];
  291.     for (int i = 0; i < numChars; i++) {
  292.         used[i] = false;
  293.         permutation[i] = 0;
  294.     }
  295.     stack<PermStep> operations;
  296.     operations.push(PermStep(permutation, used, numChars, 0));
  297.     while (!operations.empty()) {
  298.         PermStep top = operations.top();
  299.         operations.pop();
  300.         if (top.index == numChars) {
  301.             char* ready = new char[numChars + 1];
  302.             top.perm[numChars] = 0;
  303.             strcpy(ready, top.perm);
  304.             results.push_back(ready);
  305.         }
  306.         else {
  307.             for (int i = 0; i < numChars; i++) {
  308.                 if (!top.used[i]) {
  309.                     top.used[i] = true;
  310.                     top.perm[top.index] = characters[i];
  311.                     operations.push(PermStep(top.perm,top.used,numChars,top.index+1));
  312.                     top.used[i] = false;
  313.                 }
  314.             }
  315.         }
  316.  
  317.     }
  318.     return results;
  319. }
  320.  
  321. int main()
  322. {
  323.     return 0;
  324. }
  325.  
  326. //PO MOI NAHCIN 1VA
  327. #include <stdio.h>
  328. #include <string.h>
  329. #include <string>
  330. #include <vector>
  331. #include <stack>
  332. #include <iostream>
  333. //#include <bits/stdc++.h>
  334.  
  335. using namespace std;
  336.  
  337. void swap(char &x, char &y)
  338. {
  339.     char temp;
  340.     temp = x;
  341.     x = y;
  342.     y = temp;
  343. }
  344. void permute(string a, int l, int r, vector<string> &v)
  345. {
  346.     if (l == r)//there aren't any letters left to be permuted
  347.     {
  348.         //cout << a;
  349.         v.push_back(a);
  350.     }
  351.     else
  352.     {
  353.         for (int i = l; i <= r; i++)
  354.         {
  355.             swap(a[l], a[i]);
  356.             permute(a, l + 1, r, v);
  357.             swap(a[l], a[i]); //backtrack
  358.         }
  359.     }
  360. }
  361.  
  362. #define OPER_PRINT 0
  363. #define OPER_TRAV 1
  364. struct Operation {
  365.     int operType;
  366.     string a;
  367.     int l, r;
  368.     Operation(string s,int t,  int ll, int rr) :operType(t), a(s), l(ll), r(rr){}
  369. };
  370.  
  371. bool isUsed(string a)
  372. {
  373.     return true;
  374. }
  375. void permStack(string a, int l, int r)
  376. {
  377.     stack <Operation> s;
  378.     s.push(Operation(a, OPER_TRAV, l, r));
  379.     while (!s.empty())
  380.     {
  381.         Operation topOperation = s.top();
  382.         string a = topOperation.a;
  383.         int l = topOperation.l;
  384.         int r = topOperation.r;
  385.         s.pop();
  386.         if (l == r)
  387.         {
  388.             cout << a;
  389.         //  swap(a[l], a[i]);
  390.         }
  391.         else
  392.         {
  393.             for (size_t i = l; i <=l; i++)
  394.             {
  395.                 if(!used(a))
  396.                 swap(a[l], a[i]);
  397.                 s.push(Operation(a, OPER_TRAV, l+1, r));
  398.             }
  399.         }
  400.     }
  401. }
  402.  
  403. void printV(vector <string> v)
  404. {
  405.     for (size_t i = 0; i < v.size(); i++)
  406.     {
  407.         cout << v[i] << " ";
  408.     }
  409.     cout << endl;
  410. }
  411.  
  412.  
  413.  
  414. int main()
  415. {
  416.     //char str[] = "ABC";
  417.     //int n = strlen(str);
  418.     string str1 = "ABC";
  419.     vector<string> v;
  420.     int n = str1.size();
  421.     permute(str1, 0, n - 1, v);
  422.     printV(v);
  423.     cout << endl;
  424.     permStack(str1,0,n-1);
  425.  
  426.     /*string str = "ABCD";
  427.  
  428.     // Rotating arround second element
  429.     // string becomes "BCDA"
  430.     rotate(str.begin(), str.begin() + 1, str.end());
  431.     cout << str << endl;
  432.  
  433.     // Again rotating around second element
  434.     // string becomes "CDAB"
  435.     rotate(str.begin(), str.begin() + 1, str.end());
  436.     cout << str;
  437.     */
  438.     return 0;
  439. }
  440.  
  441. #include <stdio.h>
  442. #include <string.h>
  443. #include <string>
  444. #include <vector>
  445. #include <stack>
  446. #include <iostream>
  447. //#include <bits/stdc++.h>
  448.  
  449. using namespace std;
  450.  
  451. void swap(char &x, char &y)
  452. {
  453.     char temp;
  454.     temp = x;
  455.     x = y;
  456.     y = temp;
  457. }
  458. /*void generatePerms(char characters[], int size, vector<char*>& results, char* perm, bool* used, int index) {
  459.     if (index == size) {
  460.         char* ready = new char[size + 1];
  461.         perm[size] = 0;
  462.         strcpy(ready, perm);
  463.         results.push_back(ready);
  464.     }
  465.     for (int i = 0; i < size; i++) {
  466.         if (!used[i]) {
  467.             used[i] = true;
  468.             perm[index] = characters[i];
  469.             generatePerms(characters, size, results, perm, used, index + 1);
  470.             used[i] = false;
  471.         }
  472.     }
  473. }*/
  474. struct PermStep {
  475.     string perm;
  476.     bool* used;
  477.     int index;
  478.     PermStep(string perm, bool* used, int size, int idx) {
  479.         this->perm = perm;
  480.         this->used = new bool[size];
  481.         for (int i = 0; i < size; i++) {
  482.             this->used[i] = used[i];
  483.             this->perm[i] = perm[i];
  484.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement