Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <algorithm>
- #include <cstring>
- #include <stdio.h>
- using namespace std;
- struct wordAndCount {
- int counter;
- string word;
- };
- void print_strings(vector<string> *array);
- void prepare_array(vector<string> *, vector<string> *);
- void split(string ,vector<string> *, vector<string> *);
- bool found_in_string(string, vector<string> *);
- void preparation(vector<string> *, vector<wordAndCount> *);
- int found(string , vector<wordAndCount> *);
- void printit(vector<wordAndCount> *);
- void quicksort(vector<wordAndCount> *, int, int);
- void quicksort_string(vector<wordAndCount> *);
- void quicksort_final(vector<wordAndCount> *, int, int);
- int main(int argc, char *argv[])
- {
- int forb_result;
- int length_result;
- string aux;
- string forb;
- string length;
- vector<string> *phrases = new vector<string>;
- vector<string> *forbbidens = new vector<string>;
- stringstream convert;
- getline(cin, forb);
- convert << forb;
- convert >> forb_result;
- for(int i = 0; i < forb_result; ++i) {
- getline(cin, aux);
- std::transform(aux.begin(), aux.end(), aux.begin(), ::tolower);
- forbbidens->push_back(aux);
- }
- convert.str("");
- convert.clear();
- getline(cin, length);
- convert << length;
- convert >> length_result;
- for(int i = 0; i < length_result; ++i) {
- getline(cin, aux);
- std::transform(aux.begin(), aux.end(), aux.begin(), ::tolower);
- phrases->push_back(aux);
- }
- convert.str("");
- convert.clear();
- prepare_array(forbbidens, phrases);
- return 0;
- }
- void prepare_array(vector<string> *forbidden, vector<string> *phrases) {
- string phrase;
- vector<wordAndCount> *result = new vector<wordAndCount>;
- vector<string> *phrase_array;
- for(int i = 0; i < phrases->size(); i++) {
- phrase_array = new vector<string>;
- phrase = phrases->at(i);
- split(phrase, phrase_array, forbidden);
- preparation(phrase_array, result);
- }
- quicksort(result, 0, result->size() - 1);
- quicksort_string(result);
- printit(result);
- }
- void split(string phrase,vector<string> *phrase_array, vector<string> *forbidden) {
- string letter;
- string word("");
- vector<char> v(phrase.size() + 1);
- memcpy(&v.front(), phrase.c_str(), phrase.size() + 1);
- char *cp = v.data();
- char *worker;
- worker = strtok(cp, " ,.;:?!()-_@#$%&*+{}");
- while(worker != NULL) {
- word = worker;
- if(found_in_string(word, forbidden) == false) {
- phrase_array->push_back(word);
- }
- worker = strtok(NULL, " ,.;:?!()-_@#$%&*+{}");
- }
- }
- void preparation(vector<string> *phrase_array, vector<wordAndCount> *result) {
- int pos;
- wordAndCount index;
- for(int i = 0; i < phrase_array->size(); ++i) {
- if((pos = found((*phrase_array).at(i), result)) != -1) {
- (*result).at(pos).counter++;
- } else {
- index.counter = 1;
- index.word = (*phrase_array).at(i);
- result->push_back(index);
- }
- }
- }
- void quicksort(vector<wordAndCount> *result, int left, int right) {
- int begin = left;
- int end = right;
- int pivot_pos = (begin + end) / 2;
- wordAndCount pivot = (*result).at(pivot_pos);
- wordAndCount aux;
- while(begin<=end) {
- while((*result).at(begin).counter > pivot.counter && begin < right) {
- begin++;
- }
- while((*result).at(end).counter < pivot.counter && end > left) {
- end--;
- }
- if(begin <= end) {
- aux = (*result).at(begin);
- (*result).at(begin) = (*result).at(end);
- (*result).at(end) = aux;
- begin++;
- end--;
- }
- }
- if(end > left) {
- quicksort(result, left, end);
- }
- if(begin < right) {
- quicksort(result, begin, right);
- }
- }
- void quicksort_string(vector<wordAndCount> *result) {
- int counter;
- int end = 0;
- int start = 0;
- int actual_size = result->size() - 1;
- wordAndCount aux;
- while(start < actual_size) {
- aux = (*result).at(start);
- counter = 0;
- if(aux.counter == 1) {
- quicksort_final(result, start, actual_size);
- start = result->size();
- } else {
- do {
- counter++;
- } while((*result).at(start + counter).counter == aux.counter && actual_size > start + counter);
- end+=counter-1;
- quicksort_final(result, start, end);
- start += counter;
- end = start;
- }
- }
- }
- void quicksort_final(vector<wordAndCount> *result, int left, int right) {
- int begin = left;
- int end = right;
- int location = (begin + end) / 2;
- wordAndCount pivot = (*result).at(location);
- wordAndCount aux;
- while(begin <= end) {
- while(pivot.word.compare((*result).at(begin).word) > 0) {
- begin++;
- }
- while(pivot.word.compare((*result).at(end).word) < 0) {
- end--;
- }
- if(begin <= end) {
- aux = result->at(begin);
- (*result).at(begin) = (*result).at(end);
- (*result).at(end) = aux;
- begin++;
- end--;
- }
- }
- if(end > left) {
- quicksort_final(result, left, end);
- }
- if(begin < right) {
- quicksort_final(result, begin, right);
- }
- }
- int found(string word, vector<wordAndCount> *result) {
- for(int i = 0; i < result->size(); ++i) {
- if((*result).at(i).word.compare(word) == 0) {
- return i;
- }
- }
- return -1;
- }
- bool found_in_string(string word, vector<string> *string_array) {
- string aux;
- for(int i = 0; i < string_array->size(); ++i) {
- aux = (*string_array).at(i);
- if(aux.compare(word) == 0) {
- return true;
- }
- }
- return false;
- }
- void printit(vector<wordAndCount> *result) {
- string word;
- for(int i = 0; i < result->size(); ++i) {
- word = (*result).at(i).word;
- cout << word << " " << (*result).at(i).counter << endl;
- }
- }
- //for debugging purposes only
- void print_strings(vector<string> *array) {
- string word;
- for(int i = 0; i < array->size(); ++i) {
- cout << (*array).at(i) << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement