HNU 12817 Shipura(運算式求值)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   

題目連結:http://acm.hnu.cn/online/?action=problem&type=show&id=12817

解題報告:定義兩種運算子號,一種是>>,就是右移,另一種是S<x>,S<X> = (X^2) % (1e9+7);

跟其它運算式求值一樣,用兩個棧,一個存運算元,另一個存操作符,有一個問題就是>這是符號到底是S<>它的一部分還是>>它的一部分,因為符號>>緊挨右邊一定要有運算元的,而S<>緊挨右邊是一定沒有運算元的,所以只要看是不是兩個連續的>,並且緊挨右邊有沒有運算元,如果都符合的話就是>>。然後就是每次計算完S<>這個之後要對符號棧的棧頂進行判斷,如果是>>就要繼續計算。

  1 #include<cstdio>  2 #include<cstring>  3 #include<iostream>  4 #include<algorithm>  5 #include<deque>  6 #include<cstdlib>  7 using namespace std;  8 typedef long long INT;  9 const INT MOD = 1000000007;  10 const int maxn = 2000000+5; 11 char str[maxn],temp[maxn]; 12 int get_num(INT s,INT t) 13 { 14     for(int i = 0;i < t;++i) 15     { 16         s = s >> 1; 17         if(s == 0) 18         return 0; 19     } 20     return s; 21 } 22 int main() 23 { 24     while(gets(temp)) 25     { 26         if(temp[0] == ‘#‘) 27         break; 28         int len = strlen(temp),f = 0; 29         for(int i = 0;i < len; ++i) 30         if(temp[i] != ‘ ‘) 31         str[f++] = temp[i]; 32         str[f] = NULL; 33         len = f; 34         deque<INT> que1; 35         deque<char> que2; 36         //預先處理 37         for(int i = 0;i < len-2;++i) 38         if(str[i] == ‘>‘ && str[i+1] == ‘>‘ && str[i+2] != ‘>‘) 39         str[i] = str[i+1] = ‘^‘; 40         char ss[20];       //緩衝數字 41         for(int i = 0;i < len;++i) 42         { 43             if(str[i] == ‘S‘  || str[i] == ‘s‘) 44             { 45                 i++; 46                 que2.push_front(‘<‘); 47             } 48             if(str[i] == ‘>‘) 49             { 50                 INT tt = *que1.begin(); 51                 que1.pop_front(); 52                 que2.pop_front();    //計算完後把‘<‘彈出來 53                 tt *= tt; 54                 tt %= MOD; 55                 if(*que2.begin() == ‘^‘) 56                 { 57                     INT tt2 = *que1.begin(); 58                     que1.pop_front(); 59                     que1.push_front(get_num(tt2,tt)); 60                     que2.pop_front(); 61                 } 62                 else que1.push_front(tt); 63             } 64             if(str[i] == ‘^‘) 65             { 66                 i++; 67                 que2.push_front(‘^‘); 68             } 69             if(str[i] >= ‘0‘ && str[i] <= ‘9‘) 70             { 71                 int ll = 0; 72                 while(str[i] >= ‘0‘ && str[i] <= ‘9‘) 73                 { 74                     ss[ll++] = str[i]; 75                     i++; 76                 } 77                 i--; 78                 ss[ll] = NULL; 79                 int tt = atoi(ss); 80                 if(*que2.begin() == ‘^‘) 81                 { 82                     int tt2 = *que1.begin(); 83                     que1.pop_front(); 84                     que2.pop_front(); 85                     que1.push_front(get_num(tt2,tt));  //get_num計算tt2 >> tt之後壓回到棧中 86                 } 87                 else que1.push_front(tt); 88             } 89         } 90         while(!que2.empty()) 91         { 92             if(*que2.begin() == ‘^‘) 93             { 94                 que2.pop_front();   //現在只可能剩下^ 95                 int tt1 = *que1.begin(); 96                 que1.pop_front(); 97                 int tt2 = *que1.begin(); 98                 que1.pop_front(); 99                 que1.push_front(get_num(tt2,tt1));100             }101     /*        else if(*que2.begin() == ‘>‘)102             {103                 INT tt = *que1.begin();104                 que1.pop_front();105                 tt *= tt;106                 tt %= MOD;107                 que1.push_front(tt);108                 que2.pop_front();109                 que2.pop_front();110             }*/111         }112         printf("%d\n",*que1.begin());113         que2.clear();114         que1.clear();115     }116     return 0;117 }
View Code

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.