Language: C++

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <utility>
#include <tuple>
using namespace std;

using Graph = vector< vector<int> >;

void read_field(vector<char> &board, Graph &G) {
  int N, M; cin >> N >> M;
  board.resize(N);
  G.resize(N);

  for(int i=0; i<N; i++) {
    cin >> board[i];
  }

  for(int i=0; i<M; i++) {
    int u, v; cin >> u >> v;
    u--; v--;

    // directed !!!
    G[u].push_back(v);
  }
}

int rec[110][110];
struct Elem {
  int u1, u2, cost;
};

int main() {
  vector<char> A_char, B_char;
  Graph A_Graph, B_Graph;
  read_field(A_char, A_Graph);
  read_field(B_char, B_Graph);

  bool operate_inf = false;
  int N1 = A_char.size(), N2 = B_char.size();

  queue< Elem > que;

  // equal
  for(int i=0; i<N1; i++) {
    for(int j=0; j<N2; j++) {
      if(A_char[i] == B_char[j]) {
        rec[i][j] = 1;
        que.push(Elem{i, j, 1});
      }
    }
  }

  while(que.size()) {
    int u1, u2, p_cost;

    u1 = que.front().u1;
    u2 = que.front().u2;
    p_cost = que.front().cost;
    que.pop();
    
    if(p_cost < rec[u1][u2]) continue;
    if(rec[u1][u2] > max(N1 + N2)) {
      operate_inf = true;
      break;
    }
    else {
      // move (both)
      for(auto v1 : A_Graph[u1]) {
        for(auto v2 : B_Graph[u2]) {
          char c1 = A_char[v1], c2 = B_char[v2];

          int cost = 0;
          if(c1 == c2) {
            cost = p_cost + 1;
          }
          else {
            cost = p_cost;
          }

          if(rec[v1][v2] < cost) {
            rec[v1][v2] = cost;
            que.push(Elem{v1, v2, cost});
          }
        }
      }

      if(A_char[u1] != B_char[u2]) {  
        // move (A)
        for(auto v1 : A_Graph[u1]) {
          int v2 = u2;

          char c1 = A_char[v1], c2 = B_char[v2];

          int cost = 0;
          if(c1 == c2) {
            cost = p_cost + 1;
          }
          else {
            cost = p_cost;
          }

          if(rec[v1][v2] < cost) {
            rec[v1][v2] = cost;
            que.push(Elem{v1, v2, cost});
          }
        }

        // move (B)
        for(auto v2 : B_Graph[u2]) {
          int v1 = u1;

          char c1 = A_char[v1], c2 = B_char[v2];

          int cost = 0;
          if(c1 == c2) {
            cost = p_cost + 1;
          }
          else {
            cost = p_cost;
          }

          if(rec[v1][v2] < cost) {
            rec[v1][v2] = cost;
            que.push(Elem{v1, v2, cost});
          }
        }
      }
    }
  }

  if(operate_inf) {
    cout << -1 << endl;
    return 0;
  }
  
  int ans = 0;
  for(int i=0; i<N1; i++) {
    for(int j=0; j<N2; j++) {
      // printf("%d ", rec[i][j]);
      ans = max(ans, rec[i][j]);
    }
    // cout << endl;
  }
  cout << ans << endl;
  return 0;
}




Compiler output:
a.cc: In function 'int main()':
a.cc:64:25: error: no matching function for call to 'max(int)'
   64 |     if(rec[u1][u2] > max(N1 + N2)) {
      |                      ~~~^~~~~~~~~
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:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 1 provided