Language: C++

#include <iostream>
#include <vector>
#include <string>

struct move
{
    char ch, direction;

    move(){}

    move(char C, char D)
    {
        ch = C, direction = D;
    }
};

using namespace std;


int displacement(string &S, int position, vector <move> &M)
{ //cout << position << " final = ";
    for(int i = 1; i < M.size(); i++)
    {
        if(S[position] == M[i].ch)
        {
            position += (M[i].direction == 'L' ? -1 : 1);
        }

        if(position == 0 || position == S.size() - 1)
        {
            break;
        }
    }// cout << position << endl;

    return position;
}

int main()
{
    int length, no_of_moves;
    string S;
    cin >> length >> no_of_moves >> S;

    S = '*' + S + '*';

    vector <move> moves(no_of_moves + 1);
    for(int i = 1; i <= no_of_moves; i++)
    {
        cin >> moves[i].ch >> moves[i].direction;
    }

    int left = 0, right = length + 1;
    while(right - left > 1)
    {
        int mid = (left + right)/2;

        if(displacement(S, mid, moves) == 0)
        {
            left = mid;
        }
        else
        {
            right = mid;
        }
    }
    int rightmost_to_fall_left = left; //cout << "R = " << rightmost_to_fall_left << endl;

    left = 0, right = length;
    while(right - left > 1)
    {
        int mid = (left + right)/2;

        if(displacement(S, mid, moves) == length + 1)
        {
            right = mid;
        }
        else
        {
            left = mid;
        }
    }
    int leftmost_to_fall_right = right; //cout << "L = " << leftmost_to_fall_right << endl;

    int remaining = (leftmost_to_fall_right - 1) - rightmost_to_fall_left;
    cout << remaining;

    return 0;
}


Compiler output:
a.cc:20:55: error: template argument 1 is invalid
   20 | int displacement(string &S, int position, vector <move> &M)
      |                                                       ^
a.cc:20:55: error: template argument 2 is invalid
a.cc:20:55: error: template argument 1 is invalid
a.cc:20:55: error: template argument 2 is invalid
a.cc:20:55: error: template argument 1 is invalid
a.cc:20:55: error: template argument 2 is invalid
a.cc:20:43: error: invalid template-id
   20 | int displacement(string &S, int position, vector <move> &M)
      |                                           ^~~~~~
a.cc:20:51: error: reference to 'move' is ambiguous
   20 | int displacement(string &S, int position, vector <move> &M)
      |                                                   ^~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:675:5: note: candidates are: 'template<class _II, class _OI> _OI std::move(_II, _II, _OI)'
  675 |     move(_II __first, _II __last, _OI __result)
      |     ^~~~
In file included from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41:
/usr/include/c++/14/bits/move.h:137:5: note:                 'template<class _Tp> constexpr typename std::remove_reference<_Tp>::type&& std::move(_Tp&&)'
  137 |     move(_Tp&& __t) noexcept
      |     ^~~~
a.cc:5:8: note:                 'struct move'
    5 | struct move
      |        ^~~~
a.cc:20:43: error: missing template argument list after 'std::vector'; template placeholder not permitted in parameter
   20 | int displacement(string &S, int position, vector <move> &M)
      |                                           ^~~~~~
      |                                                 <>
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_vector.h:428:11: note: 'template<class _Tp, class _Alloc> class std::vector' declared here
  428 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
a.cc: In function 'int displacement(...)':
a.cc:22:24: error: 'M' was not declared in this scope
   22 |     for(int i = 1; i < M.size(); i++)
      |                        ^
a.cc: In function 'int main()':
a.cc:46:17: error: template argument 1 is invalid
   46 |     vector <move> moves(no_of_moves + 1);
      |                 ^
a.cc:46:17: error: template argument 2 is invalid
a.cc:49:21: error: invalid types 'int[int]' for array subscript
   49 |         cin >> moves[i].ch >> moves[i].direction;
      |                     ^
a.cc:49:36: error: invalid types 'int[int]' for array subscript
   49 |         cin >> moves[i].ch >> moves[i].direction;
      |                                    ^