Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <cstring>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10.  
  11. struct wordAndCount {
  12. int counter;
  13. string word;
  14. };
  15.  
  16. void print_strings(vector<string> *array);
  17. void prepare_array(vector<string> *, vector<string> *);
  18. void split(string ,vector<string> *, vector<string> *);
  19. bool found_in_string(string, vector<string> *);
  20. void preparation(vector<string> *, vector<wordAndCount> *);
  21. int found(string , vector<wordAndCount> *);
  22. void printit(vector<wordAndCount> *);
  23. void quicksort(vector<wordAndCount> *, int, int);
  24. void quicksort_string(vector<wordAndCount> *);
  25. void quicksort_final(vector<wordAndCount> *, int, int);
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29. int forb_result;
  30. int length_result;
  31. string aux;
  32. string forb;
  33. string length;
  34. vector<string> *phrases = new vector<string>;
  35. vector<string> *forbbidens = new vector<string>;
  36. stringstream convert;
  37.  
  38. getline(cin, forb);
  39. convert << forb;
  40. convert >> forb_result;
  41. for(int i = 0; i < forb_result; ++i) {
  42. getline(cin, aux);
  43. std::transform(aux.begin(), aux.end(), aux.begin(), ::tolower);
  44. forbbidens->push_back(aux);
  45. }
  46. convert.str("");
  47. convert.clear();
  48.  
  49. getline(cin, length);
  50. convert << length;
  51. convert >> length_result;
  52. for(int i = 0; i < length_result; ++i) {
  53. getline(cin, aux);
  54. std::transform(aux.begin(), aux.end(), aux.begin(), ::tolower);
  55. phrases->push_back(aux);
  56. }
  57. convert.str("");
  58. convert.clear();
  59.  
  60. prepare_array(forbbidens, phrases);
  61.  
  62. return 0;
  63. }
  64.  
  65. void prepare_array(vector<string> *forbidden, vector<string> *phrases) {
  66. string phrase;
  67. vector<wordAndCount> *result = new vector<wordAndCount>;
  68. vector<string> *phrase_array;
  69.  
  70. for(int i = 0; i < phrases->size(); i++) {
  71. phrase_array = new vector<string>;
  72. phrase = phrases->at(i);
  73. split(phrase, phrase_array, forbidden);
  74. preparation(phrase_array, result);
  75. }
  76. quicksort(result, 0, result->size() - 1);
  77. quicksort_string(result);
  78. printit(result);
  79. }
  80.  
  81. void split(string phrase,vector<string> *phrase_array, vector<string> *forbidden) {
  82. string letter;
  83. string word("");
  84. vector<char> v(phrase.size() + 1);
  85. memcpy(&v.front(), phrase.c_str(), phrase.size() + 1);
  86. char *cp = v.data();
  87. char *worker;
  88.  
  89. worker = strtok(cp, " ,.;:?!()-_@#$%&*+{}");
  90. while(worker != NULL) {
  91. word = worker;
  92. if(found_in_string(word, forbidden) == false) {
  93. phrase_array->push_back(word);
  94. }
  95. worker = strtok(NULL, " ,.;:?!()-_@#$%&*+{}");
  96. }
  97. }
  98.  
  99. void preparation(vector<string> *phrase_array, vector<wordAndCount> *result) {
  100. int pos;
  101. wordAndCount index;
  102.  
  103. for(int i = 0; i < phrase_array->size(); ++i) {
  104. if((pos = found((*phrase_array).at(i), result)) != -1) {
  105. (*result).at(pos).counter++;
  106. } else {
  107. index.counter = 1;
  108. index.word = (*phrase_array).at(i);
  109. result->push_back(index);
  110. }
  111. }
  112. }
  113.  
  114. void quicksort(vector<wordAndCount> *result, int left, int right) {
  115. int begin = left;
  116. int end = right;
  117. int pivot_pos = (begin + end) / 2;
  118. wordAndCount pivot = (*result).at(pivot_pos);
  119. wordAndCount aux;
  120.  
  121. while(begin<=end) {
  122. while((*result).at(begin).counter > pivot.counter && begin < right) {
  123. begin++;
  124. }
  125. while((*result).at(end).counter < pivot.counter && end > left) {
  126. end--;
  127. }
  128. if(begin <= end) {
  129. aux = (*result).at(begin);
  130. (*result).at(begin) = (*result).at(end);
  131. (*result).at(end) = aux;
  132. begin++;
  133. end--;
  134. }
  135. }
  136.  
  137. if(end > left) {
  138. quicksort(result, left, end);
  139. }
  140. if(begin < right) {
  141. quicksort(result, begin, right);
  142. }
  143. }
  144.  
  145. void quicksort_string(vector<wordAndCount> *result) {
  146. int counter;
  147. int end = 0;
  148. int start = 0;
  149. int actual_size = result->size() - 1;
  150. wordAndCount aux;
  151.  
  152. while(start < actual_size) {
  153. aux = (*result).at(start);
  154. counter = 0;
  155. if(aux.counter == 1) {
  156. quicksort_final(result, start, actual_size);
  157. start = result->size();
  158. } else {
  159. do {
  160. counter++;
  161. } while((*result).at(start + counter).counter == aux.counter && actual_size > start + counter);
  162.  
  163. end+=counter-1;
  164. quicksort_final(result, start, end);
  165. start += counter;
  166. end = start;
  167. }
  168. }
  169. }
  170.  
  171. void quicksort_final(vector<wordAndCount> *result, int left, int right) {
  172. int begin = left;
  173. int end = right;
  174. int location = (begin + end) / 2;
  175. wordAndCount pivot = (*result).at(location);
  176. wordAndCount aux;
  177.  
  178. while(begin <= end) {
  179. while(pivot.word.compare((*result).at(begin).word) > 0) {
  180. begin++;
  181. }
  182. while(pivot.word.compare((*result).at(end).word) < 0) {
  183. end--;
  184. }
  185.  
  186. if(begin <= end) {
  187. aux = result->at(begin);
  188. (*result).at(begin) = (*result).at(end);
  189. (*result).at(end) = aux;
  190. begin++;
  191. end--;
  192. }
  193. }
  194. if(end > left) {
  195. quicksort_final(result, left, end);
  196. }
  197. if(begin < right) {
  198. quicksort_final(result, begin, right);
  199. }
  200. }
  201.  
  202. int found(string word, vector<wordAndCount> *result) {
  203. for(int i = 0; i < result->size(); ++i) {
  204. if((*result).at(i).word.compare(word) == 0) {
  205. return i;
  206. }
  207. }
  208. return -1;
  209. }
  210.  
  211. bool found_in_string(string word, vector<string> *string_array) {
  212. string aux;
  213.  
  214. for(int i = 0; i < string_array->size(); ++i) {
  215. aux = (*string_array).at(i);
  216. if(aux.compare(word) == 0) {
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222.  
  223. void printit(vector<wordAndCount> *result) {
  224. string word;
  225.  
  226. for(int i = 0; i < result->size(); ++i) {
  227. word = (*result).at(i).word;
  228. cout << word << " " << (*result).at(i).counter << endl;
  229. }
  230. }
  231.  
  232. //for debugging purposes only
  233. void print_strings(vector<string> *array) {
  234. string word;
  235.  
  236. for(int i = 0; i < array->size(); ++i) {
  237. cout << (*array).at(i) << endl;
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement