[表达式求值 矩阵乘法] ZROI 2017提高10A. Calc

神思路,第一次见到这种操作…
构造矩阵来进行状态的转移。
A+BC ,我们记3元组 (S,M,T) 表示当前状态,其中 S=A, M=B, T=BC
这样记录,每多一个字符,转移可以表示成乘上一个矩阵。
数字 k : (S,M,T)(S,M,T10+Mk)
+ : (S,M,T)(S+T,1,0)
: (S,M,T)(S,T,0)
矩阵快速幂优化。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100005,MOD=998244353;
struct Matrix{
    int n,m,a[5][5];
    Matrix(int x=0){ memset(a,0,sizeof(a)); a[0][0]=a[1][1]=a[2][2]=a[3][3]=x; }
    Matrix operator * (const Matrix &b)const{
        Matrix c; c.n=n; c.m=b.m;
        for(int i=0;i<=c.n-1;i++)
         for(int j=0;j<=c.m-1;j++)
          for(int k=0;k<=m-1;k++)
           (c.a[i][j]+=(LL)a[i][k]*b.a[k][j]%MOD)%=MOD;
        return c;
    }
} ans; 
int n; 
Matrix getM(char ch){
    Matrix c; c.n=c.m=4;
    if(ch=='+') c.a[0][0]=c.a[2][0]=c.a[3][1]=c.a[3][3]=1; else
    if(ch=='*') c.a[0][0]=c.a[2][1]=c.a[3][3]=1; else
    c.a[0][0]=c.a[1][1]=c.a[3][3]=1, c.a[2][2]=10, c.a[1][2]=ch-'0';
    return c; 
}
char st[20];
Matrix Pow(Matrix a,int b){
    Matrix res=1; res.n=res.m=4;
    for(;b;b>>=1,a=a*a) if(b&1) res=res*a;
    return res;
}
int main(){
    freopen("zroi10A.in","r",stdin);
    freopen("zroi10A.out","w",stdout);
    scanf("%d",&n);
    ans=0; ans.n=1; ans.m=4; ans.a[0][1]=ans.a[0][3]=1;
    for(int i=1;i<=n;i++){
        int x; scanf("%d%s",&x,st+1); int len=strlen(st+1);
        Matrix res=1; res.n=res.m=4; 
        for(int i=1;i<=len;i++) res=res*getM(st[i]);
        ans=ans*Pow(res,x);
    }
    ans=ans*getM('+');
    printf("%d\n",ans.a[0][0]);
    return 0;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用栈实现复杂表达式求值的C++代码: ```c++ #include <iostream> #include <stack> #include <string> #include <cstring> #include <cmath> using namespace std; // 优先级比较函数 int cmp(char op1, char op2) { if (op1 == '(' || op2 == '(') { return -1; } if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) { return -1; } else { return 1; } } // 计算函数 double calc(double num1, double num2, char op) { switch (op) { case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': return num1 / num2; case '^': return pow(num1, num2); default: return 0; } } // 将中缀表达式转成后缀表达式 string infixToPostfix(string infix) { stack<char> s; string postfix; for (int i = 0; i < infix.length(); i++) { char c = infix[i]; if (isdigit(c) || c == '.') { // 数字和小数点直接输出 postfix += c; continue; } postfix += ' '; // 操作符之间加空格 while (!s.empty() && cmp(s.top(), c) == 1) { // 栈顶元素优先级大于等于当前操作符 postfix += s.top(); s.pop(); postfix += ' '; } if (c == ')') { // 如果是右括号,弹出直到左括号 while (!s.empty() && s.top() != '(') { postfix += s.top(); s.pop(); postfix += ' '; } s.pop(); } else { // 其他情况直接入栈 s.push(c); } } while (!s.empty()) { postfix += ' '; postfix += s.top(); s.pop(); } return postfix; } // 计算后缀表达式 double calcPostfix(string postfix) { stack<double> s; char buf[100]; for (int i = 0; i < postfix.length(); i++) { char c = postfix[i]; if (isdigit(c) || c == '.') { // 数字直接入栈 int j = i, k = 0; while (isdigit(postfix[j]) || postfix[j] == '.') { buf[k++] = postfix[j++]; } buf[k] = '\0'; double num = atof(buf); s.push(num); i = j - 1; } else if (c != ' ') { // 操作符弹出栈顶元素计算 double num2 = s.top(); s.pop(); double num1 = s.top(); s.pop(); s.push(calc(num1, num2, c)); } } return s.top(); } int main() { string infix; cout << "请输入中缀表达式:"; getline(cin, infix); string postfix = infixToPostfix(infix); cout << "后缀表达式为:" << postfix << endl; double result = calcPostfix(postfix); cout << "计算结果为:" << result << endl; return 0; } ``` 这个代码通过将中缀表达式转换为后缀表达式,然后使用栈计算后缀表达式的值来实现复杂表达式求值。其中,`cmp`函数用于比较两个操作符的优先级,`calc`函数用于计算两个数和一个操作符的结果,`infixToPostfix`函数用于将中缀表达式转换为后缀表达式,`calcPostfix`函数用于计算后缀表达式的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值