Codeforces Round #315 (Div. 1) C. New Language Dictionary order 2-sat solving __ Graph theory

Source: Internet
Author: User
Tags time limit
C. New Language time limit per test 2 seconds memory limit/test 256 megabytes input standard input output standard out Put

Living in Byteland is good enough to-begin with, but the good king decided to-please he subjects and to introduce a Nati Onal language. He gathered the best of wise men, and sent a expedition to faraway countries, so this they would Language should be designed.

After some time, the wise men returned from the even wiser. They locked up for six months in the dining room, after which They said to the king: "There are a lot of different languag ES, but almost all of them have letters to are to divided and vowels; In a word, vowels and consonants must is combined correctly. "

There are very many rules, all of them have exceptions, but we language'll be deprived of such defects! We propose to introduce a set of formal rules of combining vowels and consonants, and include in the language all the word s that satisfy them.

The rules of composing words are:the letters are divided into vowels and consonants in some certain; All words have a length of exactly n; There are m rules of the form (POS1, T1, Pos2, T2). Each rule is:if the "position pos1 has a letter of type T1, then the position pos2 a letter of type has.

You are given some string s of length n, it isn't necessarily a correct word of the new language. Among all the words of the "language" lexicographically not smaller than the string s, find the minimal one in lexicogr Aphic order. Input

The contains a single line consisting of letters ' V ' (vowel) and ' C ' (consonant), determining, which letters are Vowels and which letters are consonants. The length of this string L is the size of the alphabet of the new language (1≤L≤26). The I letters of the Chinese alphabet are used as the letters of the new alphabet. If the i-th character of the string equals to ' V ', then the corresponding-a-vowel, otherwise it is a consonant.

The second line contains two integers n, m (1≤n≤200, 0≤m≤4n (n-1))-the number of letters in a single word and t He number of rules, correspondingly.

Next m lines describe m rules of the language in the following format:pos1, T1, Pos2, T2 (1≤POS1, Pos2≤n, Pos1≠pos2 , ' V ', ' C '}).

The last line contains string s of length n, consisting of the the ' the ' the ' the ' of the ' small letters of the Chinese alphabet.

It is guaranteed that no two rules are the same. Output

Print a smallest word of a language this is lexicographically not smaller than S. If such words does not exist (for example, if the language has no words to all), print "-1" (without the quotes). Sample Test (s) input

VC
2 1
1 V 2 C
AA
Output
Ab
Input
VC
2 1
1 C 2 V
bb
Output
-1
Input
VCC
4 3
1 C 2 v
2 C 3 v
3 v 4 v
abac
Output
Acaa
Note

In the ' a ', word ' AA ' is ' language ', but word ' ab ' is.

In the second test out of all four possibilities only word "BB" isn't a word of a language, but all other words are Ographically less, so there is no answer.

In the third Test, due to the last rule, "Abac" doesn ' t belong to the language ("a" was a vowel, "c" is a consonant). The only word with prefix "AB" which meets the given rules is "Abaa". But It is less than "ABAC" and so the answer would be "ACAA"

For the first word, each letter is either a vowel or a consonant, giving a series of rules, POS1, T1, pos2, t2 table pos1 where T1 is pos2 (T2 t1 is a vowel or consonant). A target string that satisfies the above rule and has a minimum dictionary order and is greater than or equal to the given string.

It can be guaranteed that the dictionary order is minimal and that the dictionary order is greater than or equal to the given string, from large to small, the first character of the enumeration is the same, the i+1 character is greater than the given character, which translates into a 2-sat-judged problem. Each point is divided into two dots, expressed as vowels (i * 2), or as a supplement (I * 2 + 1), each rule, can correspond to two sides, such as P1 c P2 V, connected to two edge, respectively, from the P1 for C can be launched P2 for V, from P2 for C launch P1 for V. This and ordinary 2-sat problem and there are differences, ordinary 2-sat problem only to determine whether there is solution, and this problem, because it is required dictionary order the smallest string, so must first select serial number of small string first search before you can, with this search strategy can guarantee that the solution is the smallest. The 2-sat problem can be solved by using the Tarjan algorithm to find the strong connected component. Here, directly by the 2-sat problem, the graph of high symmetry, you can use deep search to find the solution. The 2-sat method is used to determine that the total complexity of O (M) is O (n * m) in each degree of complexity.

