Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define long ll
- #define all(x) begin(x), end(x)
- namespace sat {
- const int N = 512;
- vector<int> g[N];
- vector<int> r[N];
- bool flag[N];
- bool value[N];
- int no(int x) {
- return x ^ 256;
- }
- int id(int x, int v) {
- return v ? x : no(x);
- }
- void add(int x, int y) {
- //cout << "ADD\t" << x << " -> " << y << endl;
- //cout << "ADD\t" << no(y) << " -> " << no(x) << endl;
- g[x].push_back(y);
- g[no(y)].push_back(no(x));
- r[y].push_back(x);
- r[no(x)].push_back(no(y));
- }
- vector<int> order;
- bool used_topsort[N];
- int comp[N];
- int cur_comp = 1;
- void dfs_topsort(int v) {
- used_topsort[v] = true;
- for (int u : g[v]) {
- if (!used_topsort[u]) {
- dfs_topsort(u);
- }
- }
- order.push_back(v);
- }
- void dfs_csc(int v) {
- comp[v] = cur_comp;
- for (int u : r[v]) {
- if (!comp[u]) {
- dfs_csc(u);
- }
- }
- }
- void set(int x) {
- flag[x] = true;
- }
- void reset(int x) {
- flag[x] = false;
- }
- void dfs_impl(int v) {
- value[v] = true;
- for (int u : g[v]) {
- if (!value[u]) {
- dfs_impl(u);
- }
- }
- }
- bool check() {
- order.clear();
- fill_n(used_topsort, N, false);
- fill_n(comp, N, 0);
- fill_n(value, N, false);
- for (int i = 0; i < N; ++i) {
- if (!used_topsort[i]) {
- dfs_topsort(i);
- }
- if (flag[i] && !value[i]) {
- dfs_impl(i);
- }
- }
- reverse(all(order));
- for (int i : order) {
- dfs_csc(i);
- ++cur_comp;
- }
- for (int i = 0; i < N; ++i) {
- if (comp[i] == comp[no(i)]) {
- return false;
- }
- if (value[i] && value[no(i)]) {
- return false;
- }
- }
- return true;
- }
- }
- const int N = 300;
- bool alph[N];
- int l, n, m;
- string str;
- void read_input() {
- cin >> str;
- l = (int)str.size();
- for (int i = 0; i < l; ++i) {
- alph['a' + i] = (str[i] == 'C');
- }
- cin >> n >> m;
- int a, b;
- char c, h;
- for (int i = 0; i < m; ++i) {
- cin >> a >> c >> b >> h;
- sat::add(sat::id(a - 1, c == 'C'), sat::id(b - 1, h == 'C'));
- }
- cin >> str;
- }
- int main() {
- #ifdef LC
- assert(freopen("input.txt", "r", stdin));
- #endif
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- read_input();
- if (!sat::check()) {
- cout << "-1\n";
- return 0;
- }
- for (int i = 0; i < n; ++i) {
- sat::set(sat::id(i, alph[(int)str[i]]));
- }
- if (sat::check()) {
- cout << str << '\n';
- return 0;
- }
- for (int i = 0; i < n; ++i) {
- sat::reset(sat::id(i, alph[(int)str[i]]));
- }
- int len = n - 1;
- for (; len > 0; --len) {
- for (int i = 0; i < len; ++i) {
- sat::set(sat::id(i, alph[(int)str[i]]));
- }
- bool can = false;
- for (int c = str[len] + 1; c < 'a' + l; ++c) {
- if (alph[c] == 0) {
- sat::set(sat::id(len, 0));
- if (sat::check()) {
- can = true;
- }
- sat::reset(sat::id(len, 0));
- break;
- }
- }
- for (int c = str[len] + 1; c < 'a' + l; ++c) {
- if (alph[c] == 1) {
- sat::set(sat::id(len, 1));
- if (sat::check()) {
- can = true;
- }
- sat::reset(sat::id(len, 1));
- break;
- }
- }
- for (int i = 0; i < len; ++i) {
- sat::reset(sat::id(i, alph[(int)str[i]]));
- }
- if (can) {
- break;
- }
- }
- //cerr << len << endl;
- assert(len < n);
- for (int i = 0; i < len; ++i) {
- sat::set(sat::id(i, alph[(int)str[i]]));
- }
- string answer(str.size(), '-');
- ++str[len];
- for (int i = len + 1; i < n; ++i) {
- str[i] = 'a';
- }
- for (int i = 0; i < len; ++i) {
- answer[i] = str[i];
- }
- for (int i = len; i < n; ++i) {
- string vec;
- for (int c = str[i]; c < 'a' + l; ++c) {
- if (alph[c] == 0) {
- vec.push_back((char)c);
- break;
- }
- }
- for (int c = str[i]; c < 'a' + l; ++c) {
- if (alph[c] == 1) {
- vec.push_back((char)c);
- break;
- }
- }
- sort(all(vec));
- //cerr << i + 1 << '\t' << vec << endl;
- for (char c : vec) {
- sat::set(sat::id(i, alph[(int)c]));
- if (sat::check()) {
- answer[i] = c;
- break;
- } else {
- sat::reset(sat::id(i, alph[(int)c]));
- }
- }
- if (answer[i] == '-') {
- cout << "-1\n";
- return 0;
- }
- }
- cout << answer << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement