http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2967

要先找到最深的深度,然后用奇偶性来标识是做交运算还是补运算。奇数表示做叫运算,偶数表示补运算。

然后注意的一点就是要用全局变量i,这样就解决了重复算的问题。值得注意。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
char s[40000];
char s2[3]="()";
int i;
int solve(int k,int id)
{
    int ans=id;
    for(i=++k;;i++)
    {

        if(s[i]==')')return ans;
        else
        if(s[i]=='T')
        {
        if(id)ans&=1;
        else
        ans|=1;
        }
        else
        if(s[i]=='F')
        {
        if(id)ans&=0;
        else
        ans|=0;
        }
        else
        if(s[i]=='(')
        {
        if(id)ans&=solve(i,id^1);
        else
        ans|=solve(i,id^1);
        }
    }
}


int main()
{
    int test=1;
    while(cin>>s)
    {
        int ans=0;
        int temp=0;
        if(!strcmp(s,s2))break;
        int len=strlen(s);
        for(int j=0;j<len;j++)
        {
            if(s[j]=='(')temp++;
            else
            if(s[j]==')')
                ans=max(ans,temp),temp--;
        }

        printf("%d. ",test++);
        i=0;
        temp=solve(0,ans%2);
        if(temp==0)
            cout<<"false"<<endl;
        else
            cout<<"true"<<endl;
    }
    //system("pause");
    return 0;
}
posted on 2012-08-25 01:37  一把刷子  阅读(286)  评论(0编辑  收藏  举报