Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "TRAINING"
- #include <iostream>
- #include <cstdio>
- using namespace std;
- using ll = long long;
- using ld = long double;
- constexpr int N = 2e5 + 5;
- int n, q;
- string s;
- char t[N], c[N];
- void Read()
- {
- cin >> n >> q >> s;
- s = " " + s;
- for (int i = 1; i <= q; ++i)
- cin >> c[i] >> t[i];
- }
- bool Check_0(int pos)
- {
- for (int i = 1; i <= q; ++i)
- {
- if (c[i] == s[pos])
- {
- if (t[i] == 'L')
- --pos;
- else
- ++pos;
- }
- if (pos == 0)
- return true;
- else if (pos == n + 1)
- return false;
- }
- return false;
- }
- bool Check_1(int pos)
- {
- for (int i = 1; i <= q; ++i)
- {
- if (c[i] == s[pos])
- {
- if (t[i] == 'L')
- --pos;
- else
- ++pos;
- }
- if (pos == 0)
- return false;
- else if (pos == n + 1)
- return true;
- }
- return false;
- }
- void Solve()
- {
- int l = 1, m, h = n;
- int ans(0);
- while (l <= h)
- {
- m = (l + h) / 2;
- if (Check_0(m))
- l = m + 1;
- else
- h = m - 1;
- }
- ans = h;
- l = 1, h = n;
- while (l <= h)
- {
- m = (l + h) / 2;
- if (Check_1(m))
- h = m - 1;
- else
- l = m + 1;
- }
- ans += n - h;
- cout << n - ans;
- }
- int32_t main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement