Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     vector<string> v;
  10.    
  11.     ifstream input ("/Users/alexey/Desktop/Программирование/Лабы/Laba 5/test2.txt");
  12.     ofstream output1 ("/Users/alexey/Desktop/Программирование/Лабы/Laba 5/ответ1.txt");
  13.     ofstream output2 ("/Users/alexey/Desktop/Программирование/Лабы/Laba 5/ответ2.txt");
  14.    
  15.     string word;
  16.     while (input >> word){
  17.             v.push_back(word);
  18.     }
  19.    
  20.     long long int maxN = 0, maxLen = 0, indexN = - 1, indexLen = - 1;
  21.     vector<int> max;
  22.    
  23.     for (int i = 0; i < v.size(); ++i) {
  24.         long long int k = 0;
  25.         vector<int> d;
  26.         for (int j = 0; j < v.size(); ++j) {
  27.             if (i == j) continue;
  28.             if (v[j].find(v[i], 0) == 0) {
  29.                 k++;
  30.                 d.push_back(j);
  31.             }
  32.         }
  33.         if (k > maxN) {
  34.             maxN = k;
  35.             indexN = i;
  36.         }
  37.         if ((k > 0) && (v[i].length() > maxLen)) {
  38.             maxLen = v[i].length();
  39.             indexLen = i;
  40.             max.resize(k);
  41.             for (int q = 0; q < k; ++q)
  42.                 max[q] = d[q];
  43.         }
  44.     }
  45.    
  46.     if (indexLen == -1) output1 << "NO" << endl;
  47.     else {
  48.         output1 << v[indexLen] << ' ';
  49.         for (const int& index : max)
  50.             output1 << v[index] << ' ';
  51.     }
  52.    
  53.     if (indexN == -1) output2 << "NO" << endl;
  54.     else output2 << v[indexN] << ' ' << maxN;
  55.    
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement