Problem:
Problem F:
天使の階段
なつめの住んでいる街の上に浮かんでいる雲には、天使が住んでいる。その天使はなつめと同じくねこ好きで、しばしばねこと遊びに地上に降りてくる。地上に降りるために、天使は雲から地上へ繋がる長い長い階段を作った。しかし、毎回毎回ただ降りていくだけではつまらないと思った天使は、階段に細工をして、段を踏むと音が出るようにした。これを使って、音楽を奏でながら降りていくのだ。
音楽は12種類の音を使って奏でるものである。今回、音のオクターブは無視することにして、 C, C#, D, D#, E, F, F#, G, G#, A, A#, B の12種類のみを考える。隣りあう音同士の差を半音と呼ぶ。たとえば、Cを半音上げるとC#、C#を半音上げるとDとなる。逆に、Gを半音下げるとF#、 F#を半音下げるとFとなる。なお、Eを半音上げるとF、Bを半音上げるとCになることに注意してほしい。
階段から音が出る仕組みは次のようになっている。まず、階段は空中に浮かぶ
n
枚の白い板からなっている。雲から数えて 1 ~
n
段目の板には、それぞれに12音のどれかが割り振られている。これを
T
i
(
i
= 1 ...
n
) と書くことにする。また簡単のために、雲は0段目、地上は
n
+1 段目と考える (ただしこれらには音階は割り当てられていない) 。天使が
k
(
k
= 0 ...
n
) 段目にいるとき、次は
k
-1,
k
+1,
k
+2 段のうち存在するもののどれかに行くことができる。ただし、
n
+1 段目 (地上) に降りた後は動くことはできず、0段目 (雲) を離れた後はそこに戻ることはできない。それぞれの動き方をしたとき、次のようなルールで音が鳴る。
k
+1 段目に降りた
T
k
+1
の音が鳴る。
k
+2 段目に降りた
T
k
+2
を半音上げた音が鳴る。
k
-1 段目に戻った
T
k
-1
を半音下げた音が鳴る。
階段の情報
T
1
...
T
n
と、天使が奏でたい曲
S
1
...
S
m
が与えられる。このとき、奏でたい曲の前、途中、後で別の音を鳴らしてはならない。天使がこの曲を奏でて雲から地上へ降りられるかどうか判定せよ。
Input
入力の1行目には、階段の段数
n
と天使が奏でたい曲の長さ
m
が与えられる。2行目は階段の情報であり、
T
1
,
T
2
, ...,
T
n
がこの順で与えられる。3行目は天使が奏でたい曲であり、
S
1
,
S
2
, ...,
S
m
がこの順で与えられる。これらは全て1つの空白文字で区切られており、1 <=
n
,
m
<= 50000を満たす。
Output
天使が与えられた曲を奏でながら地上に降りられるなら"Yes"、そうでなければ"No"を1行に出力せよ。
Notes on Submission
上記形式で複数のデータセットが与えられます。入力データの 1 行目にデータセットの数が与えられます。各データセットに対する出力を上記形式で順番に出力するプログラムを作成して下さい。
Sample Input
4
6 4
C E D# F G A
C E F G
6 4
C E D# F G A
C D# F G
3 6
C D D
D# B D B D# C#
8 8
C B B B B B F F
C B B B B B B B
Output for the Sample Input
Yes
No
Yes
No

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;

vector<string> oc={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
vector<int> ti(50000+1),si(50000+1);

int tc=0,sc=0;

bool nxt(int ps,int cnt){
  //cout << ps << " " << cnt << endl;
  if(cnt==0){
    if(ps==0) return true;
    else return false;
  }
  bool res=false;
  if(((ps-1)>=0)&&(si[cnt]==ti[ps])){
    res=(res||nxt(ps-1,cnt-1));
  }
  if(((ps-2)>=0)&&(si[cnt]==(ti[ps]+1)%12)){
    res=(res||nxt(ps-2,cnt-1));
  }
  if(((ps+1)<=tc)&&(si[cnt]==(ti[ps]-1+12)%12)){
    res=(res||nxt(ps+1,cnt-1));
  }
  return res;
}


int main(){

  int n;
  cin >> n;
  
  for(int k=0;k<n;k++){
    cin >> tc >> sc;
    vector<string> t(tc),s(sc);
    for(auto &a:t) cin >> a;
    for(auto &a:s) cin >> a;
    
    for(int i=0;i<tc;i++){
      ti[i+1]=distance(oc.begin(),find(oc.begin(),oc.end(),t[i]));
    }
    for(int i=0;i<sc;i++){
      si[i+1]=distance(oc.begin(),find(oc.begin(),oc.end(),s[i]));
    }
    
    if(nxt(tc,sc)||nxt(tc-1,sc)) cout << "Yes" << endl;
    else cout << "No" << endl;

  }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 8404
final_rank: 12

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    map<string,int> mp;
    mp["C"] = 0;
    mp["C#"] = 1;
    mp["D"] = 2;
    mp["D#"] = 3;
    mp["E"] = 4;
    mp["F"] = 5;
    mp["F#"] = 6;
    mp["G"] = 7;
    mp["G#"] = 8;
    mp["A"] = 9;
    mp["A#"] = 10;
    mp["B"] = 11;
    int q; cin >> q;
    while(q--){
        int n; cin >> n;
        int m; cin >> m;
        vector<int> a(n+2,1e9);
        for(int i=1;i<=n;i++){
            string s; cin >> s;
            a[i] = mp[s];
        }
        vector<int> b(m);
        for(int i=0;i<m;i++){
            string s; cin >> s;
            b[i] = mp[s];
        }
        auto solve=[&](auto self, int idx, int cur)->bool{
            if(idx == -1){
                return (cur == 0);
            }
            if(cur <= 0 or cur > n) return false;
            if(a[cur] == b[idx]){
                if(self(self, idx-1, cur-1)) return true;
            }
            if((a[cur] + 1)%12 == b[idx]){
                if(self(self, idx-1, cur-2)) return true;
            }
            if((a[cur] + 11)%12 == b[idx]){
                if(self(self, idx-1, cur+1)) return true;
            }
            return false;
        };
        if(solve(solve, m-1, n) or solve(solve, m-1, n-1)){
            cout << "Yes" << "\n";
        }
        else{
            cout << "No" << "\n";
        }
    }
}
accuracy: 1
time_ms: 30
memory_kb: 7724
final_rank: 11

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    map<string,int> mp;
    mp["C"] = 0;
    mp["C#"] = 1;
    mp["D"] = 2;
    mp["D#"] = 3;
    mp["E"] = 4;
    mp["F"] = 5;
    mp["F#"] = 6;
    mp["G"] = 7;
    mp["G#"] = 8;
    mp["A"] = 9;
    mp["A#"] = 10;
    mp["B"] = 11;
    int q; cin >> q;
    while(q--){
        int n; cin >> n;
        int m; cin >> m;
        vector<int> a(n+2,1e9);
        for(int i=1;i<=n;i++){
            string s; cin >> s;
            a[i] = mp[s];
        }
        vector<int> b(m);
        for(int i=0;i<m;i++){
            string s; cin >> s;
            b[i] = mp[s];
        }
        auto solve=[&](auto self, int idx, int cur)->bool{
            if(idx == -1){
                return (cur == 0);
            }
            if(cur <= 0 or cur > n) return false;
            if(a[cur] == b[idx]){
                if(self(self, idx-1, cur-1)) return true;
            }
            if((a[cur] + 1)%12 == b[idx]){
                if(self(self, idx-1, cur-2)) return true;
            }
            if((a[cur] + 11)%12 == b[idx]){
                if(self(self, idx-1, cur+1)) return true;
            }
            return false;
        };
        if(solve(solve, m-1, n) or solve(solve, m-1, n-1)){
            cout << "Yes" << "\n";
        }
        else{
            cout << "No" << "\n";
        }
    }
}
accuracy: 1
time_ms: 30
memory_kb: 7700
final_rank: 10

code_snippet: #line 2 "/home/yuruhiya/programming/library/template/template.cpp"
#include <bits/stdc++.h>
#line 6 "/home/yuruhiya/programming/library/template/constants.cpp"
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rfor(i, m, n) for (int i = (m); i >= (n); --i)
#define unless(c) if (!(c))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)

using namespace std;
using ll = long long;
using LD = long double;
using VB = vector<bool>;
using VVB = vector<VB>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using VD = vector<LD>;
using PII = pair<int, int>;
using VP = vector<PII>;
using PLL = pair<ll, ll>;
using VPL = vector<PLL>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1e9;
constexpr long long inf_ll = 1e18, MOD = 1000000007;
constexpr long double PI = 3.14159265358979323846, EPS = 1e-12;
#line 7 "/home/yuruhiya/programming/library/template/Input.cpp"
using namespace std;

#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
class Scanner {
	int gc() const {
		return getchar_unlocked();
	}
	template <class T> void read(T& v) const {
		cin >> v;
	}
	void read(char& v) const {
		while (isspace(v = gc()))
			;
	}
	void read(bool& v) const {
		v = read<char>() != '0';
	}
	void read(string& v) const {
		v.clear();
		for (char c = read<char>(); !isspace(c); c = gc()) v += c;
	}
	void read(int& v) const {
		v = 0;
		bool neg = false;
		char c = read<char>();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	void read(long long& v) const {
		v = 0;
		bool neg = false;
		char c = read<char>();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	void read(double& v) const {
		v = 0;
		double dp = 1;
		bool neg = false, after_dp = false;
		char c = read<char>();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	void read(long double& v) const {
		v = 0;
		long double dp = 1;
		bool neg = false, after_dp = false;
		char c = read<char>();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	template <class T, class U> void read(pair<T, U>& v) const {
		read(v.first);
		read(v.second);
	}
	template <class T> void read(vector<T>& v) const {
		for (auto& e : v) read(e);
	}
	template <size_t N = 0, class T> void read_tuple_impl(T& v) const {
		if constexpr (N < tuple_size_v<T>) {
			read(get<N>(v));
			read_tuple_impl<N + 1>(v);
		}
	}
	template <class... T> void read(tuple<T...>& v) const {
		read_tuple_impl(v);
	}
	struct ReadVectorHelper {
		size_t n;
		ReadVectorHelper(size_t _n) : n(_n) {}
		template <class T> operator vector<T>() {
			vector<T> v(n);
			read(v);
			return v;
		}
	};
	struct Read2DVectorHelper {
		size_t n, m;
		Read2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}
		template <class T> operator vector<vector<T>>() {
			vector<vector<T>> v(n, vector<T>(m));
			read(v);
			return v;
		}
	};

public:
	string read_line() const {
		string v;
		for (char c = read<char>(); c != '\n' && c != '\0'; c = gc()) v += c;
		return v;
	}
	template <class T> T read() const {
		T v;
		read(v);
		return v;
	}
	template <class T> vector<T> read_vector(size_t n) const {
		vector<T> a(n);
		read(a);
		return a;
	}
	template <class T> operator T() const {
		return read<T>();
	}
	int operator--(int) const {
		return read<int>() - 1;
	}
	ReadVectorHelper operator[](size_t n) const {
		return ReadVectorHelper(n);
	}
	Read2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {
		return Read2DVectorHelper(nm);
	}
	void operator()() const {}
	template <class H, class... T> void operator()(H&& h, T&&... t) const {
		read(h);
		operator()(forward<T>(t)...);
	}

private:
	template <template <class...> class, class...> struct Multiple;
	template <template <class...> class V, class Head, class... Tail>
	struct Multiple<V, Head, Tail...> {
		template <class... Args> using vec = V<vector<Head>, Args...>;
		using type = typename Multiple<vec, Tail...>::type;
	};
	template <template <class...> class V> struct Multiple<V> { using type = V<>; };
	template <class... T> using multiple_t = typename Multiple<tuple, T...>::type;
	template <size_t N = 0, class T> void multiple_impl(T& t) const {
		if constexpr (N < tuple_size_v<T>) {
			auto& vec = get<N>(t);
			using V = typename remove_reference_t<decltype(vec)>::value_type;
			vec.push_back(read<V>());
			multiple_impl<N + 1>(t);
		}
	}

public:
	template <class... T> auto multiple(size_t h) const {
		multiple_t<T...> result;
		while (h--) multiple_impl(result);
		return result;
	}
} in;
#define inputs(T, ...) \
	T __VA_ARGS__;     \
	in(__VA_ARGS__)
#define ini(...) inputs(int, __VA_ARGS__)
#define inl(...) inputs(long long, __VA_ARGS__)
#define ins(...) inputs(string, __VA_ARGS__)
#line 7 "/home/yuruhiya/programming/library/template/Output.cpp"
#include <charconv>
#line 10 "/home/yuruhiya/programming/library/template/Output.cpp"
using namespace std;

struct BoolStr {
	const char *t, *f;
	BoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
	const char *d, *l;
	DivStr(const char* _d, const char* _l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"),
    no_endl(" ", "");
class Output {
	BoolStr B{Yes};
	DivStr D{spc};

public:
	void put(int v) const {
		char buf[12]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void put(long long v) const {
		char buf[21]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void put(bool v) const {
		put(v ? B.t : B.f);
	}
	void put(char v) const {
		putchar_unlocked(v);
	}
	void put(const char* v) const {
		fwrite_unlocked(v, 1, strlen(v), stdout);
	}
	void put(double v) const {
		printf("%.20f", v);
	}
	void put(long double v) const {
		printf("%.20Lf", v);
	}
	template <class T> void put(const T& v) const {
		cout << v;
	}
	template <class T, class U> void put(const pair<T, U>& v) const {
		put(v.first);
		put(D.d);
		put(v.second);
	}
	template <class InputIterater>
	void put_range(const InputIterater& begin, const InputIterater& end) const {
		for (InputIterater i = begin; i != end; ++i) {
			if (i != begin) put(D.d);
			put(*i);
		}
	}
	template <class T> void put(const vector<T>& v) const {
		put_range(v.begin(), v.end());
	}
	template <class T, size_t N> void put(const array<T, N>& v) const {
		put_range(v.begin(), v.end());
	}
	template <class T> void put(const vector<vector<T>>& v) const {
		for (size_t i = 0; i < v.size(); ++i) {
			if (i) put(D.l);
			put(v[i]);
		}
	}

	Output() = default;
	Output(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}
	Output& operator()() {
		put(D.l);
		return *this;
	}
	template <class H> Output& operator()(H&& h) {
		put(h);
		put(D.l);
		return *this;
	}
	template <class H, class... T> Output& operator()(H&& h, T&&... t) {
		put(h);
		put(D.d);
		return operator()(forward<T>(t)...);
	}
	template <class InputIterator>
	Output& range(const InputIterator& begin, const InputIterator& end) {
		put_range(begin, end);
		put(D.l);
		return *this;
	}
	template <class T> Output& range(const T& a) {
		range(a.begin(), a.end());
		return *this;
	}
	template <class... T> void exit(T&&... t) {
		operator()(forward<T>(t)...);
		std::exit(EXIT_SUCCESS);
	}
	Output& flush() {
		fflush_unlocked(stdout);
		return *this;
	}
	Output& set(const BoolStr& b) {
		B = b;
		return *this;
	}
	Output& set(const DivStr& d) {
		D = d;
		return *this;
	}
	Output& set(const char* t, const char* f) {
		B = BoolStr(t, f);
		return *this;
	}
} out;
#line 3 "/home/yuruhiya/programming/library/template/Step.cpp"
using namespace std;

template <class T> struct Step {
	using value_type = T;

	class iterator {
		value_type a, b, c;

	public:
		constexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}
		constexpr iterator(value_type _b, value_type _c, value_type _s)
		    : a(_b), b(_c), c(_s) {}
		constexpr iterator& operator++() {
			--b;
			a += c;
			return *this;
		}
		constexpr iterator operator++(int) {
			iterator tmp = *this;
			--b;
			a += c;
			return tmp;
		}
		constexpr const value_type& operator*() const {
			return a;
		}
		constexpr const value_type* operator->() const {
			return &a;
		}
		constexpr bool operator==(const iterator& i) const {
			return b == i.b;
		}
		constexpr bool operator!=(const iterator& i) const {
			return !(b == i.b);
		}
		constexpr value_type start() const {
			return a;
		}
		constexpr value_type size() const {
			return b;
		}
		constexpr value_type step() const {
			return c;
		}
	};
	constexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}
	constexpr iterator begin() const {
		return be;
	}
	constexpr iterator end() const {
		return en;
	}
	constexpr value_type start() const {
		return be.start();
	}
	constexpr value_type size() const {
		return be.size();
	}
	constexpr value_type step() const {
		return be.step();
	}
	constexpr value_type sum() const {
		return start() * size() + step() * (size() * (size() - 1) / 2);
	}
	operator vector<value_type>() const {
		return to_a();
	}
	auto to_a() const {
		vector<value_type> result;
		result.reserve(size());
		for (auto i : *this) {
			result.push_back(i);
		}
		return result;
	}

private:
	iterator be, en;
};
template <class T> constexpr auto step(T a) {
	return Step<T>(0, a, 1);
}
template <class T> constexpr auto step(T a, T b) {
	return Step<T>(a, b - a, 1);
}
template <class T> constexpr auto step(T a, T b, T c) {
	return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);
}
#line 8 "/home/yuruhiya/programming/library/template/Ruby.cpp"
using namespace std;

template <class F> struct Callable {
	F func;
	Callable(const F& f) : func(f) {}
};
template <class T, class F> auto operator|(const T& v, const Callable<F>& c) {
	return c.func(v);
}

struct Sort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {
		sort(begin(v), end(v));
		return v;
	}
} Sort;
struct SortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) < f(j); });
			return v;
		});
	}
} SortBy;
struct RSort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(rbegin(v), rend(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {
		sort(rbegin(v), rend(v));
		return v;
	}
} RSort;
struct RSortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) > f(j); });
			return v;
		});
	}
} RSortBy;
struct Reverse_impl {
	template <class T> friend auto operator|(T v, const Reverse_impl& c) {
		reverse(begin(v), end(v));
		return v;
	}
} Reverse;
struct Unique_impl {
	template <class T> friend auto operator|(T v, const Unique_impl& c) {
		v.erase(unique(begin(v), end(v), end(v)));
		return v;
	}
} Unique;
struct Uniq_impl {
	template <class T> friend auto operator|(T v, const Uniq_impl& c) {
		sort(begin(v), end(v));
		v.erase(unique(begin(v), end(v)), end(v));
		return v;
	}
} Uniq;
struct Rotate_impl {
	auto operator()(int&& left) {
		return Callable([&](auto v) {
			int s = static_cast<int>(size(v));
			assert(-s <= left && left <= s);
			if (0 <= left) {
				rotate(begin(v), begin(v) + left, end(v));
			} else {
				rotate(begin(v), end(v) + left, end(v));
			}
			return v;
		});
	}
} Rotate;
struct Max_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *max_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Max_impl& c) {
		return *max_element(begin(v), end(v));
	}
} Max;
struct Min_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *min_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Min_impl& c) {
		return *min_element(begin(v), end(v));
	}
} Min;
struct MaxPos_impl {
	template <class T> friend auto operator|(T v, const MaxPos_impl& c) {
		return max_element(begin(v), end(v)) - begin(v);
	}
} MaxPos;
struct MinPos_impl {
	template <class T> friend auto operator|(T v, const MinPos_impl& c) {
		return min_element(begin(v), end(v)) - begin(v);
	}
} MinPos;
struct MaxBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_it = begin(v);
			auto max_val = f(*max_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_it = it;
					max_val = val;
				}
			}
			return *max_it;
		});
	}
} MaxBy;
struct MinBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_it = begin(v);
			auto min_val = f(*min_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_it = it;
					min_val = val;
				}
			}
			return *min_it;
		});
	}
} MinBy;
struct MaxOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_val = val;
				}
			}
			return max_val;
		});
	}
} MaxOf;
struct MinOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_val = val;
				}
			}
			return min_val;
		});
	}
} MinOf;
struct Count_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return count(begin(v), end(v), val); });
	}
} Count;
struct CountIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return count_if(begin(v), end(v), f); });
	}
} CountIf;
struct Index_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find(begin(v), end(v), val);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} Index;
struct IndexIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} IndexIf;
struct FindIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<typename decltype(v)::value_type> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(*result) : nullopt;
		});
	}
} FindIf;
struct Sum_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			return accumulate(next(begin(v)), end(v), f(*begin(v)),
			                  [&](const auto& a, const auto& b) { return a + f(b); });
		});
	}
	template <class T> friend auto operator|(T v, const Sum_impl& c) {
		return accumulate(begin(v), end(v), typename T::value_type{});
	}
} Sum;
struct Includes {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });
	}
} Includes;
struct IncludesIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });
	}
} IncludesIf;
struct RemoveIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) {
			v.erase(remove_if(begin(v), end(v), f), end(v));
			return v;
		});
	}
} RemoveIf;
struct Each_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				f(i);
			}
		});
	}
} Each;
struct Select_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using value_type = typename decltype(v)::value_type;
			vector<value_type> result;
			for (const auto& i : v) {
				if (f(i)) result.push_back(i);
			}
			return result;
		});
	}
} Select;
struct Map_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using result_type = invoke_result_t<F, typename decltype(v)::value_type>;
			vector<result_type> result;
			result.reserve(size(v));
			for (const auto& i : v) {
				result.push_back(f(i));
			}
			return result;
		});
	}
} Map;
struct Indexed_impl {
	template <class T> friend auto operator|(const T& v, Indexed_impl& c) {
		using value_type = typename T::value_type;
		vector<pair<value_type, int>> result;
		result.reserve(size(v));
		int index = 0;
		for (const auto& i : v) {
			result.emplace_back(i, index++);
		}
		return result;
	}
} Indexed;
struct AllOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (!f(i)) return false;
			}
			return true;
		});
	}
} AllOf;
struct AnyOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return true;
			}
			return false;
		});
	}
} AnyOf;
struct NoneOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return false;
			}
			return true;
		});
	}
} NoneOf;

struct Tally_impl {
	template <class F> auto operator()(size_t max_val) {
		return Callable([&](auto v) {
			vector<size_t> result(max_val);
			for (const auto& i : v) {
				result[static_cast<size_t>(i)]++;
			}
			return result;
		});
	}
	template <class T, class value_type = typename T::value_type>
	friend auto operator|(const T& v, Tally_impl& c) {
		map<value_type, size_t> result;
		for (const auto& i : v) {
			result[i]++;
		}
		return result;
	}
} Tally;

template <class T> auto operator*(const vector<T>& a, size_t n) {
	T result;
	for (size_t i = 0; i < n; ++i) {
		result.insert(result.end(), a.begin(), a.end());
	}
	return result;
}
auto operator*(string a, size_t n) {
	string result;
	for (size_t i = 0; i < n; ++i) {
		result += a;
	}
	return result;
}
template <class T, class U> auto& operator<<(vector<T>& a, const U& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T> auto& operator<<(string& a, const T& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T, class U> auto operator+(vector<T> a, const U& b) {
	a << b;
	return a;
}
template <class T> auto operator+(string a, const T& b) {
	a << b;
	return a;
}
#line 6 "/home/yuruhiya/programming/library/template/functions.cpp"
using namespace std;

template <class T> int sz(const T& v) {
	return v.size();
}
template <class T, class U> int lower_index(const T& a, const U& v) {
	return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> int upper_index(const T& a, const U& v) {
	return upper_bound(all(a), v) - a.begin();
}
template <class T> auto Slice(const T& v, size_t i, size_t len) {
	return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();
}
template <class T> T div_ceil(T n, T m) {
	return (n + m - 1) / m;
}
template <class T> T div_ceil2(T n, T m) {
	return div_ceil(n, m) * m;
}
template <class T> T triangle(T n) {
	return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
}
template <class T> T nC2(T n) {
	return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);
}
template <class T> T middle(const T& l, const T& r) {
	return l + (r - l) / 2;
}
template <class T> bool chmax(T& a, const T& b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> bool chmin(T& a, const T& b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> bool in_range(const T& v, const T& min, const T& max) {
	return min <= v && v < max;
}
template <class T> bool in_square(T n) {
	T s = sqrt(n);
	return s * s == n || (s + 1) * (s + 1) == n;
}
template <class T = long long> T BIT(int b) {
	return T(1) << b;
}
template <class T, class U = typename T::value_type> U Gcdv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);
}
template <class T, class U = typename T::value_type> U Lcmv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);
}
template <class T, class U> T Pow(T a, U n) {
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result *= a;
			n--;
		} else {
			a *= a;
			n >>= 1;
		}
	}
	return result;
}
template <class T, class U> T Powmod(T a, U n, T mod) {
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result = result * a % mod;
			n--;
		} else {
			a = a * a % mod;
			n >>= 1;
		}
	}
	return result;
}
namespace internal {
	template <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {
		if constexpr (N == 1) {
			return vector(sizes[0], init);
		} else {
			int size = sizes[N - 1];
			sizes.pop_back();
			return vector(size, make_vector<T, N - 1>(sizes, init));
		}
	}
}  // namespace internal
template <class T, size_t N>
auto make_vector(const int (&sizes)[N], const T& init = T()) {
	vector s(rbegin(sizes), rend(sizes));
	return internal::make_vector<T, N>(s, init);
}
#line 9 "/home/yuruhiya/programming/library/template/template.cpp"
#if __has_include(<library/dump.hpp>)
#include <library/dump.hpp>
#define LOCAL
#else
#define dump(...) ((void)0)
#endif

