alexjercan/bugnet huggingface dataset:
Problem p00870 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

// ok[i][j] := elm[i] が s[j] で受理されるか
bool ok[12][5000];
// dp[i][j] := s[i] までで j(mask) が受理されるか
bool dp[5000][1 << 12];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  while (cin >> n >> m, n || m) {
    vector<string> elm(n);
    for (int i = 0; i < n; i++)
      cin >> elm[i];
    string s = "";
    for (int i = 0; i < m; i++) {
      string a;
      cin >> a;
      s += a;
    }
    int m = s.size();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        ok[i][j] = false;
    for (int i = 0; i < m; i++)
      for (int j = 0; j < (1 << n); j++)
        dp[i][j] = false;
    for (int i = 0; i < n; i++) {
      int sz = elm[i].size();
      for (int j = 0; j <= m - sz; j++) {
        if (elm[i] == s.substr(j, sz)) {
          ok[i][j + sz - 1] = true;
          dp[j + sz - 1][1 << i] = true;
        }
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = 1; j < (1 << n); j++) {
        if (!dp[i][j])
          continue;
        for (int k = 0; k < n; k++) {
          int sz = elm[k].size();
          if (i + sz >= m)
            continue;
          if ((j >> k) & 1)
            continue;
          if (!ok[k][i + sz])
            continue;
          dp[i + sz][j ^ (1 << k)] = true;
        }
      }
    }
    int ret = 0;
    for (int i = 0; i < m; i++)
      if (dp[i][(1 << n) - 1])
        ret++;
    cout << ret << endl;
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

// ok[i][j] := elm[i] が s[j] で受理されるか
bool ok[12][5000];
// dp[i][j] := s[i] までで j(mask) が受理されるか
bool dp[5000][1 << 12];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  while (cin >> n >> m, n || m) {
    vector<string> elm(n);
    for (int i = 0; i < n; i++)
      cin >> elm[i];
    string s = "";
    for (int i = 0; i < m; i++) {
      string a;
      cin >> a;
      s += a;
    }
    int m = s.size();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        ok[i][j] = false;
    for (int i = 0; i < m; i++)
      for (int j = 0; j < (1 << n); j++)
        dp[i][j] = false;
    for (int i = 0; i < n; i++) {
      int sz = elm[i].size();
      for (int j = 0; j <= m - sz; j++) {
        if (elm[i] == s.substr(j, sz)) {
          ok[i][j + sz - 1] = true;
          dp[j + sz - 1][1 << i] = true;
        }
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = 1; j < (1 << n); j++) {
        for (int k = 0; k < n; k++) {
          int sz = elm[k].size();
          if (i - sz < 0)
            continue;
          if (((j >> k) & 1) == 0)
            continue;
          if ((j ^ (1 << k)) == 0)
            continue;
          if (dp[i - sz][j ^ (1 << k)] && ok[k][i])
            dp[i][j] = true;
        }
      }
    }
    int ret = 0;
    for (int i = 0; i < m; i++)
      if (dp[i][(1 << n) - 1])
        ret++;
    cout << ret << endl;
  }
}
Problem p00874 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int w, d;
  cin >> w >> d;
  while (w != 0) {
    vector<int> ws;
    vector<int> ds;
    int b, i, j, sum = 0;
    for (i = 0; i < w; i++) {
      cin >> b;
      ws.push_back(b);
    }
    for (i = 0; i < d; i++) {
      cin >> b;
      ds.push_back(b);
    }
    for (i = 0; i < ws.size(); i++) {
      for (j = 0; j < ds.size(); j++) {
        if (ws[i] == ds[j]) {
          sum += ws[i];
          // cout << sum;
          ws.erase(ws.begin() + i);
          ds.erase(ds.begin() + j);
          i = -1;
          j = -1;
        }
      }
    }
    for (i = 0; i < ws.size(); i++) {
      for (j = 0; j < ds.size(); j++) {
        if (ws[i] == ds[j]) {
          sum += ws[i];
          // cout << sum;
          ws.erase(ws.begin() + i);
          ds.erase(ds.begin() + j);
          i = -1;
          j = -1;
        }
      }
    }
    // cout << sum << endl;
    for (i = 0; i < ws.size(); i++) {
      sum += ws[i];
      // cout << ws[i] << endl;
    }
    // cout << sum << endl;
    for (i = 0; i < ds.size(); i++) {
      sum += ds[i];
      // cout << ds[i] << endl;
    }
    cout << sum << endl;
    // cout << w << d << endl;;
    cin >> w >> d;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int w, d;
  cin >> w >> d;
  while (w != 0) {
    vector<int> ws;
    vector<int> ds;
    int b, i, j, sum = 0;
    for (i = 0; i < w; i++) {
      cin >> b;
      ws.push_back(b);
    }
    for (i = 0; i < d; i++) {
      cin >> b;
      ds.push_back(b);
    }
    for (i = 0; i < ws.size(); i++) {
      for (j = 0; j < ds.size(); j++) {
        if (ws[i] == ds[j]) {
          sum += ws[i];
          // cout << sum;
          ws.erase(ws.begin() + i);
          ds.erase(ds.begin() + j);
          i = 0;
          j = 0;
        }
      }
    }
    for (i = 0; i < ws.size(); i++) {
      for (j = 0; j < ds.size(); j++) {
        if (ws[i] == ds[j]) {
          sum += ws[i];
          // cout << sum;
          ws.erase(ws.begin() + i);
          ds.erase(ds.begin() + j);
          i = 0;
          j = 0;
        }
      }
    }
    // cout << sum << endl;
    for (i = 0; i < ws.size(); i++) {
      sum += ws[i];
      // cout << ws[i] << endl;
    }
    // cout << sum << endl;
    for (i = 0; i < ds.size(); i++) {
      sum += ds[i];
      // cout << ds[i] << endl;
    }
    cout << sum << endl;
    cin >> w >> d;
  }
}
Problem p00874 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int w = -1, d = -1;
  int tmp;
  int ans;
  vector<int> hw(21), hd(21);
  for (;;) {
    ans = 0;
    cin >> w >> d;
    if (w == 0)
      break;
    for (int i = 0; i < 21; i++) {
      hw[i] = 0;
      hd[i] = 0;
    }
    for (int i = 0; i < w; i++) {
      cin >> tmp;
      hw[tmp]++;
    }
    for (int i = 0; i < d; i++) {
      cin >> tmp;
      hd[tmp]++;
    }
    for (int i = 1; i < 21; i++) {
      ans += i * max(hd[i], hw[i]);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int w = -1, d = -1;
  int tmp;
  int ans = 0;
  vector<int> hw(21, 0), hd(21, 0);
  while (w != 0) {
    cin >> w, d;
    for (int i = 0; i < w; i++) {
      cin >> tmp;
      hw[tmp]++;
    }
    for (int i = 0; i < d; i++) {
      cin >> tmp;
      hd[tmp]++;
    }
    for (int i = 1; i < 21; i++) {
      if (hw[i] != 0) {
        if (hw[i] > hd[i])
          ans += i * hw[i];
        else
          ans += i * hd[i];
      } else if (hd[i] != 0)
        ans += i * hd[i];
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p00875 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define DEB 1
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

pair<string, string> pss[12];
map<string, int> msi;

string subplace(string &s, int i) {
  int p = 0;
  string ret = s;
  while ((p = ret.find(pss[i].first, p)) != string::npos) {
    ret.replace(p, pss[i].first.length(), pss[i].second);
    p += pss[i].second.size();
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    msi.clear();
    rep(i, n) { cin >> pss[i].first >> pss[i].second; }
    string a, b;
    cin >> a >> b;

    int ans = -1;
    queue<pair<string, int>> q;
    q.push(mp(a, 0));
    msi[a] = 0;
    while (!q.empty()) {
      string s = q.front().first;
      int c = q.front().second;
      q.pop();
      if (s.length() > 1000)
        break;
      if (s == b) {
        ans = c;
        break;
      }
      rep(i, n) {
        string tmp = subplace(s, i);
        if (msi.find(tmp) == msi.end()) {
          q.push(mp(tmp, c + 1));
          msi[tmp] = c + 1;
        }
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define DEB 1
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

pair<string, string> pss[12];

string subplace(string &s, int i) {
  int p = 0;
  string ret = s;
  while ((p = ret.find(pss[i].first, p)) != string::npos) {
    ret.replace(p, pss[i].first.length(), pss[i].second);
    p += pss[i].second.size();
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    rep(i, n) { cin >> pss[i].first >> pss[i].second; }
    string a, b;
    cin >> a >> b;

    int ans = -1;
    queue<pair<string, int>> q;
    q.push(mp(a, 0));
    while (!q.empty()) {
      string s = q.front().first;
      int c = q.front().second;
      q.pop();
      if (s.length() > 1000)
        break;
      if (s == b) {
        ans = c;
        break;
      }
      rep(i, n) { q.push(mp(subplace(s, i), c + 1)); }
    }

    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00875 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<string> a(n), b(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    string ini, fin;
    cin >> ini >> fin;

    queue<pair<int, string>> q;
    q.push(make_pair(0, ini));
    bool success = false;
    while (!q.empty()) {
      int step = q.front().first;
      string str = q.front().second;
      q.pop();
      if (str == fin) {
        cout << step << endl;
        success = true;
        break;
      }
      for (int i = 0; i < n; i++) {
        string newstr = "";
        bool nochange = true;
        for (int j = 0; j < (int)str.length(); j++) {
          bool failed = true;
          for (int k = 0; k < (int)a[i].length() && j + k < (int)str.length();
               k++) {
            if (str[j + k] != a[i][k]) {
              break;
            } else if (k == (int)a[i].length() - 1) {
              newstr += b[i];
              j += a[i].length() - 1;
              nochange = false;
              failed = false;
            }
          }
          if (failed) {
            newstr += str[j];
          }
        }
        if (!nochange && newstr.length() <= 10) {
          q.push(make_pair(step + 1, newstr));
        }
      }
    }

    if (!success) {
      cout << -1 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<string> a(n), b(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    string ini, fin;
    cin >> ini >> fin;

    queue<pair<int, string>> q;
    q.push(make_pair(0, ini));
    bool success = false;
    while (!q.empty()) {
      int step = q.front().first;
      string str = q.front().second;
      q.pop();
      if (str == fin) {
        cout << step << endl;
        success = true;
        break;
      }
      for (int i = 0; i < n; i++) {
        string newstr = "";
        bool nochange = true;
        for (int j = 0; j < (int)str.length(); j++) {
          bool failed = false;
          for (int k = 0; k < (int)a[i].length() && j + k < (int)str.length();
               k++) {
            if (str[j + k] != a[i][k]) {
              failed = true;
              break;
            } else if (k == (int)a[i].length() - 1) {
              newstr += b[i];
              j += a[i].length() - 1;
              nochange = false;
            }
          }
          if (failed) {
            newstr += str[j];
          }
        }
        if (!nochange && newstr.length() <= 10) {
          q.push(make_pair(step + 1, newstr));
        }
      }
    }

    if (!success) {
      cout << -1 << endl;
    }
  }
  return 0;
}
Problem p00875 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct po {
  string a, b;
};
int n;
vector<po> str;
int ans;
string goal;
int cnt = 0;
set<string> memo;

void saiki(string y, int kai) {
  cnt++;
  // cout << y << endl;
  if (y == goal && ans > kai)
    ans = kai;

  if (ans > kai && y.size() < 11)
    for (int i = 0; i < n; i++) {
      string ima = y;
      for (int j = 0; j < ima.size(); j++) {
        if (str[i].a.size() > ima.size() - j)
          break;
        int flg = 0;
        for (int k = 0; k < str[i].a.size(); k++) {
          if (str[i].a[k] != ima[j + k]) {
            flg = 1;
            break;
          }
        }
        if (flg == 0) {
          string nx;
          int nj;
          for (int k = 0; k < ima.size(); k++) {
            if (k == j)
              nx += str[i].b, k += str[i].a.size() - 1, nj = nx.size() - 1;
            else
              nx += ima[k];
          }
          j = nj;
          ima = nx;
        }
      }
      if (y != ima && memo.count(ima) == 0) {
        memo.insert(ima);
        saiki(ima, kai + 1);
      }
    }
}

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;

    str.resize(n);
    for (int i = 0; i < n; i++)
      cin >> str[i].a >> str[i].b;

    memo.clear();
    string start;
    cin >> start;
    cin >> goal;

    ans = 10000000;
    saiki(start, 0);
    if (ans == 10000000)
      cout << -1 << endl;
    else
      cout << ans << endl;
    // cout <<cnt <<endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct po {
  string a, b;
};
int n;
vector<po> str;
int ans;
string goal;
int cnt = 0;
set<string> memo;

void saiki(string y, int kai) {
  cnt++;
  // cout << y << endl;
  if (y == goal && ans > kai)
    ans = kai;

  if (ans > kai && y.size() < 25)
    for (int i = 0; i < n; i++) {
      string ima = y;
      for (int j = 0; j < ima.size(); j++) {
        if (str[i].a.size() > ima.size() - j)
          break;
        int flg = 0;
        for (int k = 0; k < str[i].a.size(); k++) {
          if (str[i].a[k] != ima[j + k]) {
            flg = 1;
            break;
          }
        }
        if (flg == 0) {
          string nx;
          int nj;
          for (int k = 0; k < ima.size(); k++) {
            if (k == j)
              nx += str[i].b, k += str[i].a.size() - 1, nj = nx.size() - 1;
            else
              nx += ima[k];
          }
          j = nj;
          ima = nx;
        }
      }
      if (y != ima && memo.count(ima) == 0) {
        memo.insert(ima);
        saiki(ima, kai + 1);
      }
    }
}

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;

    str.resize(n);
    for (int i = 0; i < n; i++)
      cin >> str[i].a >> str[i].b;

    memo.clear();
    string start;
    cin >> start;
    cin >> goal;

    ans = 10000000;
    saiki(start, 0);
    if (ans == 10000000)
      cout << -1 << endl;
    else
      cout << ans << endl;
    // cout <<cnt <<endl;
  }

  return 0;
}
Problem p00875 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

struct State {
  string s;
  int count;

  State() {}
  ~State() {}

  State(string a, int b) : s(a), count(b) {}
};

bool solve() {
  int n;
  cin >> n;
  if (n == 0) {
    return false;
  }

  string a[10], b[10], gamma, delta;

  set<string> visited;

  for (int j = 0; j < n; ++j) {
    cin >> a[j] >> b[j];
  }
  cin >> gamma >> delta;

  queue<State> que;
  que.push(State(gamma, 0));

  int ret = -1;

  while (!que.empty()) {
    State state = que.front();
    que.pop();

    if (visited.find(state.s) != end(visited)) {
      continue;
    }
    visited.insert(state.s);

    if (state.s.size() > delta.size()) {
      continue;
    }

    if (state.s == delta) {
      ret = state.count;
      break;
    }

    for (int j = 0; j < n; ++j) {
      string t = state.s;
      int aLen = a[j].size();

      stringstream ss;
      for (int k = 0; k < t.size(); ++k) {
        if (k + aLen <= t.size() && t.substr(k, aLen) == a[j]) {
          ss << b[j];
          k += aLen - 1;
        } else {
          ss << t[k];
        }
      }

      que.push(State(ss.str(), state.count + 1));
    }
  }

  cout << ret << endl;
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

struct State {
  string s;
  int count;

  State() {}
  ~State() {}

  State(string a, int b) : s(a), count(b) {}
};

bool solve() {
  int n;
  cin >> n;
  if (n == 0) {
    return false;
  }

  string a[10], b[10], gamma, delta;

  for (int j = 0; j < n; ++j) {
    cin >> a[j] >> b[j];
  }
  cin >> gamma >> delta;

  queue<State> que;
  que.push(State(gamma, 0));

  int ret = -1;

  while (!que.empty()) {
    State state = que.front();
    que.pop();

    if (state.s.size() > delta.size()) {
      continue;
    }

    if (state.s == delta) {
      ret = state.count;
      break;
    }

    for (int j = 0; j < n; ++j) {
      string t = state.s;
      int aLen = a[j].size();

      stringstream ss;
      for (int k = 0; k < t.size(); ++k) {
        if (k + aLen <= t.size() && t.substr(k, aLen) == a[j]) {
          ss << b[j];
          k += aLen - 1;
        } else {
          ss << t[k];
        }
      }

      que.push(State(ss.str(), state.count + 1));
    }
  }

  cout << ret << endl;
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}
Problem p00875 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <string>
using namespace std;
int n;
string a[100], b[100], S, T;
queue<pair<string, int>> Q;
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i] >> b[i];
    cin >> S >> T;
    while (!Q.empty())
      Q.pop();
    Q.push(make_pair(S, 0));
    while (!Q.empty()) {
      pair<string, int> U = Q.front();
      Q.pop();
      string W = U.first;
      if (T == W) {
        cout << U.second << endl;
        goto E;
      }
      for (int i = 0; i < n; i++) {
        string V = W;
        for (int j = 0; j <= (int)V.size() - (int)a[i].size(); j++) {
          if (V.substr(j, a[i].size()) == a[i]) {
            V = V.substr(0, j) + b[i] +
                V.substr(j + a[i].size(), V.size() - (j + a[i].size()));
            j += b[i].size() - 1;
          }
        }
        if (W != V && V.size() <= T.size()) {
          Q.push(make_pair(V, U.second + 1));
        }
      }
    }
    cout << "-1" << endl;
  E:;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;
int n;
string a[100], b[100], S, T;
queue<pair<string, int>> Q;
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i] >> b[i];
    cin >> S >> T;
    while (!Q.empty())
      Q.pop();
    Q.push(make_pair(S, 0));
    while (!Q.empty()) {
      pair<string, int> U = Q.front();
      Q.pop();
      string W = U.first;
      if (T == W) {
        cout << U.second << endl;
        goto E;
      }
      for (int i = 0; i < n; i++) {
        string V = W;
        for (int j = 0; j <= V.size() - a[i].size(); j++) {
          if (V.substr(j, a[i].size()) == a[i]) {
            V = V.substr(0, j) + b[i] +
                V.substr(j + a[i].size(), V.size() - (j + a[i].size()));
            j += b[i].size() - 1;
          }
        }
        if (W != V && V.size() <= T.size()) {
          Q.push(make_pair(V, U.second + 1));
        }
      }
    }
    cout << "-1" << endl;
  E:;
  }
  return 0;
}
Problem p00875 (C++) — buggy submission, verdict: Time Limit Exceeded

// 20
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    string a[10], b[10];
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    string s, g;
    cin >> s >> g;
    typedef pair<int, string> pis;
    queue<pis> que;
    que.push(make_pair(0, s));
    map<string, bool> mem;
    while (!que.empty()) {
      pis c = que.front();
      if (c.second == g)
        break;
      que.pop();
      if (mem[c.second]++ || c.second.size() >= g.size())
        continue;
      for (int i = 0; i < n; i++) {
        string ns = c.second;
        vector<int> rp;
        for (int j = 0; j < ns.size();) {
          if (ns.substr(j, a[i].size()) == a[i]) {
            rp.push_back(j);
            j += a[i].size();
          } else {
            j++;
          }
        }
        for (int j = 0; j < rp.size(); j++) {
          ns = ns.substr(0, rp[j] + j * (b[i].size() - a[i].size())) + b[i] +
               ns.substr(rp[j] + j * (b[i].size() - a[i].size()) + a[i].size());
        }
        que.push(make_pair(c.first + 1, ns));
      }
    }
    cout << (que.empty() ? -1 : que.front().first) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 20
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    string a[10], b[10];
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    string s, g;
    cin >> s >> g;
    typedef pair<int, string> pis;
    queue<pis> que;
    que.push(make_pair(0, s));
    while (!que.empty()) {
      pis c = que.front();
      if (c.second == g)
        break;
      que.pop();
      if (c.second.size() >= g.size())
        continue;
      for (int i = 0; i < n; i++) {
        string ns = c.second;
        vector<int> rp;
        for (int j = 0; j < ns.size();) {
          if (ns.substr(j, a[i].size()) == a[i]) {
            rp.push_back(j);
            j += a[i].size();
          } else {
            j++;
          }
        }
        for (int j = 0; j < rp.size(); j++) {
          ns = ns.substr(0, rp[j] + j * (b[i].size() - a[i].size())) + b[i] +
               ns.substr(rp[j] + j * (b[i].size() - a[i].size()) + a[i].size());
        }
        que.push(make_pair(c.first + 1, ns));
      }
    }
    cout << (que.empty() ? -1 : que.front().first) << endl;
  }
  return 0;
}