#define N 2100 #define M 100005 #define MAXN 205 #define MOD 1000000000000000007 int n,m,p1,p2,n2,xi,st[n],sn,sp[30],len;
Char Str[n],s1[10],s2[10],strc,strv,ans[n];
Vector<int> P[n];
BOOL Vis[n];
    void Getscsv (char & Sc,char & Sv,int tc) {sc = SV = ' n ';
        for (; TC < len;tc++) {if (SP[TC] && sc = ' = ') sc = ' a ' + TC;
    if (!SP[TC] && sv = = ' n ') sv = ' a ' + TC;
    BOOL DFS (int x) {if (vis[x^1]) return false;
    if (Vis[x]) return true;
    int tx = X/2;
    if (TX < XI) {if (x & 1)!= Sp[str[tx]-' a '] return false;
        else if (tx = = XI) {int TC = STR[TX]-' a ' + 1;
        Char sc[2];
        Getscsv (SC[1],SC[0],TC);
        if (sc[x & 1]!= ' ") {Ans[tx] = sc[x & 1];
    else return false;
        else {if (x & 1) && strc = = ' is ') return false; if (!) (
    X & 1) && STRV = = ' I ') return false;
    } Vis[x] = true; st[sn++] = x; FI (P[x].size ()) {if (!
    DFS (P[x][i])) return false;
return true;
    BOOL Twodfs (int ti,int xi1,int Xi2,char Sv,char SC) {Ans[ti] = SV;
    sn = 0; if (!
        DFS (XI1)) {FJ (SN) vis[st[j]] = false;
        sn = 0;
        Ans[ti] = SC; if (!
    DFS (XI2)) return false;
return true;
    BOOL Chosedfs (char Sv,char sc,int ti) {if (SV = ' n ' && sc = ' = ') return false;
    sn = 0;
       if (SV = = ' ") {ans[ti] = SC; if (!
    DFS (2 * ti + 1)) return false;
        else if (sc = = ' ") {ans[ti] = SV; if (!
    DFS (2 * ti)) return false;
        else {int xi1,xi2;
        if (SV < SC) {if (!twodfs (Ti,ti * 2, TI * 2 + 1,SV,SC)) return false;
        else {if (!twodfs (TI,TI * 2 + 1, TI * 2,sc,sv)) return false;
} return true;
    BOOL Sat_2 () {fill (vis,false); for (int i = 0;i<n2;i++) {if (!vis[i] &&!vis[i^1]) {int ti = I/ 2; if (Ti < XI) {if (!
            DFS (2 * ti + sp[str[ti]-' a ']) return false;
                else if (Ti = = XI) {int TC = Str[ti]-' a ' + 1;
                Char sv = ' n ', sc = ';
                Getscsv (SC,SV,TC); if (!
            Chosedfs (Sv,sc,ti)) return false; } else {if (!
            Chosedfs (Strv,strc,ti)) return false;
}} return true;
    } void Add_edge (int a,int b) {p[a].push_back (b);
P[b^1].push_back (a^1);
        int main () {while (SS (str)!=eof) {len = strlen (str);
        for (int i = 0;i < len;i++) {Sp[i] = str[i] = = ' C '? 1:0;
        } getscsv (strc,strv,0);
        S2 (N,M);
        N2 = n + N;
        FI (n2) p[i].clear (); FI (m) {S (P1);
            SS (S1); S (p2);
            SS (S2);
            p1--;p 2--;
            P1 *= 2;p2 *= 2; P1 = s1[0] = = ' C '?
            P1^1:P1; P2 = s2[0] = = ' C '?
            P2^1:P2;
        Add_edge (P1,P2);
        } SS (str);
        BOOL flag = TRUE;
            for (int i = n;i>=0 && flag;i--) {XI = i;
                if (Sat_2 ()) {flag = false;
                for (int j = 0;j < i + i;j++) {ANS[J/2] = STR[J/2]; for (int j = i + i + 2;j<n2;j++) {if (Vis[j]) {ANS[J/2] = ( j&1)?
                    STRC:STRV;
                } Ans[n] = ' I ';
            printf ("%s\n", ans);
    } if (flag) printf (" -1\n");
return 0;
 }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.