Language: C++

#include<stdio.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <functional>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <string>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <queue>
#include<iomanip>
#include<bitset>
#include<stack>
#include<set>
#include<limits>
#include <complex>
using namespace std;

const int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : num(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) { ; }
	Mod(int n) : num((n % mod + mod) % mod) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	assert(b.num != 0);
	return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
	assert(b.num != 0);
	return a = a * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < MAX_N - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}
template<typename T>
vector<vector<T>> keisann(const vector<vector<T>>l, const vector<vector<T>>r) {
	vector<vector<T>>ans(l.size(), vector<T>(r[0].size()));
	assert(l[0].size() == r.size());
	for (unsigned int h = 0; h < l.size(); ++h) {
		for (unsigned int i = 0; i < r.size(); ++i) {
			for (unsigned int w = 0; w < r[0].size(); ++w) {
			
				ans[h][w] +=l[h][i] * r[i][w];
			}
		}
	}
	return ans;
}
template<typename T>
vector<vector<T>>powgyou(vector<vector<T>>a, const long long int n) {
	assert(a.size() == a[0].size());
	if (!n) {
		vector<vector<T>>e(a.size(), vector<T>(a[0].size()));
		for (unsigned int i = 0; i < a.size(); ++i) {
			e[i][i] = 1;
		}
		return e;
	}
	if (n == 1)return a;
	else {
		vector<vector<T>>ans(a.size(), vector<T>(a[0].size(), 0));
		ans = powgyou(a, n / 2);
		ans = keisann(ans, ans);
		if (n % 2) {
			ans = keisann(ans, a);
		}
		return ans;
	}
}

long long int powint(long long int a, int b) {
	if (b == 0)return 1;
	if (b == 1)return a;
	else {
		long long int ans = 1;
		long long int c = powint(a, b / 2);
		ans *= c*c;
		ans *= (b % 2) ? a : 1;
		return ans;
	}
	
}

struct Used {
	int ap;
	int bp;
	int point;
};


vector<vector<int>>aedges(100);
vector<vector<int>>bedges(100);
vector<int>anums;
vector<int>bnums;

int memo[100][100];

int getans(int ap, int bp, vector<Used>&used, int nowp) {
	bool gp = false;
	if (anums[ap] == bnums[bp]) {
		nowp++;
		gp = true;
	}
	if (memo[ap][bp] >= 0)return memo[ap][bp];
	auto it = find_if(used.begin(), used.end(), [=](const Used& l) {return l.ap == ap&&l.bp == bp; });
	if (it != used.end()) {
		if (it->point < nowp)return memo[ap][bp] = 99999999;
		else {
			return nowp;
		}
	}
	int ans = 0;
	for (int i = 0; i <= aedges[ap].size(); ++i) {
		for (int j = 0; j <= bedges[bp].size(); ++j) {
			if (gp && ((i == aedges[ap].size()) || (j == bedges[bp].size())))continue;
			if (i == aedges[ap].size() && j == bedges[bp].size())continue;
			int naedge = (i == aedges[ap].size()) ? ap : aedges[ap][i];
			int nbedge = (j == bedges[bp].size() )? bp : bedges[bp][j];

			used.push_back(Used{ ap, bp, nowp });
			ans = max(ans,getans(naedge, nbedge, used, nowp));
			used.pop_back();
		}
	}
	return memo[ap][bp] = ans+ (anums[ap] == bnums[bp]);
}


int main(void) {
	memset(memo, -1, sizeof(memo));
	int aN, aM; cin >> aN >> aM;

	for (int i = 0; i < aN; ++i) {
		char c; cin >> c;
		anums.push_back(c - 'a');
	}
	for (int i = 0; i < aM; ++i) {
		int a, b; cin >> a >> b;
		a--; b--;
		aedges[a].push_back(b);
	}


	int bN, bM; cin >> bN >> bM;

	for (int i = 0; i < bN; ++i) {
		char c; cin >> c;
		bnums.push_back(c - 'a');
	}
	for (int i = 0; i < bM; ++i) {
		int a, b; cin >> a >> b;
		a--; b--;
		bedges[a].push_back(b);
	}
	int ans = 0;
	for (int i = 0; i < aN; ++i) {
		for (int j = 0; j < bN; ++j) {
			ans=max(ans,getans(i, j, vector<Used>(),0));
		}
	}
	if (ans >= 99999999)cout << -1 << endl;
	else {
		cout << ans << endl;
	}
	return 0;
}

/*
4
0 4
7 12
6 8
4 6

*/

Compiler output:
a.cc: In function 'int main()':
a.cc:204:50: error: cannot bind non-const lvalue reference of type 'std::vector<Used>&' to an rvalue of type 'std::vector<Used>'
  204 |                         ans=max(ans,getans(i, j, vector<Used>(),0));
      |                                                  ^~~~~~~~~~~~~~
a.cc:144:41: note:   initializing argument 3 of 'int getans(int, int, std::vector<Used>&, int)'
  144 | int getans(int ap, int bp, vector<Used>&used, int nowp) {
      |                            ~~~~~~~~~~~~~^~~~