Advertisement
ivangarvanliev

Untitled

Mar 12th, 2025
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<string> find_basic_commands(vector<string>& logs) {
  5.     sort(logs.begin(), logs.end(), [](const string &a, const string &b) {
  6.         return a.length() < b.length();
  7.     });
  8.    
  9.     vector<string> basic_commands;
  10.     for (const string& log : logs) {
  11.         bool is_substring = false;
  12.         for (const string& cmd : basic_commands) {
  13.             if (log.find(cmd) != string::npos) {
  14.                 is_substring = true;
  15.                 break;
  16.             }
  17.         }
  18.         if (!is_substring) {
  19.             basic_commands.push_back(log);
  20.         }
  21.     }
  22.     return basic_commands;
  23. }
  24.  
  25. int main() {
  26.     int C;
  27.     cin >> C;
  28.     cin.ignore();
  29.    
  30.     for (int case_idx = 1; case_idx <= C; ++case_idx) {
  31.         int N;
  32.         cin >> N;
  33.         cin.ignore();
  34.        
  35.         vector<string> logs(N);
  36.         for (int i = 0; i < N; ++i) {
  37.             getline(cin, logs[i]);
  38.         }
  39.        
  40.         vector<string> result = find_basic_commands(logs);
  41.        
  42.         cout << "Case #" << case_idx << ": " << result.size();
  43.         for (const string& cmd : result) {
  44.             cout << " " << cmd;
  45.         }
  46.         cout << "\n";
  47.     }
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement