翻译布尔表达式
SDUT OJ 翻译布尔表达式 
 翻译布尔表达式
 Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
 大家都学过了布尔表达式的翻译,其中有一个拉链-回填技术,这次我们就练习这个技术。
Input
 输入为一行字符串,例如: a < b or c < d and e < f
每个符号都用空格间隔。
其中逻辑运算符包含 and 和 or , 关系运算符包含 < 、> 、<= 、 >= 、== 、 != 。
Output
 假链跳到0,真链跳到1,表达式序号从100开始排。
Sample Input
 a < b or c < d and e < f
 Sample Output
 100(j<,a,b,1)
 101(j,,,102)
 102(j<,c,d,104)
 103(j,,,0)
 104(j<,e,f,100)
 105(j,,,103)
问题思路:
划分and和or运算,只在一种情况下输出。
分析and和or的特点:
and运算:有假则假,若假则直接跳转到假出口,若真则继续探索。
or运算:有真则真,若真则直接跳转到真出口,若假则继续探索。
本题的解题思路就是利用假出口回填,一直探索假出口的位置
#include <bits/stdc++.h>
using namespace std;
vector <string> ans;
int main()
{
    string s,x;
    getline(cin, s);
    //控制最后的输出,若最后ans里只有and也可以输出
    s += " end";
    stringstream ss(s);
    int F = 100, id = 100, T = 1;
    while(ss >> x)
    {
        if(x == "or" || x == "end")
        {
            if(x == "or")
                F += 2;
            else
                F = 0;
            int n = ans.size();
            //输出and运算的内容
            for(int i = 0; i < n-3; i += 3)
            {
                cout <<id<< "(j" << ans[i+1].c_str()<<','<<ans[i].c_str()<<','<<ans[i+2].c_str()<<','<<id+2<<')'<<endl;
                id++;
                //and运算有假则假,直接跳转到假出口,同时需要记录假出口,方便回填
                cout <<id<< "(j" << ",_,_," << F << ')' << endl;
                F = id++;
            }
            //对于or运算,有真则真,直接跳转到真出口,同时需要记录真出口的位置,方便回填
            cout <<id<< "(j" << ans[n-2].c_str()<<','<<ans[n-3].c_str()<<','<<ans[n-1].c_str()<<','<<T<<')'<<endl;
            T = id++;
            cout <<id<< "(j" << ",_,_," << F << ')' << endl;
            id++;
            //清空
            ans.clear();
        }
        else if(x == "and")
            F += 2;
        else
            ans.push_back(x);
    }
    return 0;
}
 
                   
                   
                   
                   
                             
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   3301
					3301
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            