#P9889. Reasoning
Reasoning
Reasoning
Problem Description
The reasoning system consists of the following symbols:
- Parentheses: and ;
- Logical connectives: and ;
- Universal quantifier: ;
- Variables: ;
- Constants: ;
- Functions: ;
- Predicates: . This reasoning system also includes concepts such as term, formula, free occurrence, and replacement. Based on these concepts, we can define whether a certain term can replace a certain variable without contradiction. This is one of the basis for reasoning, and you wants to solve this problem first. Term is defined as follows:
- Every variable is a term.
- Every constant is a term.
- If are terms and is a function, then is a term. Formula (well-formed formula) is defined as follows:
- If are terms and is a predicate, then is a formula. This kind of formula is also called the atomic formula.
- If and are formulas, then and are both formulas.
- If is a formula, and is a variable, then is also a formula. Free occurrence of a variable in formula is defined as follows:
- If is an atomic formula, then occurs free in if and only if occurs in (which means there is a char inside the string ).
- If is , then occurs free in if and only if occurs free in .
- If is , then occurs free in if and only if occurs free in or occurs free in .
- If is , then occurs free in if and only if occurs free in and . For every formula , every variable , and every term , the replacement is defined as:
- If is an atomic formula, then is the expression formed by simply replacing every char to string .
- If is , then .
- If is , then
- If is , then $(\forall y\psi)^x_t= \begin{cases} \forall y(\psi^x_t),&\text{if } x\ne y;\\\\ \forall y\psi,&\text{if } x=y. \end{cases}$ And finally, we can now define the rule of zero-contradiction replacement:
- For atomic formula , can always replace in without contradiction.
- If is , then can replace in without contradiction if and only if can replace without contradiction in .
- If is , then can replace in without contradiction if and only if can replace without contradiction in both and .
- If is , then can replace in without contradiction if and only if
- doesn't occur free in , or
- doesn't occur in , and can replace in without contradiction. Now, by programming, Jack wants to judge whether is replaceable by the term without contradiction.
Input
At the beginning of the input section, there is an integer , indicating the number of test cases. For every test case, the first line gives a formula , which has length . In the input formula, will be replaced by char , will be replaced by char , and will be replaced by char . The next line consists of an integer , denoting the number of function and predicate. Then there followed lines, each line consists of a char and an integer, representing the parameter number of the function or predicate. The next line consists of an integer , denoting queries. There follow lines, each of which consists of a term and a variable , denoting the query of whether the term can replace the variable in the formula A without replacement. It is guaranteed that the length of string is no more than .
Output
The output consists of sections. In each section, you need to print a character to answer whether the variable is replaceable without contradiction. If the answer is yes, you should print the formula after replacement.
Sample Input
1
AxAy(PzvfxygxyI(NQ))
5
f 3
g 2
P 3
Q 0
h 2
4
hxx x
hxy y
hzz z
hxz z
Sample Output
Y
AxAy(PzvfxygxyI(NQ))
Y
AxAy(PzvfxygxyI(NQ))
Y
AxAy(PhzzvfxygxyI(NQ))
N
Hint
The given formula is $\forall x\forall y(P(z, v, f(x, y, g(x, y)))\to(\neg Q))$. For the first and the second query, and don't occur free in the formula, because they are constrained by . Thus the answer is the original formula. For the third query, occurs free in the formula, because it isn't constrained by . Thus the answer is to replace by . For the last query, occurs free in the formula, and after the replacement, in will be constrained by , making the in no longer occur free. Thus the replacement is illegal.
Source
2023“钉耙编程”中国大学生算法设计超级联赛(9)