fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include<algorithm>
  5. #include <iterator>
  6. using namespace std;
  7.  
  8. int main() {
  9. // your code goes here
  10.  
  11. /*string data[5][5] = { {"A","B","FG","C","D"} ,
  12. {"B","G","D"},
  13. {"B","F","G","AB"},
  14. {"F","AB","C","D"},
  15. {"A","BC","G","F","DE"}
  16. };
  17. int min_support = 2;
  18. set<char> unique_events; // sets hold unique values
  19. map<char, int> unique_support; // map each unique value to it's support
  20.  
  21. for (int row = 0; row < 5; row++) {
  22. for (int col = 0; col < 5; col++) {
  23. for (int s = 0; s < data[row][col].length(); s++) {
  24. // looping over the dataset to insert unique events into the set
  25. unique_events.insert(data[row][col][s]);
  26. }
  27. }
  28. }
  29.  
  30. set<char> ::iterator it;
  31. map<char, int> ::iterator t;
  32. map<char, int> ::iterator loop;
  33.  
  34. // iterating over each event in the set comparing it to the row in the dataset
  35. // (whether it appeared n the row or not) if yes, break from that row incrementing it's support
  36. // in the map
  37. for (it = unique_events.begin(); it != unique_events.end(); it++) {
  38. for (int row = 0; row < 5; row++) {
  39.  
  40. bool found = false;
  41.  
  42. for (int col = 0; col < 5; col++) {
  43. for (int s = 0; s < data[row][col].length(); s++) {
  44.  
  45. if (*it == data[row][col][s]) {
  46. //searching for the event in the map exists or not
  47. t = unique_support.find(data[row][col][s]);
  48. if (t != unique_support.end())
  49. t->second++;
  50. else
  51. unique_support.insert(pair<char,int>(data[row][col][s],1));
  52. found = true;
  53. }
  54. if (found)
  55. break;
  56. }
  57. if (found)
  58. break;
  59. }
  60.  
  61. }
  62. }
  63. /// pruning support
  64. for (t = unique_support.begin(); t != unique_support.end(); ) {
  65. if (t->second < min_support) {
  66. loop = t;
  67. unique_events.erase(t->first);
  68. t++;
  69. unique_support.erase(loop); // if support < min_support , delete from the map
  70.  
  71. }
  72. else
  73. t++;
  74.  
  75. }
  76.  
  77.  
  78. ////// CANDIDATE GENERATION OF SIZE 2
  79. set<string> candidatesSize2;
  80. //string candidatesSize2[100];
  81. set<char> ::iterator item1;
  82. set<char> ::iterator item2;
  83. int i = 0;
  84. for (item1 = unique_events.begin(); item1 != unique_events.end(); item1++) {
  85. for (item2 = unique_events.begin(); item2 != unique_events.end(); item2++) {
  86. string a, b;
  87. a.append(1, *item1);
  88. b.append(1, *item2);
  89. string h = a + ' '+ b;
  90.  
  91. candidatesSize2.insert(h);
  92. }
  93. }
  94.  
  95. for (item1 = unique_events.begin(); item1 != unique_events.end(); item1++) {
  96. it = item1;
  97. for (item2 = ++it; item2 != unique_events.end(); item2++) {
  98. //cout << *it << endl;
  99. //cout << *item1 << " " << *item2 << endl;
  100. string a, b;
  101. a.append(1, *item1);
  102. b.append(1, *item2);
  103. string h = a + b;
  104.  
  105. candidatesSize2.insert(h);
  106. }
  107. }
  108.  
  109. */
  110. set<string> ::iterator item11;
  111. set<string> ::iterator item22;
  112. set<string> candidatesSize3;
  113.  
  114. set<string> candidatesSize2;
  115. candidatesSize2.insert("b b");
  116. candidatesSize2.insert("a b");
  117. candidatesSize2.insert("b c");
  118. candidatesSize2.insert("c b");
  119. candidatesSize2.insert("bd");
  120. candidatesSize2.insert("d b");
  121. candidatesSize2.insert("b e");
  122. candidatesSize2.insert("d c");
  123. candidatesSize2.insert("ce");
  124.  
  125.  
  126.  
  127.  
  128.  
  129. for (item11 = candidatesSize2.begin(); item11 != candidatesSize2.end(); item11++) {
  130. for (item22 = candidatesSize2.begin(); item22 != candidatesSize2.end(); item22++) {
  131. string a, b, c;
  132. string i1=*item11,i2=*item22;
  133. if (i1.size() == 3&& i2.size() == 3)
  134. {
  135. if (i1[2] == i2[0])
  136. {
  137. a.append(1, i1[0]);
  138. b.append(1, i2[0]);
  139. c.append(1, i2[2]);
  140. string h = a + ' ' + b + ' ' + c;
  141. candidatesSize3.insert(h);
  142.  
  143. }
  144. }
  145. else if (i1.size() == 3&& i2.size() == 2)
  146. {
  147. if (i1[2] == i2[0])
  148. {
  149. a.append(1, i1[0]);
  150. b.append(1, i2[0]);
  151. c.append(1, i2[1]);
  152. string h = a + ' ' + b + c;
  153. candidatesSize3.insert(h);
  154.  
  155. }
  156. }
  157. else if (i1.size() == 2&& i2.size() == 3)
  158. {
  159. if (i1[1] == i2[0])
  160. {
  161. a.append(1, i1[0]);
  162. b.append(1, i2[0]);
  163. c.append(1, i2[2]);
  164. string h = a + b + ' ' + c;
  165. candidatesSize3.insert(h);
  166.  
  167. }
  168. }
  169. else
  170. {
  171. if (i1[1] == i2[0])
  172. {
  173. a.append(1, i1[0]);
  174. b.append(1, i2[0]);
  175. c.append(1, i2[1]);
  176. string h = a + b + c;
  177. candidatesSize3.insert(h);
  178.  
  179. }
  180. }
  181.  
  182. }
  183. }
  184.  
  185. set<string> ::iterator sz;
  186. for (sz = candidatesSize3.begin(); sz != candidatesSize3.end(); sz++) {
  187. cout << *sz << endl;
  188. }
  189.  
  190.  
  191. return 0;
  192. }
Success #stdin #stdout 0.01s 5332KB
stdin
Standard input is empty
stdout
a b b
a b c
a b e
a bd
b b b
b b c
b b e
b bd
b c b
b ce
bd b
bd c
c b b
c b c
c b e
c bd
d b b
d b c
d b e
d bd
d c b
d ce