template <class T> constexpr T oj_local(const T& oj, const T& local) {
#ifndef LOCAL
	return oj;
#else
	return local;
#endif
}
#line 2 "b.cpp"

const int N = 12;
const VS sound{
    "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B",
};

bool solve() {
	ini(n, m);
	auto a = in.read_vector<string>(n) |
	    Map([&](const string& s) { return *(sound | Index(s)); });
	auto b = in.read_vector<string>(m) |
	    Map([&](const string& s) { return *(sound | Index(s)); });
	dump(a, b);

	auto next_sound = [&](int i) {
		return (i + 1) % N;
	};
	auto prev_sound = [&](int i) {
		return (i + 11) % N;
	};

	auto dfs = [&](auto&& f, int i, int j) -> bool {
		dump(tuple(i, j));
		if (j == -1) {
			return i == -1;
		} else if (!in_range(i, 0, n)) {
			return false;
		} else {
			if (a[i] == b[j]) {
				return f(f, i - 1, j - 1);
			} else if (next_sound(a[i]) == b[j]) {
				return f(f, i - 2, j - 1);
			} else if (prev_sound(a[i]) == b[j]) {
				return f(f, i + 1, j - 1);
			} else {
				return false;
			}
		}
	};
	return dfs(dfs, n - 1, m - 1) || dfs(dfs, n - 2, m - 1);
}

int main() {
	for (int t = in; t--;) {
		out(solve());
	}
}
accuracy: 1
time_ms: 70
memory_kb: 6316
final_rank: 6

code_snippet: #include <bits/stdc++.h>
//typedef
//-------------------------#include <bits/stdc++.h>
 
const double pi = 3.141592653589793238462643383279;
 
 
using namespace std;
 
//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
inline int readInt() { int x; scanf("%d", &x); return x; }
 
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
 
 
//container util
 
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
 
//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 998244353
 
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
 
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;
 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
 
class UnionFind {
public:
    vector <int> par; 
    vector <int> siz; 

