Advertisement
Dang_Quan_10_Tin

TRAINING

Apr 7th, 2022
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #define task "TRAINING"
  2. #include <iostream>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. using ll = long long;
  8. using ld = long double;
  9.  
  10. constexpr int N = 2e5 + 5;
  11.  
  12. int n, q;
  13. string s;
  14.  
  15. char t[N], c[N];
  16.  
  17. void Read()
  18. {
  19.     cin >> n >> q >> s;
  20.     s = " " + s;
  21.  
  22.     for (int i = 1; i <= q; ++i)
  23.         cin >> c[i] >> t[i];
  24. }
  25.  
  26. bool Check_0(int pos)
  27. {
  28.     for (int i = 1; i <= q; ++i)
  29.     {
  30.         if (c[i] == s[pos])
  31.         {
  32.             if (t[i] == 'L')
  33.                 --pos;
  34.             else
  35.                 ++pos;
  36.         }
  37.  
  38.         if (pos == 0)
  39.             return true;
  40.         else if (pos == n + 1)
  41.             return false;
  42.     }
  43.  
  44.     return false;
  45. }
  46.  
  47. bool Check_1(int pos)
  48. {
  49.     for (int i = 1; i <= q; ++i)
  50.     {
  51.         if (c[i] == s[pos])
  52.         {
  53.             if (t[i] == 'L')
  54.                 --pos;
  55.             else
  56.                 ++pos;
  57.         }
  58.  
  59.         if (pos == 0)
  60.             return false;
  61.         else if (pos == n + 1)
  62.             return true;
  63.     }
  64.  
  65.     return false;
  66. }
  67.  
  68. void Solve()
  69. {
  70.     int l = 1, m, h = n;
  71.     int ans(0);
  72.  
  73.     while (l <= h)
  74.     {
  75.         m = (l + h) / 2;
  76.         if (Check_0(m))
  77.             l = m + 1;
  78.         else
  79.             h = m - 1;
  80.     }
  81.  
  82.     ans = h;
  83.  
  84.     l = 1, h = n;
  85.  
  86.     while (l <= h)
  87.     {
  88.         m = (l + h) / 2;
  89.         if (Check_1(m))
  90.             h = m - 1;
  91.         else
  92.             l = m + 1;
  93.     }
  94.  
  95.     ans += n - h;
  96.  
  97.     cout << n - ans;
  98. }
  99.  
  100. int32_t main()
  101. {
  102.     ios_base::sync_with_stdio(0);
  103.     cin.tie(0);
  104.     cout.tie(0);
  105.  
  106.     if (fopen(task ".INP", "r"))
  107.     {
  108.         freopen(task ".INP", "r", stdin);
  109.         freopen(task ".OUT", "w", stdout);
  110.     }
  111.  
  112.     Read();
  113.     Solve();
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement