回 帖 发 新 帖 刷新版面

主题:[原创]关于一道ACM题的解法

Problem
At the University of Kentucky, they build a lot of high-performance computer hardware and software, often using one supercomputer to design the next. One of the most fundamental computer design problems is logic optimization: making sure that the optimized logic still computes the same function as the original design. 
For this problem, your program will be given two logic expressions to compare for logical equivalence.

  
 Input
The first line of the input consists of a positive integer n, which is the number of datasets (lines) that follow. Each dataset consists of a single line containing the two input expressions to be tested. The input expressions consist of any of 26 variables named a-z, the binary operators |, &, ^, (OR, AND and XOR respectively), the unary ~ (NOT), and parenthesis. The expressions should be evaluated ignoring all other characters and with operator precedence as in the C language (parenthesis, ~, &, ^, |). The two expressions will be input in sequence and it is up to your program to determine where one expression ends and the next begins.

Most logic manipulation programs would convert each expression into a normal form and check if the two normalized expressions are identical. Fortunately for you, each expression will consist of no more than 100 operations using no more than 10 different variables. For that many cases, you can test for equivalence by simply evaluating the two input expressions for all possible inputs and comparing the results.
   
 Output
For each data set, print:

Data set N: Equivalent

if the expressions produce the same result, or:

Data set N: Different

if they produce different results. Of course N should be replaced by the data set number.     

   
 Sample Input
 Copy to clipboard
3
a ^b&(b|a)~b^ a
a^b&(b|a)(a^(b&(b|a)))
~~~~z~~z
    
   
 Sample Output
Data set 1: Different
Data set 2: Equivalent
Data set 3: Equivalent

 

回复列表 (共6个回复)

沙发

#include<stdio.h>
#include<math.h>
#define MAXSIZ   100
#define SIZE     200
#define  isDigit(ch)   (ch>='0'&&ch<='9')
#define  isOp(ch)      (ch=='~'||ch=='&'||ch=='|'||ch=='^')
#define  isAlpha(ch)   ((ch|0x20)>='a'&&(ch|0x20)<='z')
#define  intToA(n,pch) do{int stack[10],m=n,i=0;\
                              while(m)\
                              {\
                                  stack[i++]=m%10;m=m/10;\
                              }\
                              while(--i>=0)\
                              {\
                                  *pch=stack[i]+'0';pch++;\
                              }\
                           }while(0)           //用于将整数n转化成相应的charater
                                                                          

int calc(int a,char opera,int b)//计算a opera b
{
    int res;
    switch(opera)
    {
    case '&':res=a&b;break;
    case '|':res=a|b;break;
    case '^':res=a^b;break;
    default:break; 
    }
    return res;
}
int priority(char opera1,char opera2)//比较优先级
{
    int compRes;
    switch(opera1)
    {
    case '~':compRes=1;break;
    case '&':
             compRes=opera2=='~'?-1:(opera2=='^'?0:1);break;
    case '^':
             compRes=opera2=='~'?-1:(opera2=='&'?0:1);break;
    case '|':
             compRes=opera2=='('?1:-1;break;
    default:break; 
    }
    return compRes;
}

板凳

int calcExpValue(char* exp,int len)//表达式求值
{
   int  res[len+1],a,b;
   char op[len+1],*pch;
   int i,j,k;
   
   pch=exp;
   i=0;
   j=-1;
   while(*pch!='\0')
   {
       if(isDigit(*pch))
       {
           res[i]=0;
           while(*pch!='\0'&&isDigit(*pch))
           {
               res[i]=res[i]*10+*pch-'0';
               pch++;
           }
           i++;
       }
       else if(isOp(*pch))
       {
            if(j<0||priority(*pch,op[j])>0)
            {
                op[++j]=*pch;
            }
            else
            {
                   do                              
                 {
                      b=res[i-1];     
                      if(op[j]=='~')
                      {
                          res[i-1]=~b;
                      }
                      else
                      {
                          a=res[i-2];
                          res[i-2]=calc(a,op[j],b);
                          i--;
                      }
                      j--;
                 }while(j>=0&&priority(*pch,op[j])<=0);
                 op[++j]=*pch;
            }
            pch++;
       }
       else if(*pch=='(') 
       {
            op[++j]=*pch++;
       }
       else
       {
           while(op[j]!='(')
           { 
               b=res[i-1]; 
               if(op[j]=='~') 
               {
                   res[i-1]=~b;
               } 
               else
               {
                   a=res[i-2];
                   res[i-2]=calc(a,op[j],b);
                   i--;
               }
               j--;
           }
           j--;
           pch++;
       }
   }
   while(j>=0)
   {
       b=res[i-1];
       if(op[j]=='~')
       {
           res[i-1]=~b;
       } 
       else
       {
           a=res[i-2];
           res[i-2]=calc(a,op[j],b);
           i--;
       }
       j--;
   }
   return res[0];
}

3 楼

