10
14
2015
0

[Codeforces Round #315] New Language


 

E. New Language
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Living in Byteland was good enough to begin with, but the good king decided to please his subjects and to introduce a national language. He gathered the best of wise men, and sent an expedition to faraway countries, so that they would find out all about how a language should be designed.

After some time, the wise men returned from the trip 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 languages, but almost all of them have letters that are divided into vowels and consonants; in a word, vowels and consonants must be combined correctly."

There are very many rules, all of them have exceptions, but our language will 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 words that satisfy them.

The rules of composing words are:

  • The letters are divided into vowels and consonants in some certain way;
  • 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 has a letter of type t2.

You are given some string s of length n, it is not necessarily a correct word of the new language. Among all the words of the language that lexicographically not smaller than the string s, find the minimal one in lexicographic order.

Input

The first line 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 first l letters of the English alphabet are used as the letters of the alphabet of the new language. If the i-th character of the string equals to 'V', then the corresponding letter is a vowel, otherwise it is a consonant.

The second line contains two integers nm (1 ≤ n ≤ 2000 ≤ m ≤ 4n(n - 1)) — the number of letters in a single word and the number of rules, correspondingly.

Next m lines describe m rules of the language in the following format: pos1, t1, pos2, t2 (1 ≤ pos1, pos2 ≤ npos1 ≠ pos2 'V', 'C}).

The last line contains string s of length n, consisting of the first l small letters of the English alphabet.

It is guaranteed that no two rules are the same.

Output

Print a smallest word of a language that is lexicographically not smaller than s. If such words does not exist (for example, if the language has no words at 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 first test word "aa" is not a word of the language, but word "ab" is.

In the second test out of all four possibilities only word "bb" is not a word of a language, but all other words are lexicographically less, so there is no answer.

In the third test, due to the last rule, "abac" doesn't belong to the language ("a" is a vowel, "c" is a consonant). The only word with prefix "ab" that meets the given rules is "abaa". But it is less than "abac", so the answer will be "acaa"

题目大意:有一种新的语言,给出它的各个字母是元音还是辅音,有若干个限制条件,每个条件表示如果一个字符串的某个位置是元音(或辅音)那么某个位置必须是元音(或辅音),满足所有限制条件的是这种语言的单词。给出一个字符串(不一定是单词),求和它长度一样且字典序不小于它的字典序最小的单词。


枚举前i位于给出字符串相同,强制第i+1位大于给出字符串,后面的则按字典序最小选择,就是一个2-SAT判定问题了,DFS暴力搞就行了(似乎Floyd也行,Cf机器太强了。。)。

代码在此。

Category: 题解 | Tags: Codeforces 2-SAT | Read Count: 358

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com