Background
As you should know, a formal systemconsists of a set of axioms and set of inference or productionrules. Theorems, in general, are results that can be obtained upon the axioms andthe inference rules of the system. Deciding if a statement is a theorem of a given formalsystem is not a trivial matter; in fact, for some systems it is not even possible, as weknow by Gödel's incompleteness theorem.
The Problem
ME is a formal system with an infinite number ofaxioms and just one production rule. There are only three symbols that may appear in anyaxiom of ME: the capital letters M and E, and the symbol ?. In spite of the infinitenumber 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 anaxiom, 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 ofvalid 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 atheorem, 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 adecision algorithm for the system ME. But your goal is less general: just write a programthat 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 theword "theorem" if the test linecontains a theorem, or the word "no-theorem" inother 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
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main
{
private static final boolean DEBUG = false;
private BufferedReader cin;
private PrintWriter cout;
private String s;
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
public String next()
{
try {
return cin.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input()
{
s = next();
return true;
}
public void solve()
{
int m = s.indexOf('M');
int e = s.indexOf('E');
if (m == -1 || e == -1 || m > e) {
cout.println("no-theorem");
cout.flush();
return;
}
String s1 = s.substring(0, m);
String s2 = s.substring(m + 1, e);
String s3 = s.substring(e + 1);
//System.out.println("s1:" + s1 + " s2:" + s2 + " s3:" + s3);
boolean ok = true;
for (int i = 0, len = s1.length(); i < len; i++) {
char ch = s1.charAt(i);
if (ch != '?') {
ok = false;
break;
}
}
if (ok) {
for (int i = 0, len = s2.length(); i < len; i++) {
char ch = s2.charAt(i);
if (ch != '?') {
ok = false;
break;
}
}
}
if (ok) {
for (int i = 0, len = s3.length(); i < len; i++) {
char ch = s3.charAt(i);
if (ch != '?') {
ok = false;
break;
}
}
}
if (ok) {
if (s1.length() == 0 || s2.length() == 0
|| s3.length() == 0 || s1.length() + s2.length() != s3.length()) ok = false;
}
if (ok) cout.println("theorem");
else cout.println("no-theorem");
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
int t = Integer.parseInt(solver.next());
while (t-- > 0) {
solver.input();
solver.solve();
}
}
}