void convertion(char* exp1,char* exp2)//完成将表达式exp1和exp2中的变元赋随机值,并以字符串
//的形式保存在exp1和exp2
{
     char temp1[SIZE*2],temp2[SIZE*2],*pch1,*pch2,*p;
     char param1[11],param2[11];
     int len1,len2,i,j,k,val1[11],val2[11],v1len,v2len,flag;
     
     len1=strlen(exp1);
     len2=strlen(exp2);
     pch1=exp1;
     pch2=exp2;
     v1len=0;
     v2len=0;
     for(i=0,k=0;*pch1!='\0';i++)
     {
         if(isAlpha(*pch1))
         {
             for(j=0,flag=0;j<v1len;j++)
             {
                 if(param1[j]==*pch1)
                 {
                     flag=1;break;
                 }
             }
             if(flag)
             {
                val1[v1len]=val1[j];
             }
             else
             {
                 srand(i);
                 val1[v1len]=rand()%10000; 
             }
             char* p=temp1+k;
             int inc;
             intToA(val1[v1len],p);
             inc=p-temp1-k;
             k+=inc;
             param1[v1len]=*pch1;
             v1len++;
         }
         else
         {
             temp1[k++]=*pch1;
         }
         pch1++;
     }
     temp1[k]='\0';
     param1[v1len]='\0';
     val1[v1len]='\0';
     pch2=exp2;
     for(i=len2,k=0;*pch2!='\0';)
     {
         if(isAlpha(*pch2))
         {
             for(j=0,flag=0;j<v1len;j++)
             {
                 if(*pch2==param1[j])
                 {
                     flag=1;break;
                 }
             }
             if(flag)
             {
                 val2[v2len]=val1[j];
             }
             else
             {
                 for(j=0,flag=0;j<v2len;j++)
                 {
                     if(*pch2==param2[j])
                     {
                         flag=1;break;
                     }
                 }
                 if(flag)
                 {
                     val2[v2len]=val2[j];
                 }
                 else
                 {
                     srand(i);
                     val2[v2len]=rand()%10000;
                 }
             }
             char* p=temp2+k;
             int inc;
             intToA(val2[v2len],p);
             inc=p-temp2-k;
             k+=inc;
             param2[v1len]=*pch2;
             v2len++;
         }
         else
         {
             temp2[k++]=*pch2;
         }
         pch2++;
     }
     temp2[k]='\0';
     strcpy(exp1,temp1);
     strcpy(exp2,temp2);
     return ;
}

4 楼

void split(char* s,char*a,char*b)//完成将输入字符串分解成两个表达式a和b
{
     int len=strlen(s),i,j,state;
     char *pch,str[len+1];
     
     pch=s;
     len=0;
     while(*pch!='\0')
     {
         if(*pch!=' '&&*pch!='\t')
         {
             str[len++]=*pch;
         }
         pch++;
     }
     str[len]='\0';
     i=j=0;
     pch=str;
     state=1;
     while(1)
     {
          if(state&1)
          {
               if(*pch>='a'&&*pch<='z')
               {
                    state=2;
               }
               a[i++]=*pch;
          }
          else
          {
              if(*pch==')')
              {
                  a[i++]=*pch;
              }
              else if(*pch=='|'||*pch=='&'||*pch=='^')
              {
                   a[i++]=*pch;
                   state=1;
              }
              else
              {
                  break;
              }
          }
          pch++;
     }
     a[i]='\0';
     while(*pch!='\0')
     {
         b[j++]=*pch++;
     }
     b[j]='\0';
     return ;
}
int judge(char* exp)//随机化100次后判断是否仍相等,是返回1,否则返回0
{
    char exp1[SIZE*2+1],exp2[SIZE*2+1];
    int flag,i;
    split(exp,exp1,exp2);
    for(i=0;i<1100;i++)
    {
        convertion(exp1,exp2);
        if(calcExpValue(exp1,strlen(exp1))!=calcExpValue(exp2,strlen(exp2)))
        {
            return 0;
        }
    }
    return 1;
}
int main(void)
{
    char exp[SIZE*2+1];
    int cases,count;
    scanf("%d",&cases);
    count=1;
    gets(exp);
    while(cases--)
    {
        gets(exp);
        printf("Data set %d: ",count++);
        if(judge(exp))
        {
            printf("Eqivalent\n");
        }
        else
        {
            printf("Different\n");
        }
    }
    return 0;
}
//以上就是我解题的基本思想,为甚么提交是Wrong Answer,按理说随机赋值再循环1100还判断错误的概率几近为0的呀~~~~~~~~~~~~~~~~

5 楼

才100次够么?
a|b|c|d|e|f|g|h|i|j a|~a

6 楼

100次通常情况下应该已经够了,问题是:我现在已经把它加到了1100次,还是不能通过~~~~兄台有何高见,望不吝指教啊~~~~~

我来回复

您尚未登录,请登录后再回复。点此登录或注册