Advertisement
Korotkodul

Задача 3. Популярная песня

Oct 15th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <algorithm>
  4. #include <set>
  5. #include <map>
  6. #include <cmath>
  7. #include <iostream>
  8. using namespace std;
  9. using ll = long long;
  10.  
  11. /*bool cmp(pair<string, int> a, pair <string, int> b) {
  12.     return a.first
  13. }*/
  14.  
  15. int main()
  16. {
  17.     string N;
  18.     cin >> N;
  19.     int M;
  20.     cin >> M;
  21.     ll ans = 0;
  22.     //vector <string> sections;
  23.     map <string, int> met;
  24.     map <string, int> last_idx;
  25.     map <string, int> val;
  26.     unsigned int max_size = 0;
  27.     for (int i = 0; i < M; ++i) {
  28.         string sect;
  29.         int ch;
  30.         cin >> sect>>ch;
  31.         val[sect] = ch;
  32.         last_idx[sect] = -999;
  33.         met[sect] = 0;
  34.         if (sect.size() > max_size) {
  35.             max_size = sect.size();
  36.         }
  37.     }
  38.     //cout << N.size() - max_size;
  39.     int till = N.size() - max_size;
  40.  
  41.     for (int i = 0; i < till; ++i) {
  42.         //cout << N << '\n';
  43.         for (auto el: val) {
  44.             string sub = el.first;
  45.             int ch = el.second;
  46.             int idx = N.find(sub);
  47.             //cout << "sub = " << sub << "  idx = " << idx << '\n';
  48.             //cout << "idx = " << idx << "   last_idx[sub] = " << last_idx[sub] << '\n';
  49.             if (idx == -1 || idx > N.size()) {
  50.                 //cout << "ONE\n";
  51.                 continue;
  52.             }
  53.             else if (idx == last_idx[sub] - 1) {
  54.                 //cout << "TWO\n";
  55.                 last_idx[sub] = idx;
  56.             }
  57.             else {
  58.                 //cout << "MET INCREASE\n";
  59.                 met[sub]++;
  60.                 last_idx[sub] = idx;
  61.             }
  62.         }
  63.         N = N.substr(1);
  64.         //cout << '\n';
  65.     }
  66.     //cout << "MET\n";
  67.     for (auto x : met) {
  68.         string sub = x.first;
  69.         int many = x.second;
  70.         //cout << "sub = " << sub << "   met = " << many << '\n';
  71.         ans += val[sub] * met[sub];
  72.     }
  73.     //cout << "ans = " << ans << '\n';
  74.     cout << ans << '\n';
  75.     //string a = "Hello World";
  76.     //auto ans = a.find("Helo");
  77.     //cout << typeid(ans).name() << '\n';
  78.     //if (ans > a.size()) cout << "NFOUND\n";
  79.     //cout << ans;
  80. }
  81. //MET
  82. //sub = 123456789   met = 3
  83. //sub = 1234567891   met = 2
  84. //sub = 91   met = 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement