zoukankan      html  css  js  c++  java
  • 表达式求值(noip2015等价表达式)

    题目大意

    给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些

    做法

    模拟一个多项式显然难以实现
    那么我们高兴的找一些素数代入表达式,再随便找一个素数做模
    表达式求值优先级表

    ( ) + - * ^
    ( < = < < < <
    )
    + < > > > < <
    - < > > > < <
    * < > > > > <
    ^ < > > > > >
    如果前一符号优先级大于有一符号我们就进行前一个符号的运算 ``` #include #include #include #include typedef long long LL; const int Q=10000007; const int A=33; LL p[A]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,91,97,131,2930,3033,32767,131071}; int chg[256]; char cmp[7][7]={//前一符号与后一符号比较 {}, {' ','<','=','<','<','<','<'}, {' ',' ',' ',' ',' ',' ',' '}, {' ','<','>','>','>','<','<'}, {' ','<','>','>','>','<','<'}, {' ','<','>','>','>','>','<'}, {' ','<','>','>','>','>','>'} }; char t[53],s[53]; int m; LL ans[A]; LL tmp[A]; int opr[53],stopr; LL num[A][53];int stnum;

    void print(int x)
    {
    for(int i=1;i<A;i++) if(tmp[i]!=ans[i]) return;
    printf("%c",'A'+x);
    }

    LL yusuan(LL a,LL b,int x)
    {
    if(x3) return (a+b)%Q;
    if(x
    4) return ((a-b)%Q+Q)%Q;
    if(x5) return (a*b)%Q;
    if(x
    6)
    {
    LL res=1;
    while(b--) res=(res*a)%Q;
    return res;
    }
    }

    void calc(LL to)
    {
    int n=0,len=strlen(t),i,j,x;LL d;
    for(i=0;i<len;i++) if(t[i]!=' ') s[++n]=t[i];
    stopr=0;stnum=0;
    s[0]='(';s[++n]=')';
    for(i=0;i<=n;i++)
    {
    if(chg[s[i]])
    {
    x=chg[s[i]];
    while(stopr&&cmp[opr[stopr]][x]'>')
    {
    stnum--;
    for(j=1;j<A;j++) num[j][stnum]=yusuan(num[j][stnum],num[j][stnum+1],opr[stopr]);
    stopr--;
    }
    if(stopr&&cmp[opr[stopr]][x]
    '=') stopr--;
    else opr[++stopr]=x;
    }
    else
    {
    if(s[i]=='a')
    {
    ++stnum;
    for(j=1;j<A;j++) num[j][stnum]=p[j];
    }
    else
    {
    d=0;
    for(;isdigit(s[i]);i++) d=(d
    10+s[i]-'0')%Q;
    i--;
    ++stnum;
    for(j=1;j<A;j++) num[j][stnum]=d;
    }
    }
    }
    for(i=1;i<A;i++) to[i]=num[i][1];
    }

    int main()
    {
    chg['(']=1;chg[')']=2;
    chg['+']=3;chg['-']=4;
    chg['*']=5;chg['^']=6;
    int i;
    gets(t);
    calc(ans);
    scanf("%d",&m);getchar();
    for(i=0;i<m;i++)
    {
    gets(t);
    calc(tmp);
    print(i);
    }
    return 0;
    }

  • 相关阅读:
    VUEX
    使用element框架 增加router路由
    VUE目录
    elementUI 创建
    VUE组件(父子组件)
    VUE操作DOM获取HTML、删除HTML、插入HTML
    VUE网络交互axios(网络请求库)
    VUE 或者JS 常用数据类型及方法:字符串、数组、对象
    VUE实际案例--计数器(商城数量加减)
    VUE学习 --数据类型、el挂点、指令等
  • 原文地址:https://www.cnblogs.com/acha/p/6219437.html
Copyright © 2011-2022 走看看