UVa11203 - Can you decide it for ME?

探讨了形式系统ME的构造及其定理判定方法。该系统由无限数量的公理和单一的推导规则组成,使用特定符号串来定义公理,并通过推导规则生成定理。介绍了判定给定字符串是否为ME系统的定理的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值