1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| #include <bits/stdc++.h>
const int kMaxN = 405;
int l, n, m; int mi[26][2], op[26], vis[kMaxN]; std::vector<int> G[kMaxN]; std::string s, ans;
bool dfs(int u) { if (vis[(u <= n) ? (u + n) : (u - n)]) return 0; else if (vis[u]) return 1; vis[u] = 1; for (auto v : G[u]) if (!vis[v] && !dfs(v)) return vis[u] = 0; return 1; }
bool check(int k) { std::fill_n(vis + 1, 2 * n, 0); ans = s; for (int i = 1; i <= k; ++i) if (!dfs(i + op[s[i] - 'a'] * n)) return 0; for (int i = k + 1; i <= n; ++i) { int x = mi[0][0], y = mi[0][1]; if (!~x) { if (!dfs(i + op[y] * n)) return 0; ans[i] = char('a' + y); } else if (!~y) { if (!dfs(i + op[x] * n)) return 0; ans[i] = char('a' + x); } else if (x < y) { if (!dfs(i + op[x] * n)) { if (!dfs(i + op[y] * n)) return 0; else ans[i] = char('a' + y); } else { ans[i] = char('a' + x); } } else { if (!dfs(i + op[y] * n)) { if (!dfs(i + op[x] * n)) return 0; else ans[i] = char('a' + x); } else { ans[i] = char('a' + y); } } } return 1; }
void print(std::string s) { for (int i = 1; i <= n; ++i) std::cout << s[i]; std::cout << '\n'; }
void dickdreamer() { std::string str; std::cin >> str >> n >> m; l = str.size(); for (int i = 0; i < l; ++i) op[i] = (str[i] == 'C'); memset(mi, -1, sizeof(mi)); for (int i = 0; i < l; ++i) { for (int j = l - 1; j >= i; --j) mi[i][op[j]] = j; } for (int i = 1; i <= m; ++i) { int x, y; std::string sx, sy; std::cin >> x >> sx >> y >> sy; int ox = (sx[0] == 'C'), oy = (sy[0] == 'C'); G[x + ox * n].emplace_back(y + oy * n); G[y + (oy ^ 1) * n].emplace_back(x + (ox ^ 1) * n); } std::cin >> s; s = " " + s; if (check(n)) return print(s); for (int i = n; i; --i) { int now = s[i] - 'a', x = mi[now + 1][0], y = mi[now + 1][1]; if (!~x && !~y) { continue; } else if (!~x) { s[i] = char(y + 'a'); if (!check(i)) continue; else return print(ans); } else if (!~y) { s[i] = char(x + 'a'); if (!check(i)) continue; else return print(ans); } else { if (x < y) { s[i] = char(x + 'a'); if (!check(i)) { s[i] = char(y + 'a'); if (!check(i)) continue; else return print(ans); } else { return print(ans); } } else { s[i] = char(y + 'a'); if (!check(i)) { s[i] = char(x + 'a'); if (!check(i)) continue; else return print(ans); } else { return print(ans); } } } s[i] = char(now + 'a'); } std::cout << "-1\n"; }
int32_t main() { #ifdef ORZXKR freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0); int T = 1; while (T--) dickdreamer(); return 0; }
|