UVA11203 Can you decide it for ME?【水题】

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 Gdel’s incompleteness theorem.
    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

问题链接UVA11203 Can you decide it for ME?
问题简述:(略)
问题分析
    题目太长了!读懂应该就没有问题了。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11203 Can you decide it for ME? */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    string s;

    cin >> n;
    while(n--) {
        cin >> s;
        int m = s.find('M');
        int e = s.find('E');
        if(m == (int)string::npos || e == (int)string::npos || m > e) {
            cout << "no-theorem" << endl;
            continue;
        }

        string s1 = s.substr(0, m);
        string s2 = s.substr(m + 1, e - m - 1);
        string s3 = s.substr(e + 1);

        bool flag = true;
        for(int i = 0, len = s1.length(); i < len; i++)
            if(s1[i] != '?') {
                flag = false;
                break;
            }

        if(flag) {
            for(int i = 0, len = s2.length(); i < len; i++)
                if(s2[i] != '?') {
                    flag = false;
                    break;
                }
        }

        if(flag) {
            for(int i = 0, len = s3.length(); i < len; i++)
                if(s3[i] != '?') {
                    flag = false;
                    break;
                }
        }

        if(flag) {
            if(s1.length() == 0 || s2.length() == 0 || s3.length() == 0 || s1.length() + s2.length() != s3.length())
                flag = false;
        }

        if(flag)
            cout << "theorem" << endl;
        else
            cout << "no-theorem" << endl;
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/10367932.html