Problem p00286 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> connectAmida(const vector<int> &x, const vector<int> &y) {
  int n = x.size();
  vector<int> ans(n);
  for (int i = 0; i < n; ++i)
    ans[i] = y[x[i]];
  return ans;
}

vector<int> repeatAmida(int a, const vector<int> &x) {
  int n = x.size();
  vector<int> len(n, 0);
  for (int i = 0; i < n; ++i) {
    int j = i;
    do {
      j = x[j];
      ++len[j];
    } while (j != i);
  }

  vector<int> ans(n);
  for (int i = 0; i < n; ++i) {
    int j = i;
    for (int k = 0; k < a % len[i]; ++k)
      j = x[j];
    ans[i] = j;
  }
  return ans;
}

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> init(n);
  for (int i = 0; i < n; ++i)
    init[i] = i;

  vector<vector<int>> to(128, vector<int>(n, -1));
  for (int i = 0; i < k; ++i) {
    char p;
    int h;
    cin >> p >> h;

    vector<int> v = init;
    for (int y = 0; y < h - 1; ++y) {
      for (int x = 0; x < n - 1; ++x) {
        int g;
        cin >> g;
        if (g == 1)
          swap(v[x], v[x + 1]);
      }
    }
    for (int j = 0; j < n; ++j)
      to[p][v[j]] = j;
  }

  int q;
  cin >> q;
  while (--q >= 0) {
    string s;
    cin >> s;
    stack<pair<vector<int>, int>> stk;
    stk.push(make_pair(init, 1));

    unsigned k = 0;
    while (k < s.size()) {
      if (s[k] == '(') {
        stk.push(make_pair(init, 1));
        ++k;
      } else if (s[k] == ')') {
        vector<int> v = stk.top().first;
        stk.pop();
        v = repeatAmida(stk.top().second, v);
        v = connectAmida(stk.top().first, v);
        stk.top() = make_pair(v, 1);
        ++k;
      } else if (isdigit(s[k])) {
        int a = 0;
        while (isdigit(s[k])) {
          a *= 10;
          a += s[k] - '0';
          ++k;
        }
        stk.top().second = a;
      } else if (isalpha(s[k])) {
        vector<int> v = to[s[k]];
        v = repeatAmida(stk.top().second, v);
        v = connectAmida(stk.top().first, v);
        stk.top() = make_pair(v, 1);
        ++k;
      } else {
        ++k;
      }
    }

    vector<int> v = stk.top().first;
    vector<int> ans(n);
    for (int i = 0; i < n; ++i)
      ans[v[i]] = i + 1;

    cout << ans[0];
    for (int i = 1; i < n; ++i)
      cout << ' ' << ans[i];
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> connectAmida(const vector<int> &x, const vector<int> &y) {
  int n = x.size();
  vector<int> ans(n);
  for (int i = 0; i < n; ++i)
    ans[i] = y[x[i]];
  return ans;
}

vector<int> repeatAmida(int a, const vector<int> &x) {
  int n = x.size();
  vector<int> len(n, 0);
  for (int i = 0; i < n; ++i) {
    int j = i;
    do {
      j = x[j];
      ++len[j];
    } while (j != i);
  }

  vector<int> ans(n);
  for (int i = 0; i < n; ++i) {
    int j = i;
    for (int k = 0; k < a; ++k)
      j = x[j];
    ans[i] = j;
  }
  return ans;
}

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> init(n);
  for (int i = 0; i < n; ++i)
    init[i] = i;

  vector<vector<int>> to(128, vector<int>(n, -1));
  for (int i = 0; i < k; ++i) {
    char p;
    int h;
    cin >> p >> h;

    vector<int> v = init;
    for (int y = 0; y < h - 1; ++y) {
      for (int x = 0; x < n - 1; ++x) {
        int g;
        cin >> g;
        if (g == 1)
          swap(v[x], v[x + 1]);
      }
    }
    for (int j = 0; j < n; ++j)
      to[p][v[j]] = j;
  }

  int q;
  cin >> q;
  while (--q >= 0) {
    string s;
    cin >> s;
    stack<pair<vector<int>, int>> stk;
    stk.push(make_pair(init, 1));

    unsigned k = 0;
    while (k < s.size()) {
      if (s[k] == '(') {
        stk.push(make_pair(init, 1));
        ++k;
      } else if (s[k] == ')') {
        vector<int> v = stk.top().first;
        stk.pop();
        v = repeatAmida(stk.top().second, v);
        v = connectAmida(stk.top().first, v);
        stk.top() = make_pair(v, 1);
        ++k;
      } else if (isdigit(s[k])) {
        int a = 0;
        while (isdigit(s[k])) {
          a *= 10;
          a += s[k] - '0';
          ++k;
        }
        stk.top().second = a;
      } else if (isalpha(s[k])) {
        vector<int> v = to[s[k]];
        v = repeatAmida(stk.top().second, v);
        v = connectAmida(stk.top().first, v);
        stk.top() = make_pair(v, 1);
        ++k;
      } else {
        ++k;
      }
    }

    vector<int> ans = stk.top().first;
    for (int i = 0; i < n; ++i)
      ++ans[i];

    cout << ans[0];
    for (int i = 1; i < n; ++i)
      cout << ' ' << ans[i];
    cout << endl;
  }

  return 0;
}