zoukankan      html  css  js  c++  java
  • noip 2005 等价表达式

    /*
    开始想的是 维护a的每个指数的系数 
    然而不好办 然而还有^10^10^10这种数据
    特殊值带入吧 多搞几个素数 接下来就是玄学的事了
    给a赋值之后 就是简单地表达式求值
    虽然思路简单 但是字符串一向很恶心、、 数据括号有问题。。。。
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define maxn 255
    #define mod 10007
    using namespace std;
    int n,a,top1,top2,l,li,s1[maxn],s2[maxn];
    int ans[6]={0,1007,1607,1847,2711,2713};
    int target[6];
    int order[100];
    char s[maxn],si[maxn],c;
    int Mi(int x,int y)
    {
        int ans=x;
        for(int i=2;i<=y;i++)
          ans=(ans*x)%mod;
        return ans;
    }
    int Go(int x,int y,char z)
    {
        if(z==45)return (x-y+mod)%mod;
        if(z==43)return (x+y)%mod;
        if(z==42)return (x*y)%mod;
        if(z==94)return Mi(x,y);
    }
    int main()
    {
        order[45]=order[43]=1;
        order[42]=2;order[94]=3;
        gets(si);li=strlen(si+1);l=0;
        for(int i=0;i<=li;i++)
          if(si[i]==' ')continue;
          else s[++l]=si[i];
        s[0]='(';s[++l]=')';
        for(int k=1;k<=5;k++)
          {
            a=ans[k];int i=0;top1=top2=0;
            memset(s1,0,sizeof(s1));
            memset(s2,0,sizeof(s2));
            while(i<=l)
              {
                if(s[i]=='a')
                  {
                      if(top1>=1&&s[i-1]>='0'&&s[i-1]<='9')
                      {
                          int tmp=s1[top1];
                         s1[top1]=a*tmp%mod;
                      }
                    else top1=top1+1,s1[top1]=a;
                  }
                if(s[i]>='0'&&s[i]<='9')
                    {
                        int x=0;
                        while(s[i]>='0'&&s[i]<='9'){x=x*10+s[i]-'0';i++;}
                        top1=top1+1;s1[top1]=x;i=i-1; 
                    }
                   else
                    {
                        if(s[i]=='(')top2=top2+1,s2[top2]=s[i];
                        if(s[i]=='^')
                           {
                             int x=0;i=i+1;int r=s1[top1];
                             while(s[i]>='0'&&s[i]<='9'){x=x*10+s[i]-'0';i++;}
                           s1[top1]=Go(r,x,'^');i=i-1;
                         }
                        if(s[i]=='+'||s[i]=='-'||s[i]=='*')
                        {
                          if(order[s[i]]<=order[s2[top2]])
                            {
                              while(order[s[i]]<=order[s2[top2]])
                                  {
                                    int r1=s1[top1];top1=top1-1;
                                  int r2=s1[top1];top1=top1-1;
                                   int r3=Go(r2,r1,s2[top2]);
                                   top2=top2-1;top1=top1+1;s1[top1]=r3;
                                }
                              top2=top2+1;s2[top2]=s[i];
                            }
                          else if(order[s[i]]>order[s2[top2]])top2=top2+1,s2[top2]=s[i];
                        }
                      if(s[i]==')')
                        {
                            while(s2[top2]!='('&&top2)
                              {
                                int r1=s1[top1];top1=top1-1;
                              int r2=s1[top1];top1=top1-1;
                                int r3=Go(r2,r1,s2[top2]);
                                top2=top2-1;top1=top1+1;s1[top1]=r3;
                            }
                          top2=top2-1;
                        }
                     }
                   i=i+1;
                }
            target[k]=s1[1];
          }
        scanf("%d",&n);c=getchar();
        for(int p=1;p<=n;p++)
          {
              li=0;memset(si,0,sizeof(si));
              gets(si);li=strlen(si+1);l=0;
            for(int i=0;i<=li;i++)
              if(si[i]==' ')continue;
               else s[++l]=si[i];
            s[0]='(';s[++l]=')';int sum=0;
            for(int k=1;k<=5;k++)
              {
                a=ans[k];int i=0;top1=top2=0;
                memset(s1,0,sizeof(s1));
                memset(s2,0,sizeof(s2));
                  while(i<=l)
                  {
                    if(s[i]=='a')
                      {
                          if(top1>=1&&s[i-1]>='0'&&s[i-1]<='9')
                          {
                              int tmp=s1[top1];
                              s1[top1]=a*tmp%mod;
                          }
                        else top1=top1+1,s1[top1]=a;
                      }
                    if(s[i]>='0'&&s[i]<='9')
                        {
                             int x=0;
                              while(s[i]>='0'&&s[i]<='9'){x=x*10+s[i]-'0';i++;}
                            top1=top1+1;s1[top1]=x;i=i-1;
                        }
                       else
                        {
                            if(s[i]=='(')top2=top2+1,s2[top2]=s[i];
                            if(s[i]=='^')
                               {
                                 int x=0;i=i+1;int r=s1[top1];
                                 while(s[i]>='0'&&s[i]<='9'){x=x*10+s[i]-'0';i++;}
                                s1[top1]=Go(r,x,'^');i=i-1;
                             }
                            if(s[i]=='+'||s[i]=='-'||s[i]=='*')
                            {
                               if(order[s[i]]<=order[s2[top2]])
                                {
                                  while(order[s[i]]<=order[s2[top2]])
                                      {
                                        int r1=s1[top1];top1=top1-1;
                                       int r2=s1[top1];top1=top1-1;
                                       int r3=Go(r2,r1,s2[top2]);
                                       top2=top2-1;top1=top1+1;s1[top1]=r3;
                                    }
                                  top2=top2+1;s2[top2]=s[i];
                                }
                              if(order[s[i]]>order[s2[top2]])top2=top2+1,s2[top2]=s[i];
                            }
                          if(s[i]==')')
                            {
                                   while(s2[top2]!='('&&top2)
                                  {
                                    int r1=s1[top1];top1=top1-1;
                                  int r2=s1[top1];top1=top1-1;
                                    int r3=Go(r2,r1,s2[top2]);
                                     top2=top2-1;top1=top1+1;s1[top1]=r3;
                                }
                              top2=top2-1;
                             }
                         }
                   i=i+1;
                  }
                if(s1[1]==target[k])sum++;
              }
            if(sum==5)printf("%c",'A'-1+p);
          }
        return 0; 
    }
  • 相关阅读:
    hisi3516/3519开发(二)—xshell连接串口
    linux svn使用
    IdentityServer4 源码介绍
    想写博客
    # VS2019 快捷键插入当前时间
    # 使用 vscode markdown 遇到的问题
    # 学Vue
    teXt使用
    Linux基础
    NopCommerce(Core)学习目录
  • 原文地址:https://www.cnblogs.com/yanlifneg/p/5670796.html
Copyright © 2011-2022 走看看