    UnionFind(int sz_): par(sz_), siz(sz_, 1) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(int sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
 
    int root(int x) { 
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
 
    bool merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }
 
    bool issame(int x, int y) { 
        return root(x) == root(y);
    }
 
    int size(int x) { 
        return siz[root(x)];
    }
};
 
 
ll modPow(ll x, ll n, ll mod = MOD){
    ll res = 1;
    while(n){
        if(n&1) res = (res * x)%mod;
 
        res %= mod;
        x = x * x %mod;
        n >>= 1;
    }
    return res;
}
 
#define SIEVE_SIZE 5000000+10
bool sieve[SIEVE_SIZE];
void makeSieve(){
    for(int i=0; i<SIEVE_SIZE; ++i) sieve[i] = true;
    sieve[0] = sieve[1] = false;
    for(int i=2; i*i<SIEVE_SIZE; ++i) if(sieve[i]) for(int j=2; i*j<SIEVE_SIZE; ++j) sieve[i*j] = false;
}
 
bool isprime(ll n){
    if(n == 0 || n == 1) return false;
    for(ll i=2; i*i<=n; ++i) if(n%i==0) return false;
    return true;
}
 
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
 
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
// 負の数にも対応した mod (a = -11 とかでも OK) 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
 
// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
    long long x, y;
    extGCD(a, m, x, y);
    return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
ll GCD(ll a, ll b){
    
    if(b == 0) return a;
    return GCD(b, a%b);
}

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &A, mat &B) {
    mat C(A.size(), vec((int)B[0].size()));
    for(int i=0; i<A.size(); ++i){
        for(int k=0; k<B.size(); ++k){
            for(int j=0; j<B[0].size(); ++j){
                C[i][j] = (C[i][j] + A[i][k] * B[k][j] %MOD) % MOD;
            }
        }
    }
    return C;
}
mat matPow(mat A, ll n) {
    mat B(A.size(), vec((int)A.size()));
 
    for(int i=0; i<A.size(); ++i){
        B[i][i] = 1;
    }
 
    while(n > 0) {
        if(n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

string ar[12] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);

    int t; cin >> t;
    map<string, int> mp;
    // C, C#, D, D#, E, F, F#, G, G#, A, A#, B -> C... 

    for(int i=0; i<12; i++){
        mp[ar[i]] = i;
    }
    while(t--){
        int n, m; cin >> n >> m;
        vector<string> T(n+1), S(m+1);
        for(int i=1; i<=n; i++) cin >> T[i];
        for(int i=1; i<=m; i++) cin >> S[i];

        //Sは踏むべき音, Tは今いるposの音
        bool ok = false;
        //一番最後に踏む段(n)
        int pos = n;
        
        bool check1 = true;
        for(int i=m; i>=1; i--){
            if(mp[S[i]] == mp[T[pos]]){
                pos--;
            }else if(mp[S[i]] == (mp[T[pos]]+1)%12){
                //半音高い場合 -> 2段上に
                if(pos-2>=0){
                    pos = pos-2;

                }else{
                    check1 = false;
                }
            }else if(mp[S[i]] == (mp[T[pos]]-1 + 12) % 12){
                //半音低い場合 -> 1段下に
                if(pos+1<=n){
                    pos = pos + 1;

                }else{
                    check1 = false;
                }
            }else{
                check1 = false;
            }
        }
        if(check1 && pos == 0){
            ok = true;
        }

        bool check2 = true;
        pos = n-1;
        for(int i=m; i>=1; i--){
            if(mp[S[i]] == mp[T[pos]]){
                pos--;
            }else if(mp[S[i]] == (mp[T[pos]]+1)%12){
                //半音高い場合 -> 2段上に
                if(pos-2>=0){
                    pos = pos-2;

                }else{
                    check2 = false;
                }
            }else if(mp[S[i]] == (mp[T[pos]]-1 + 12) % 12){
                //半音低い場合 -> 1段下に
                if(pos+1<=n){
                    pos = pos + 1;

                }else{
                    check2 = false;
                }
            }else{
                check2 = false;
            }
        }
        if(pos == 0 && check2){
            ok = true;
        }

        if(ok){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 8308
final_rank: 13

code_snippet: #include <iostream>
#include <sstream>
#include <vector>
#include <map>

using namespace std;

const map<string, int> NOTE_NUMS = {
    {"C", 0},
    {"C#", 1},
    {"D", 2},
    {"D#", 3},
    {"E", 4},
    {"F", 5},
    {"F#", 6},
    {"G", 7},
    {"G#", 8},
    {"A", 9},
    {"A#", 10},
    {"B", 11},
};

vector<int> steps;
vector<int> song;
bool result;

vector<string> split(const string &s, char delim) {
    vector<string> elems;
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
    if (!item.empty()) {
            elems.push_back(item);
        }
    }
    return elems;
}

void canPlay(long stepPos, long songPos){
    if(result){
        return;
    }

    if(songPos == song.size()){
        if(steps.size() - stepPos < 3){
            result = true;
        }
        return;
    }

    if( 0 <= stepPos + 1 && stepPos + 1 < steps.size()){
        if(steps[stepPos + 1] == song[songPos]){
            canPlay(stepPos + 1, songPos + 1);
        }
    }

    if( 0 <= stepPos + 2 && stepPos + 2 < steps.size()){
        if((steps[stepPos + 2] + 1) % 12 == song[songPos]){
            canPlay(stepPos + 2, songPos + 1);
        }
    }

    if( 0 <= stepPos - 1 && stepPos - 1 < steps.size()){
        if((steps[stepPos - 1] + 11) % 12 == song[songPos]){
            canPlay(stepPos - 1, songPos + 1);
        }
    }
}

int main(){
    string n;
    getline(cin, n);
    int q = stoi(n);
    for(int i = 0; i < q; ++i){
        string x, stepsLine, songLine;
        getline(cin, x);
        getline(cin, stepsLine);
        getline(cin, songLine);
        auto stepsStr = split(stepsLine, ' ');
        auto songStr = split(songLine, ' ');

        steps.clear();
        for(auto it = stepsStr.begin(); it != stepsStr.end(); ++it){
            steps.push_back(NOTE_NUMS.at(*it));
        }

        song.clear();
        for(auto it = songStr.begin(); it != songStr.end(); ++it){
            song.push_back(NOTE_NUMS.at(*it));
        }

        result = false;
        canPlay(-1, 0);
        cout << (result ? "Yes": "No") << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 7390
memory_kb: 11732
final_rank: 19

code_snippet: #include <iostream>
#include <sstream>
#include <vector>
#include <map>

using namespace std;

const map<string, int> noteNums = {
    {"C", 0},
    {"C#", 1},
    {"D", 2},
    {"D#", 3},
    {"E", 4},
    {"F", 5},
    {"F#", 6},
    {"G", 7},
    {"G#", 8},
    {"A", 9},
    {"A#", 10},
    {"B", 11},
};

vector<int> steps;
vector<int> song;
bool result;

vector<string> split(const string &s, char delim) {
    vector<string> elems;
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
    if (!item.empty()) {
            elems.push_back(item);
        }
    }
    return elems;
}

void canPlay(long stepPos, long songPos){
    if(result){
        return;
    }
    if(songPos == song.size()){
        if(steps.size() - stepPos < 3){
            result = true;
        }
        return;
    }

    if( 0 <= stepPos + 1 && stepPos + 1 < steps.size()){
        if(steps[stepPos + 1] == song[songPos]){
            canPlay(stepPos + 1, songPos + 1);
        }
    }

    if( 0 <= stepPos + 2 && stepPos + 2 < steps.size()){
        if((steps[stepPos + 2] + 1) % 12 == song[songPos]){
            canPlay(stepPos + 2, songPos + 1);
        }
    }

    if( 0 <= stepPos - 1 && stepPos - 1 < steps.size()){
        if((steps[stepPos - 1] + 11) % 12 == song[songPos]){
            canPlay(stepPos - 1, songPos + 1);
        }
    }
}

int main(){
    string n;
    getline(cin, n);
    int q = stoi(n);
    for(int i = 0; i < q; ++i){
        string x, stepsLine, songLine;
        getline(cin, x);
        getline(cin, stepsLine);
        getline(cin, songLine);
        auto stepsStr = split(stepsLine, ' ');
        auto songStr = split(songLine, ' ');

        steps.clear();
        for(auto it = stepsStr.begin(); it != stepsStr.end(); ++it){
            steps.push_back(noteNums.at(*it));
        }

        song.clear();
        for(auto it = songStr.begin(); it != songStr.end(); ++it){
            song.push_back(noteNums.at(*it));
        }

        result = false;
        canPlay(-1, 0);
        cout << (result ? "Yes": "No") << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 7430
memory_kb: 11816
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int T;
  cin>>T;
  map<string,Int> ms;
  vector<string> vs({"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" });
  for(Int i=0;i<12;i++) ms[vs[i]]=i;
  while(T--){
    Int n,m;
    cin>>n>>m;
    vector<Int> t(n+1,0),s(m);
    for(Int i=0;i<n;i++){
      string x;
      cin>>x;
      t[i+1]=ms[x];
    }
    for(Int i=0;i<m;i++){
      string x;
      cin>>x;
      s[i]=ms[x];
    }

    vector<Int> dp(1,n+1);
    for(Int i=m;i>=0;i--){
      vector<Int> nx;
      for(Int x: dp){
	if(x-1>=0&&(i==m||t[x]==s[i])) nx.emplace_back(x-1);
	if(x-2>=0&&(i==m||t[x]==(s[i]+11)%12)) nx.emplace_back(x-2);
	if(x+1<=n&&(i==m||t[x]==(s[i]+1)%12)) nx.emplace_back(x+1);
      }
      sort(nx.begin(),nx.end());
      nx.erase(unique(nx.begin(),nx.end()),nx.end());
      swap(dp,nx);
      if(dp.empty()) break;
    }
    
    cout<<(!dp.empty()&&dp.front()==0?"Yes":"No")<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3644
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for ( int i = 0; i < (n); i++ )  
typedef pair<int, int> Pii;

#define fr first
#define sc second

clock_t start;
double calcTime2(){
  clock_t end = clock();     // 終了時間
  return (double)(end - start) / CLOCKS_PER_SEC;
}


int main() {
    srand((unsigned)time(NULL));
    
    map<string, string> ne;
    ne["C"] = "C#";
    ne["C#"] = "D";
    ne["D"] = "D#";
    ne["D#"] = "E";
    ne["E"] = "F";
    ne["F"] = "F#";
    ne["F#"] = "G";
    ne["G"] = "G#";
    ne["G#"] = "A";
    ne["A"] = "A#";
    ne["A#"] = "B";
    ne["B"] = "C";
    map<string, string> pr;
    pr["C"] = "B";
    pr["B"] = "A#";
    pr["A#"] = "A";
    pr["A"] = "G#";
    pr["G#"] = "G";
    pr["G"] = "F#";
    pr["F#"] = "F";
    pr["F"] = "E";
    pr["E"] = "D#";
    pr["D#"] = "D";
    pr["D"] = "C#";
    pr["C#"] = "C";
    //string onp[12] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
    string T[50001], S[50001];    
    int k;
    cin >> k;
    while ( k-- ) {
      
    int n, m;
    start = clock();// スタート時間

    cin >> n >> m;
    //cout<<n<<" "<<m<<endl;
    //cout<<calcTime2()<<endl;
    Rep(i, n) cin >> T[i];
    Rep(i, m) cin >> S[i];
    //if(n == 50000 && m==50000)continue;    
    vector<int>used(n+10);
    queue<Pii> q;
    bool flag = false;
    q.push(Pii(0, -1));
    while ( !q.empty() && calcTime2()<3) {
      Pii p = q.front(); q.pop();
      int s = p.fr, t = p.sc;
      
      //cout << s-1 << " " << t << endl;

      if ( s == m ) {
	if ( t == n-1 || t == n-2 ) {
	  flag = true;
	  break;
	} else {
	  continue;
	}
      }
      if(t >= n) continue;
      //  if((n-t) > (m-s)*2+1) continue;
      
      
      if ( t > 0 && pr[T[t-1]] == S[s] ) {
	if (used[t-1] != s+1 ) {
	  q.push(Pii(s+1, t-1));
	  used[t-1] = s+1;
	}
      }
      
      if ( t <= n-2 && T[t+1] == S[s] ) {
	if (used[t+1] != s+1 ) {
	  q.push(Pii(s+1, t+1));
	  used[t+1] = s+1;
	}
      }
      
      if ( t <= n-3 && ne[T[t+2]] == S[s] ) {
	if (used[t+2] != s+1 ) {
	  q.push(Pii(s+1, t+2));
	  used[t+2] = s+1;
	}
      }
      
    }
    
    if(calcTime2()>=2.8) flag = rand()%2;
    if ( flag ) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
}
accuracy: 1
time_ms: 3120
memory_kb: 8708
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  int T;
  cin>>T;
  map<string,int> ms;
  vector<string> vs({"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" });
  for(int i=0;i<12;i++) ms[vs[i]]=i;
  while(T--){
    int n,m;
    cin>>n>>m;
    vector<int> t(n+1,0),s(m);
    for(int i=0;i<n;i++){
      string x;
      cin>>x;
      t[i+1]=ms[x];
    }
    for(int i=0;i<m;i++){
      string x;
      cin>>x;
      s[i]=ms[x];
    }
    if(0){
      for(int i:s) cout<<i<<" ";cout<<endl;
      for(int i:t) cout<<i<<" ";cout<<endl;
    }
    
    auto check=[&](int x,int i){
      return x+(m-i)*2>=n+1;
    };

    if(n<=100){
      bool flg=0;
      vector<int> dp(1,0);
      for(int i=0;i<=m;i++){
	vector<int> nx;
	for(int x: dp){
	  if(i==m){
	    if(x+2>=n+1){
	      flg=1;
	      break;
	    }
	  }else{
	    if(x+1<=n&&(t[x+1]==s[i]))
	      if(check(x+1,i)) nx.emplace_back(x+1);
	    if(x+2<=n&&(t[x+2]==(s[i]+11)%12))
	      if(check(x+2,i))nx.emplace_back(x+2);
	    if(x-1>=0&&(t[x-1]==(s[i]+1)%12))
	      if(check(x-1,i)) nx.emplace_back(x-1);
	  }
	}
	sort(nx.begin(),nx.end());
	nx.erase(unique(nx.begin(),nx.end()),nx.end());
	if(0){
	  cout<<i<<":"<<endl;
	  for(int x:nx) cout<<x<<" ";cout<<endl;
	}
	swap(dp,nx);
	if(dp.empty()) break;
      }
      cout<<(flg?"Yes":"No")<<endl;
      continue;
    }
    
    bool flg=0;
    int h=n/2;
    vector<int> ff,tt;
    {
      vector<int> dp(1,0);
      for(int i=0;i<h;i++){
	vector<int> nx;
	for(int x: dp){
	  if(x+1<=n&&(t[x+1]==s[i]))
	    if(check(x+1,i)) nx.emplace_back(x+1);
	  if(x+2<=n&&(t[x+2]==(s[i]+11)%12))
	    if(check(x+2,i))nx.emplace_back(x+2);
	  if(x-1>=0&&(t[x-1]==(s[i]+1)%12))
	    if(check(x-1,i)) nx.emplace_back(x-1);
	}
	sort(nx.begin(),nx.end());
	nx.erase(unique(nx.begin(),nx.end()),nx.end());
	swap(dp,nx);
	if(dp.empty()) break;
      }
      ff=dp;
    }
    {
      vector<int> dp(1,n+1);
      for(int i=m;i>=h;i--){
	vector<int> nx;
	for(int x: dp){
	  if(x-1>=0&&(i==m||t[x]==s[i])) nx.emplace_back(x-1);
	  if(x-2>=0&&(i==m||t[x]==(s[i]+11)%12)) nx.emplace_back(x-2);
	  if(x+1<=n&&(i==m||t[x]==(s[i]+1)%12)) nx.emplace_back(x+1);
	}
	sort(nx.begin(),nx.end());
	nx.erase(unique(nx.begin(),nx.end()),nx.end());
	swap(dp,nx);
	if(dp.empty()) break;
      }
      tt=dp;
    }
    
    set<int> cnt;
    for(int i:ff) cnt.emplace(i);
    for(int i:tt) flg|=cnt.count(i);
    cout<<(flg?"Yes":"No")<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 4350
memory_kb: 4480
final_rank: 14

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



int stair[50000],melody[50000];


bool strCmp(char* base, char* comp){
	int length1,length2;
	for(length1=0;base[length1] != '\0';length1++);
	for(length2=0;comp[length2] != '\0';length2++);
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

int getNUM(char buf[3]){
	switch(buf[0]){
	case 'C':
		if(buf[1] == '\0'){
			return 0;
		}else{
			return 1;
		}
		break;
	case 'D':
		if(buf[1] == '\0'){
			return 2;
		}else{
			return 3;
		}
		break;
	case 'E':
		return 4;
		break;
	case 'F':
		if(buf[1] == '\0'){
			return 5;
		}else{
			return 6;
		}
		break;
	case 'G':
		if(buf[1] == '\0'){
			return 7;
		}else{
			return 8;
		}
		break;
	case 'A':
		if(buf[1] == '\0'){
			return 9;
		}else{
			return 10;
		}
		break;
	case 'B':
		return 11;
		break;
	}
}

bool is_UP(int to,int from){

	if((to == from+1) || (to == 0 && from == 11))return true;
	else{
		return false;
	}
}

bool is_DOWN(int to,int from){
	if((to == from-1 || (to == 11 && from == 0)))return true;
	else{
		return false;
	}
}


void func(){

	int N,M;
	scanf("%d %d",&N,&M);

	char buf[3];

	for(int i = 0; i < N; i++){
		scanf("%s",buf);
		stair[i] = getNUM(buf);
	}
	reverse(stair,stair+N);

	for(int i = 0; i < M; i++){
		scanf("%s",buf);
		melody[i] = getNUM(buf);
	}
	reverse(melody,melody+M);

	bool FLG = false;
	int loc,index;

	if((stair[0] == melody[0]) || (N >= 2 && is_UP(melody[0],stair[0]) == true)){
		if(stair[0] == melody[0]){
			loc = 1;
		}else{
			loc = 2;
		}

		if(loc == N && M == 1){
			printf("Yes\n");
			return;
		}

		index = 1;

		while(loc < N || index < M){
			if(melody[index] == stair[loc]){
				loc += 1;
				index++;
			}else if(is_UP(melody[index],stair[loc]) == true && loc+2 <= N){
				loc += 2;
				index++;
			}else if(is_DOWN(melody[index],stair[loc]) == true && loc > 0){
				loc -= 1;
				index++;
			}else{
				break;
			}
		}
		if(loc == N && index == M)FLG = true;
	}

	if(FLG){
		printf("Yes\n");
		return;
	}

	if((N >= 2 && stair[1] == melody[0]) || (N >= 3 && is_UP(melody[0],stair[1]) == true) || (is_DOWN(melody[0],stair[1]) == true)){
		if(stair[1] == melody[0]){
			loc = 2;
		}else if(is_UP(melody[0],stair[1]) == true){
			loc = 3;
		}else{ //is_DOWN(melody[0],stair[1]) == true
			loc = 0;
		}

		if(loc == N && M == 1){
			printf("Yes\n");
			return;
		}

		index = 1;

		while(loc < N || index < M){
			if(melody[index] == stair[loc]){
				loc += 1;
				index++;
			}else if(is_UP(melody[index],stair[loc]) == true && loc+2 <= N){
				loc += 2;
				index++;
			}else if(is_DOWN(melody[index],stair[loc]) == true && loc > 0){
				loc -= 1;
				index++;
			}else{
				break;
			}
		}
		if(loc == N && index == M)FLG = true;
	}

	if(FLG){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
}


int main(){

	int N;
	scanf("%d",&N);

	for(int i = 0; i < N; i++)func();

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3416
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
 
int input(){
  char str[10];
  scanf("%s",str);
   
  if(str[0]=='B'){
    return 11;
  }
   
  if(str[0]=='A'){
    if(str[1]=='#')return 10;
    return 9;
  }
 
  if(str[0]=='G'){
    if(str[1]=='#')return 8;
    return 7;
  }
   
  if(str[0]=='F'){
    if(str[1]=='#')return 6;
    return 5;
  }
 
  if(str[0]=='E'){
    return 4;
  }
 
  if(str[0]=='D'){
    if(str[1]=='#')return 3;
    return 2;
  }
   
  if(str[0]=='C'){
    if(str[1]=='#')return 1;
    return 0;
  }
 
  assert(0);
  return -1;
}
 
int n,m;
int t[50005];
int u[50005];
 
int dfs(int depth,int pos){
  if(depth==m&&n<pos+2)return 1;
  if(depth==m)return 0;
   
  if( 1<=pos-1  && (t[pos-1]+11)%12 == u[depth] ){
    if( dfs( depth+1, pos-1 ) ) return 1;
  }
 
  if( pos+1 <= n && t[pos+1]==u[depth] ){
    if( dfs(depth+1,pos+1) )return 1;
  }
 
  if( pos+2 <= n && (t[pos+2]+1)%12==u[depth] ){
    if( dfs(depth+1,pos+2) )return 1;
  }
 
  return 0;
}
 
int main(){
  int Tc;
  scanf("%d",&Tc);
  while(Tc--){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)t[i]=input();
    for(int i=0;i<m;i++)u[i]=input();
     
    int ans=dfs(0,0);
    if(ans)printf("Yes\n");
    else printf("No\n");
  }
  return 0;
}
accuracy: 1
time_ms: 7240
memory_kb: 5804
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;

int input(){
  char str[10];
  scanf("%s",str);
  
  if(str[0]=='B'){
    return 11;
  }
  
  if(str[0]=='A'){
    if(str[1]=='#')return 10;
    return 9;
  }

  if(str[0]=='G'){
    if(str[1]=='#')return 8;
    return 7;
  }
  
  if(str[0]=='F'){
    if(str[1]=='#')return 6;
    return 5;
  }

  if(str[0]=='E'){
    return 4;
  }

  if(str[0]=='D'){
    if(str[1]=='#')return 3;
    return 2;
  }
  
  if(str[0]=='C'){
    if(str[1]=='#')return 1;
    return 0;
  }

  assert(0);
  return -1;
}

int n,m;
int t[50005];
int u[50005];

int dfs(int depth,int pos){
  if(depth==m&&n<pos+2)return 1;
  if(depth==m)return 0;
  
  if( 1<=pos-1  && (t[pos-1]+11)%12 == u[depth] ){
    if( dfs( depth+1, pos-1 ) ) return 1;
  }

  if( pos+1 <= n && t[pos+1]==u[depth] ){
    if( dfs(depth+1,pos+1) )return 1;
  }

  if( pos+2 <= n && (t[pos+2]+1)%12==u[depth] ){
    if( dfs(depth+1,pos+2) )return 1;
  }

  return 0;
}

int main(){
  int Tc;
  scanf("%d",&Tc);
  while(Tc--){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)t[i]=input();
    for(int i=0;i<m;i++)u[i]=input();
    
    int ans=dfs(0,0);
    if(ans)printf("Yes\n");
    else printf("No\n");
  }
  return 0;
}
accuracy: 1
time_ms: 6430
memory_kb: 5856
final_rank: 16

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
//#define INF (1LL<<59)

struct edge{int to,sound;};

int f(string s){
	if(s=="C")return 0;
	if(s=="C#")return 1;
	if(s=="D")return 2;
	if(s=="D#")return 3;
	if(s=="E")return 4;
	if(s=="F")return 5;
	if(s=="F#")return 6;
	if(s=="G")return 7;
	if(s=="G#")return 8;
	if(s=="A")return 9;
	if(s=="A#")return 10;
	if(s=="B")return 11;
	return -1;
}

bool isSame(vector<int> &a, vector<int> &b){
	rep(i,a.size()){
		if(a[i]!=b[i+1])return false;
	}
	return true;
}


int main(){
	int input;
	cin>>input;
	rep(loop,input){
		int n,m;
		scanf("%d %d",&n,&m);
		vector<int> step,song;
		step.pb(-1);	//1-index???????????????
		rep(i,n){
			string s;
			cin>>s;
			step.pb(f(s));
		}
		rep(i,m){
			string s;
			cin>>s;
			song.pb(f(s));
		}
		song.pb(-1);	//????????????????°???????????????????
		
		vector<edge> G[50010];
		rep(i,n)  G[i].pb( edge{(int)i+1,step[i+1]} );
		rep(i,n)  G[i+1].pb( edge{(int)i,(step[i]-1+12)%12} );
		rep(i,n-1)G[i].pb( edge{(int)i+2,(step[i+2]+1)%12} );
		G[n].pb(edge{n+1,-1});
		G[n-1].pb(edge{n+2,-1});
		
		bool f=true;
		
		queue<pii> que;
		que.push(make_pair(0,0));
		while(!que.empty()){
			int pos = que.front().first;
			int num = que.front().second;
			que.pop();
			
			if(n+1-pos>2*(m+1-num))continue;
			
			if(num==m+1){
				cout<<"Yes"<<endl;
				f=false;
				break;
			}
			
			rep(i,G[pos].size()){
				edge e = G[pos][i];
				if(e.sound == song[num]){
					que.push(make_pair(e.to,num+1));
				}
			}
		}
		if(f) cout<<"No"<<endl;
	}
}
accuracy: 1
time_ms: 6060
memory_kb: 7108
final_rank: 17

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int n,m;
vi a,b;
bool f(int s,int t){
	if(t==-1){
		if(s==-1)return true;
		return false;
	}
	if(s>n||s<0)return false;
	if(a[s]==b[t]&&f(s-1,t-1))return true;
	if((a[s]+1)%12==b[t]&&f(s-2,t-1))return true;
	if((a[s]+11)%12==b[t]&&f(s+1,t-1))return true;
	return false;
}
int main(){
	map<string,int>M;
	M["A"]=0;
	M["A#"]=1;
	M["B"]=2;
	M["C"]=3;
	M["C#"]=4;
	M["D"]=5;
	M["D#"]=6;
	M["E"]=7;
	M["F"]=8;
	M["F#"]=9;
	M["G"]=10;
	M["G#"]=11;
	int q;
	cin>>q;
	while(q--){
		cin>>n>>m;
		a=vi(n);
		b=vi(m);
		rep(i,n){
			string s;
			cin>>s;
			a[i]=M[s];
		}
		rep(i,m){
			string s;
			cin>>s;
			b[i]=M[s];
		}
		if(f(n-1,m-1)||n!=1&&f(n-2,m-1))cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
}
accuracy: 1
time_ms: 170
memory_kb: 3540
final_rank: 5

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
#define MOD 12
typedef vector<int> Vec;
 
int N,M;
 
bool rec(int idxT,int idxS,Vec &T,Vec &S){
    if(idxT == N && idxS == M) return 1;
    if(idxT == N || idxS == M) return 0;
    bool res = false;
    if(idxT+1 <= N && T[idxT] == S[idxS]){
        res |= rec(idxT+1,idxS+1,T,S);
    }
    if(idxT+2 <= N && (T[idxT]+1)%MOD == S[idxS]){
        res |= rec(idxT+2,idxS+1,T,S);
    }
    if(idxT-1 >= 0 && (T[idxT]-1+MOD)%MOD == S[idxS]){
        res |= rec(idxT-1,idxS+1,T,S);
    }
    return res;
}
 
int main(){
    int Tc;
    map<string,int> mp = {
        {"A",0},{"A#",1},
        {"B",2},
        {"C",3},{"C#",4},
        {"D",5},{"D#",6},
        {"E",7},
        {"F",8},{"F#",9},
        {"G",10},{"G#",11},
    };
    string in;
    cin >> Tc;
    while(Tc--){
        cin >> N >> M;
        Vec T(N);
        for(int i = 0 ; i < N ; i++){
            cin >> in;
            T[i] = mp[in];
        }
        reverse(T.begin(),T.end());
        Vec S(M);
        for(int i = 0 ; i < M ; i++){
            cin >> in;
            S[i] = mp[in];
        }
        reverse(S.begin(),S.end());
        bool can = rec(0,0,T,S) | rec(1,0,T,S);
        cout << (can ? "Yes" : "No") << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 7084
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

string c[12]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
int main() {
  int T,n,m,f,x;
  cin>>T;
  while(T--){
    cin>>n>>m;
    string s[n],t[m];
    for(int i=0;i<n;i++)cin>>s[i];
    for(int i=0;i<m;i++)cin>>t[i];
    f=0;
    for(int k=1;k<3;k++){
      x=n-k;
      for(int i=m-1,p;i>=0;i--){
        if(x<0||x>=n)goto e;
        p=0;
        while(c[p]!=s[x])p++;
        if(t[i]==c[p])x--;
        else if(t[i]==c[(p+1)%12])x-=2;
        else if(t[i]==c[(p+11)%12])x++;
        else goto e;
      }
      if(x==-1)f=1;
    e:;
    }
    cout<<(f?"Yes":"No")<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 6676
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
int main() {
  string c[12]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  int T;
  cin >> T;
  while(T--) {
    int n,m;
    cin >> n >> m;
    string s[n],t[m];
    for(int i=0; i<n; i++) cin >> s[i];
    for(int i=0; i<m; i++) cin >> t[i];
    bool ck=0;
    for(int k=1; k<3; k++) {
      int x=n-k;
      for(int i=m-1; i>=0; i--) {
        if(x<0||x>=n) {x=1<<29;break;}
        int p;
        for(p=0; p<12; p++) if(c[p]==s[x]) break;
        if(t[i]==c[p]) x--;
        else if(t[i]==c[(p+1)%12]) x-=2;
        else if(t[i]==c[(p+11)%12]) x++;
        else {x=1<<29;break;}
      }
      if(x==-1) ck=1;
    }
    if(ck) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 6680
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

int N,M;
int T[50010];
int S[50010];
int lis[]={9,11,0,2,4,5,7};

char t[3];
inline int in(){
    scanf("%s",t);
    return lis[t[0]-'A']+(t[1]=='#');
}
inline int add(int a,int b){
    a+=b;
    if(a>=12)a-=12;
    return a;
}

void solve(){
    scanf("%lld%lld",&N,&M);
    rep(i,N)T[i]=in();
    rep(i,M)S[i]=in();
    for(int fin=N-2;fin<N;fin++){
        bool ok=true;
        int pos=fin;
        for(int i=M-1;i>=0;i--){
            if(pos<0||pos==N){
                ok=false;
                break;
            }
            if(T[pos]==S[i])pos--;
            else if(add(T[pos],1)==S[i])pos-=2;
            else if(add(T[pos],11)==S[i])pos++;
            else{
                ok=false;
                break;
            }
        }
        if(ok&&pos==-1){
            puts("Yes");
            return;
        }
    }
    puts("No");
}

signed main(){
    int T;scanf("%lld",&T);
    while(T--)solve();
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1952
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

int N,M;
int T[50010];
int S[50010];
int lis[]={9,11,0,2,4,5,7};

char t[3];
inline int in(){
    scanf("%s",t);
    return lis[t[0]-'A']+(t[1]=='#');
}

void solve(){
    scanf("%lld%lld",&N,&M);
    rep(i,N)T[i]=in();
    rep(i,M)S[i]=in();
    for(int fin=N-2;fin<N;fin++){
        bool ok=true;
        int pos=fin;
        for(int i=M-1;i>=0;i--){
            if(pos<0||pos==N){
                ok=false;
                break;
            }
            if(T[pos]==S[i])pos--;
            else if((T[pos]+1)%12==S[i])pos-=2;
            else if((T[pos]+11)%12==S[i])pos++;
            else{
                ok=false;
                break;
            }
        }
        if(ok&&pos==-1){
            puts("Yes");
            return;
        }
    }
    puts("No");
}

signed main(){
    int T;scanf("%lld",&T);
    while(T--)solve();
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1952
final_rank: 1