Language: C++

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1000+10;
int ai, bi, ci;
int m, n;
int begin, end;
struct node{
	char x;
	int y;
}a[N],b[N],c[N],ans[N],bns[N];
int bj = 0;
bool flag = true;
bool dfs(int t, int x){
	if(t == ci && flag)	return flag;
	if(t == 0){
		if(c[t].x == a[x].x && c[t].y <= a[x].y){
			begin = a[x].y - c[t].y;
			dfs(t+1, x+1);
		}else{
			flag = false;
			return false;
		}
	}else if(t == ci-1){
		if(c[t].x == a[x].x && c[t].y <= a[x].y){
			end = a[x].y - c[t].y;
			return true;
		}else{
			flag = false;
			return false;
		}
	}else{
		if(c[t].x == a[x].x && c[t].x == a[x].x){
			dfs(t+1, x+1);
		}else{
			flag = false;
			return false;
		}
	} 
}
void print(){
	bns[0].x = ans[0].x;
	bns[0].y = ans[0].y;
	for(int i = 1; i < ai+bi; i++){
		if(ans[i].y == 0)	continue;
		if(ans[i].x == bns[bj].x){
			bns[bj].y += ans[i].y;
		}else{
			bj++;
			bns[bj].x = ans[i].x;
			bns[bj].y = ans[i].y;
		}
	}
}

int main(){
	std::ios::sync_with_stdio(false);
	ai = bi = ci = 0;
	char le;
	int num;
	while(1){
		cin >> le;
		if(le == '$') 	break;
		cin >> num;
		a[ai].x = le;
		a[ai].y = num;
		ai++;
	}
	while(1){
		cin >> le;
		if(le == '$') 	break;
		cin >> num;
		c[ci].x = le;
		c[ci].y = num;
		ci++;
	}
	while(1){
		cin >> le;
		if(le == '$') 	break;
		cin >> num;
		b[bi].x = le;
		b[bi].y = num;
		bi++;
	}
	if(ci > ai){
		for(int i = 0; i < ai; i++)
		cout << a[i].x << " " << a[i].y << " ";
		cout << endl;
		return 0; 
	}
	for(int i = 0; i <= ai-ci; i++){
		if(dfs(0,i)){
			int j = 0;
			for(j = 0; j <= i; j++){
				if(j == i){
					ans[j].x = a[j].x;
					ans[j].y = begin;
					continue;
				}
				ans[j].x = a[j].x;
				ans[j].y = a[j].y;
			}
			int mm;
			for(int k = 0; k < bi; k++,j++){
				ans[j].x = b[k].x;
				ans[j].y = b[k].y;
			}
			for(int k = i+ci-1; k < ai; j++,k++){
				if(k == ai-1){
					ans[j].x = a[k].x;
					ans[j].y = end;
					continue;
				}
				ans[j].x = a[k].x;
				ans[j].y = a[k].y;
			}
			print();
			for(int i = 0; i <= bj; i++)
			cout << bns[i].x << " " << bns[i].y << " ";
			cout << "$" << endl;
			return 0;
		}
	}
	for(int i = 0; i < ai; i++)
		cout << a[i].x << " " << a[i].y << " ";
		cout << endl;
	return 0;
}


/*

R 100 L 100 $
R 50 L 50 $
C 50 D 50 $



*/



Compiler output:
a.cc: In function 'bool dfs(int, int)':
a.cc:30:25: error: reference to 'begin' is ambiguous
   30 |                         begin = a[x].y - c[t].y;
      |                         ^~~~~
In file included from /usr/include/c++/14/string:53,
                 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/range_access.h:114:37: note: candidates are: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
/usr/include/c++/14/bits/range_access.h:113:31: note:                 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
  113 |   template<typename _Tp> _Tp* begin(valarray<_Tp>&) noexcept;
      |                               ^~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
In file included from /usr/include/c++/14/bits/range_access.h:36:
/usr/include/c++/14/initializer_list:88:5: note:                 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
a.cc:19:5: note:                 'int begin'
   19 | int begin, end;
      |     ^~~~~
a.cc:38:25: error: reference to 'end' is ambiguous
   38 |                         end = a[x].y - c[t].y;
      |                         ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
      |                               ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:19:12: note:                 'int end'
   19 | int begin, end;
      |            ^~~
a.cc: In function 'int main()':
a.cc:109:52: error: reference to 'begin' is ambiguous
  109 |                                         ans[j].y = begin;
      |                                                    ^~~~~
/usr/include/c++/14/bits/range_access.h:114:37: note: candidates are: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
/usr/include/c++/14/bits/range_access.h:113:31: note:                 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
  113 |   template<typename _Tp> _Tp* begin(valarray<_Tp>&) noexcept;
      |                               ^~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:                 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/initializer_list:88:5: note:                 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
a.cc:19:5: note:                 'int begin'
   19 | int begin, end;
      |     ^~~~~
a.cc:123:52: error: reference to 'end' is ambiguous
  123 |                                         ans[j].y = end;
      |                                                    ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
      |                               ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:19:12: note:                 'int end'
   19 | int begin, end;
      |            ^~~
a.cc: In function 'bool dfs(int, int)':
a.cc:52:1: warning: control reaches end of non-void function [-Wreturn-type]
   52 | }
      | ^