Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <map>
- using namespace std;
- vector<string> intersection(vector<string> &v1, vector<string> &v2) {
- vector<string> v3;
- sort(v1.begin(), v1.end());
- sort(v2.begin(), v2.end());
- set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v3));
- return v3;
- }
- vector<string>unite(vector<string> &v1,vector<string> &v2) {
- sort(v1.begin(), v1.end());
- sort(v2.begin(), v2.end());
- vector<string> v3;
- set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
- return v3;
- }
- int main() {
- string s_file, s_console, line_file;
- string loc;
- ifstream infile("C:\\\\Users\\\\ivanf\\\\CLionProjects\\\\serv2\\\\count_big.txt");
- //ifstream infile("count_big.txt");
- bool first_time = true;
- int count = 0;
- map<string, vector<string>> v_file;
- map<string, int> freq;
- while (cin >> s_console) {
- vector<string> v_console;
- if (s_console.length() == 1)
- v_console.push_back(s_console);
- else {
- for (int i = 0; i < s_console.length() - 1; i++) {
- v_console.push_back(s_console.substr(i, 2));
- }
- }
- float fraction = 0;
- int frequnt = 0;
- string word = "zzz";
- int i = 0;
- int num;
- if (first_time) {
- while (getline(infile, loc)) {
- s_file = loc.substr(0, loc.find('\t'));
- num = stoi(loc.substr(loc.find('\t')));
- s_file;
- freq[s_file] = num;
- vector<string> temp_file;
- if (s_file.length() == 1)
- temp_file.push_back(s_file);
- else {
- for (int i = 0; i < s_file.length() - 1; i++) {
- temp_file.push_back(s_file.substr(i, 2));
- }
- }
- v_file[s_file] = temp_file;
- vector<string> intersec = intersection(v_console, temp_file);
- vector<string> assoc = unite(v_console, temp_file);
- if ((float) intersec.size() / (float) assoc.size() > fraction) {
- fraction = (float) intersec.size() / (float) assoc.size();
- frequnt = num;
- word = s_file;
- } else if ((float) intersec.size() / (float) assoc.size() == fraction) {
- if (num > frequnt) {
- fraction = (float) intersec.size() / (float) assoc.size();
- frequnt = num;
- word = s_file;
- } else if (num == frequnt) {
- if (s_file < word) {
- fraction = (float) intersec.size() / (float) assoc.size();
- frequnt = num;
- word = s_file;
- }
- }
- }
- i++;
- count++;
- }
- } else {
- //for (int i = 0; i < count; i++)
- for (auto temp : v_file) {
- vector<string> intersec = intersection(v_console, temp.second);
- vector<string> assoc = unite(v_console, temp.second);
- if ((float) intersec.size() / (float) assoc.size() > fraction) {
- fraction = (float) intersec.size() / (float) assoc.size();
- frequnt = freq[temp.first];
- word = temp.first;
- } else if ((float) intersec.size() / (float) assoc.size() == fraction) {
- if (freq[temp.first] > frequnt) {
- fraction = (float) intersec.size() / (float) assoc.size();
- frequnt = freq[temp.first];
- word = temp.first;
- } else if (freq[temp.first] == frequnt) {
- if (temp.first < word) {
- fraction = (float) intersec.size() / (float) assoc.size();
- frequnt = freq[temp.first];
- word = temp.first;
- }
- }
- }
- }
- }
- first_time = false;
- cout << word << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement