fork(1) download
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5. #define structure map<vector<int>, int>
  6. #define FOR_MAP(ii,T) for(structure::iterator (ii)=(T).begin();(ii)!=(T).end();(ii)++)
  7. #define FOR_next_MAP(jj,ii,T) for(structure::iterator (jj)=(ii);(jj)!=(T).end();(jj)++)
  8. #define VI vector<int>
  9.  
  10. const int MIN_SUP = 2;
  11.  
  12. structure C;
  13. structure L;
  14.  
  15. void C1();
  16. void L1();
  17. void generate_C();
  18. void generate_L();
  19. void output(structure );
  20. void scan_D();
  21. void prune();
  22. bool check_compatibility(VI ,VI );
  23. void set_count(VI );
  24.  
  25. int main(int argc, char const *argv[])
  26. {
  27. C.clear();
  28. L.clear();
  29.  
  30. bool mv=true;
  31. int index=2;
  32. while(true)
  33. {
  34. if (mv)
  35. {
  36. C1();
  37. cout<<"C1\n";
  38. output(C);
  39.  
  40. L1();
  41. cout<<"L1\n";
  42. output(L);
  43.  
  44. mv=!mv;
  45. }else
  46. {
  47. generate_C();
  48. if(C.size()==0)
  49. break;
  50. cout<<"\nC"<<index<<"\n";
  51. output(C);
  52. prune();
  53. if (C.size()==0)
  54. {
  55. break;
  56. }
  57. cout<<"\nC"<<index<<" after prune \n";
  58. output(C);
  59. scan_D();
  60. cout<<"\nC"<<index<<"after scaning dataset \n";
  61. output(C);
  62. generate_L();
  63. if (L.size()==0)
  64. {
  65. break;
  66. }
  67. cout<<"\nL"<<index<<"\n";
  68. output(L);
  69. index++;
  70. }
  71. }
  72. return 0;
  73. }
  74.  
  75.  
  76. void C1()
  77. {
  78. ifstream fin;
  79. fin.open("input.txt");
  80. if(!fin)
  81. {
  82. cout<<"Input file opening error\n";
  83. exit(0);
  84. }
  85.  
  86. int n;
  87. VI v;
  88. while(fin>>n)
  89. {
  90. v.clear();
  91. if (n==-1)
  92. {
  93. continue;
  94. }
  95. v.push_back(n);
  96. if(C.count(v)>0)
  97. C[v]++;
  98. else
  99. C[v]=1;
  100. }
  101. fin.close();
  102. }
  103.  
  104. void output(structure T)
  105. {
  106. cout<<"\n";
  107. VI v;
  108. FOR_MAP(ii,T)
  109. {
  110. v.clear();
  111. v=ii->first;
  112. for (int i = 0; i < v.size(); ++i)
  113. {
  114. cout<<v[i]<<" ";
  115. }
  116.  
  117. cout<<" ---(frequency)----->> "<<ii->second;
  118. cout<<"\n";
  119.  
  120. }
  121. }
  122.  
  123. void L1()
  124. {
  125.  
  126. FOR_MAP(ii,C)
  127. {
  128. if (ii->second >= MIN_SUP)
  129. {
  130. L[ii->first]=ii->second;
  131. }
  132. }
  133.  
  134. }
  135.  
  136. void generate_C()
  137. {
  138. //clean(C);
  139. C.clear();
  140. FOR_MAP(ii,L)
  141. {
  142.  
  143. FOR_next_MAP(jj,ii,L)
  144. {
  145. if(jj==ii)
  146. continue;
  147. VI a,b;
  148. a.clear();
  149. b.clear();
  150. a=ii->first;
  151. b=jj->first;
  152. if(check_compatibility(a,b))
  153. {
  154. a.push_back(b.back());
  155. sort(a.begin(), a.end());
  156. C[a]=0;
  157. }
  158. }
  159.  
  160. }
  161.  
  162.  
  163. }
  164.  
  165. bool check_compatibility(VI a,VI b)
  166. {
  167. bool compatible=true;
  168. for (int i = 0; i < a.size()-1; ++i)
  169. {
  170. if (a[i]!=b[i])
  171. {
  172. compatible=false;
  173. break;
  174. }
  175. }
  176.  
  177. return compatible;
  178. }
  179.  
  180. void prune()
  181. {
  182. VI a,b;
  183.  
  184. FOR_MAP(ii,C)
  185. {
  186. a.clear();
  187. b.clear();
  188.  
  189. a=ii->first;
  190. for(int i = 0;i<a.size();i++)
  191. {
  192. b.clear();
  193. for (int j = 0; j < a.size(); ++j)
  194. {
  195. if(j==i)
  196. continue;
  197. b.push_back(a[j]);
  198. }
  199. if(L.find(b)==L.end())
  200. {
  201. ii->second=-1;
  202. break;
  203. }
  204.  
  205. }
  206.  
  207. }
  208. structure temp;
  209. temp.clear();
  210. FOR_MAP(ii,C)
  211. {
  212. if (ii->second != -1)
  213. {
  214. temp[ii->first]=ii->second;
  215. }
  216. }
  217. C.clear();
  218. C=temp;
  219. temp.clear();
  220. }
  221. void scan_D()
  222. {
  223. ifstream fin;
  224. fin.open("input.txt");
  225. if(!fin)
  226. {
  227. cout<<"Input file opening error\n";
  228. exit(0);
  229. }
  230. int n;
  231. VI a;
  232. while(fin>>n)
  233. {
  234. if(n==-1 && a.size()>0)
  235. {
  236. set_count(a);
  237. a.clear();
  238. }else if(n!=-1)
  239. {
  240. a.push_back(n);
  241. }
  242.  
  243. }
  244. fin.close();
  245. }
  246. void set_count(VI a)
  247. {
  248. FOR_MAP(ii,C)
  249. {
  250. VI b;
  251. b.clear();
  252. b=ii->first;
  253. int true_count=0;
  254. if (b.size()<=a.size())
  255. {
  256. for (int i = 0; i < b.size(); ++i)
  257. {
  258. for (int j = 0; j < a.size(); ++j)
  259. {
  260. if(b[i]==a[j])
  261. {
  262. true_count++;
  263. break;
  264. }
  265. }
  266. }
  267. }
  268.  
  269. if (true_count==b.size())
  270. {
  271. ii->second++;
  272. }
  273. }
  274. }
  275.  
  276. void generate_L()
  277. {
  278. L.clear();
  279. FOR_MAP(ii,C)
  280. {
  281. if(ii->second >= MIN_SUP)
  282. {
  283. L[ii->first]=ii->second;
  284. }
  285. }
  286. }
  287.  
  288.  
  289.  
Success #stdin #stdout 0s 3488KB
stdin
1 2 5 -1
2 4 -1
2 3 -1
1 2 4 -1
1 3 -1
2 3 -1
1 3 -1
1 2 3 5 -1
1 2 3 -1
stdout
Input file opening error