2013-07-07 21:48:16Morris

[UVA] 11203 - Can you decide it for ME

Background

As you should know, a formal system consists of a set of axioms and set of inference or production rules. Theorems, in general, are results that can be obtained upon the axioms and the inference rules of the system. Deciding if a statement is a theorem of a given formal system is not a trivial matter; in fact, for some systems it is not even possible, as we know by Gödel's incompleteness theorem.

The Problem

ME is a formal system with an infinite number of axioms and just one production rule. There are only three symbols that may appear in any axiom of ME: the capital letters M and E, and the symbol ?. In spite of the infinite number of axioms, there is an easy way of identifying them:

  • Axiom definition: An axiom of ME is a string of the form xM?Ex? where x is a string of one or more ? symbols.

Notice that the word xM?Ex? itself is not an axiom, because x is not a valid symbol of ME; it is just a pattern to describe the axioms. But ??M?E??? is indeed an axiom, because it fits the pattern, just replacing x with ??

There is another type of valid strings in the system ME: the theorems. Every axiom is a theorem of ME. In addition, the following production rule gives rise to an infinite number of theorems:

  • Production rule: If xMyEz is a theorem then xMy?Ez? is also a theorem, where x,y,z are strings of one or more ? symbols.

For instance, the string ??M?E??? is a theorem, because it is an axiom, and the string ??M??E???? is also a theorem, since it can be obtained upon the theorem ??M?E??? and the production rule.

Can we decide if a given string is a theorem of ME? Sure!, there is a decision algorithm for the system ME. But your goal is less general: just write a program that reads strings with at most 50 symbols and decides if they are theorem of ME.

Input

The input begins with a single positive integer N, on a line by itself, indicating the number of input lines following. Each input line contains a string with 1 to 50 characters (without spaces), but not necessarily restricted to the symbols of ME.

Output

For each input line, the program must print an output line with the word "theorem" if the test line contains a theorem, or the word "no-theorem" in other case.

Sample Input

7
??M?E??? 
xM?Ex?
??M??E????
M?E?
??m?e???
?M?E??
12M12E????

Sample Output

theorem
no-theorem
theorem
no-theorem
no-theorem
theorem
no-theorem


我們先將 aMbEc,a, b, c 分別代表有多少個 '?'
基礎定義會得到 b = 1, a = c-1, a > 0
產生規則為 b++, c++

那麼直接計算 a, b, c 個數。
然後看能不能湊回基礎定義。


#include <stdio.h>
int check(char s[]) {
int a, b, c, i = 0;
a = 0, b = 0, c = 0;
while(s[i] == '?') a++, i++;
if(s[i] != 'M') return 0;
i++;
while(s[i] == '?') b++, i++;
if(s[i] != 'E') return 0;
i++;
while(s[i] == '?') c++, i++;
if(s[i] != '\0') return 0;
return a == (c-(b-1))-1 && a && b;
}
int main() {
int testcase;
char s[105];
scanf("%d", &testcase);
while(testcase--) {
scanf("%s", s);
puts(check(s) ? "theorem" : "no-theorem");
}
return 0;
}