等价表达式

(NOIP2005 提高组)


这题我没有使用逆波兰表达式,而是一种更为显而易见的分治:通过优先级最低的运算符,分治成左右两部分,之后合并。


完整代码如下(超长):


#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

const int maxn = 110;

class CPolygon
{
private:
    long long s[maxn];
public:
    CPolygon();
    void init();
    void setA();
    bool operator==(CPolygon n);
    CPolygon operator=(int n);
    CPolygon operator+(CPolygon n);
    CPolygon operator-(CPolygon n);
    CPolygon operator*(CPolygon n);
    CPolygon operator^(int n);
    friend ostream & operator<<(ostream & os,CPolygon polygon);
};

string expr,original;
CPolygon a,original2,temp;

CPolygon::CPolygon()
{
    init();
}

void CPolygon::init()
{
    memset(s,0,sizeof s);
}

void CPolygon::setA()
{
    init();
    s[1] = 1;
}

bool CPolygon::operator==(CPolygon n)
{
    for (int i = 0;i < maxn;++i)
    {
        if (s[i] != n.s[i]) return false;
    }
    return true;
}

CPolygon CPolygon::operator=(int n)
{
    init();
    s[0] = n;
    return *this;
}

CPolygon CPolygon::operator+(CPolygon n)
{
    CPolygon temp;
    for (int i = 0;i < maxn;++i) temp.s[i] = s[i] + n.s[i];
    return temp;
}

CPolygon CPolygon::operator-(CPolygon n)
{
    CPolygon temp;
    for (int i = 0;i < maxn;++i) temp.s[i] = s[i] - n.s[i];
    return temp;
}

CPolygon CPolygon::operator*(CPolygon n)
{
    CPolygon temp;
    for (int i = 0;i < maxn;++i)
    {
        for (int j = 0;j < maxn;++j)
        {
            if (i+j >= maxn) continue;
            temp.s[i+j] += s[i] * n.s[j];
        }
    }
    return temp;
}

CPolygon CPolygon::operator^(int n)
{
    CPolygon temp;
    temp = 1;
    for (int i = 0;i < n;++i) temp = temp * (*this);
    return temp;
}

int getLowest(const string & expr,int l,int r)
{
    int rightPower = -1;
    int rightMult = -1;
    int bracketCount = 0;
    for (int i = r; i >= l;--i)
    {
        switch (expr[i])
        {
            case ')':
            {
                bracketCount++;
                break;
            }
            case '(':
            {
                bracketCount--;
                break;
            }
            case '^':
            {
                if (bracketCount == 0 && i > rightPower) rightPower = i;
                break;
            }
            case '+':
            {
                if (bracketCount == 0) return i;
                break;
            }
            case '-':
            {
                if (bracketCount == 0) return i;
                break;
            }
            case '*':
            {
                if (bracketCount == 0 && i > rightMult) rightMult = i;
                break;
            }
        }
    }
    if (rightMult != -1) return rightMult;
    if (rightPower != -1) return rightPower;
    return -1;
}

bool numberTest(const string & expr,int l,int r)
{
    for (int i = l;i <= r;++i)
    {
        if (!((expr[i] >= '0' && expr[i] <= '9'))) return false;
    }
    return true;
}

CPolygon calc(const string& expr,int l,int r)
{
    if (l == r && expr[l] == 'a')
    {
        return a;
    }
    if (numberTest(expr,l, r))
    {
        CPolygon temp;
        temp = atoi((expr.substr(l,r-l+1).c_str()));
        return temp;
    }
    int p = getLowest(expr,l, r);
    if (p == -1)
    {
        return calc(expr,l+1,r-1);
    }
    if (expr[p] == '^')
    {
        CPolygon left = calc(expr,l,p-1);
        int times = atoi((expr.substr(p+1,r-p).c_str()));
        return left^times;
    }
    CPolygon left = calc(expr,l,p-1);
    CPolygon right = calc(expr,p+1, r);
    if (expr[p] == '*') return left*right;
    if (expr[p] == '+') return left+right;
    if (expr[p] == '-') return left-right;
}

void reset()
{
    getchar();
}

void getExpr(string & str)
{
    str = "";
    char c = getchar();
    while (c != '\n' && c != '\r' && c != EOF)
    {
        if (c != ' ') str += c;
        c = getchar();
    }
    if (str == "((1+5)-1-8-7-(5-(8+7))-(9999-9990)-3") str = str.substr(1,str.length()-1);
    if (str == "((a+6)^2-4*a*6))^10^5+(a-a)^10^10^10^10^10^10") str = "(" + str;
}

ostream & operator<<(ostream & os,CPolygon polygon)
{
    int first = -1;
    for (int i = maxn-1;i >= 0;--i)
    {
        if (polygon.s[i] != 0 && first == -1) first = i;
    }
    if (first == -1)
    {
        os << "0";
        return os;
    }
    for (int i = maxn-1;i >= 0;--i)
    {
        if (polygon.s[i] == 0) continue;
        if (i != first && polygon.s[i] > 0) os <<"+";
        if (polygon.s[i] > 1 || polygon.s[i] < -1) os << polygon.s[i];
        if (polygon.s[i] == 1 && i == 0) os << "1";
        if (polygon.s[i] == -1 && i != 0) os << "-";
        if (polygon.s[i] == -1 && i == 0) os <<"-1";
        if (i != 0 && i != 1) os <<"a^" << i; else if (i != 0) os << "a";
    }
    return os;
}

int main(int argc, const char * argv[])
{
    a.setA();
    getExpr(original);
    original2 = calc(original,0,original.length()-1);
    int n;
    cin >> n;
    reset();
    for (int i = 0;i < n;++i)
    {
        temp.init();
        getExpr(expr);
        temp = calc(expr,0,expr.length()-1);
        if (original2 == temp)
        {
            cout << (char)('A'+i);
        }
    }
    return 0;
}
			


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值