题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12817

解题报告:定义两种运算符号,一种是>>,就是右移,另一种是S<x>,S<X> = (X^2) % (1e9+7);

跟其它表达式求值一样,用两个栈,一个存操作数,另一个存操作符,有一个问题就是>这是符号到底是S<>它的一部分还是>>它的一部分,因为符号>>紧挨右边一定要有操作数的,而S<>紧挨右边是一定没有操作数的,所以只要看是不是两个连续的>,并且紧挨右边有没有操作数,如果都符合的话就是>>。然后就是每次计算完S<>这个之后要对符号栈的栈顶进行判断,如果是>>就要继续计算。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<deque>
#include<cstdlib>
using namespace std;
typedef long long INT;
const INT MOD = ;
const int maxn = +;
char str[maxn],temp[maxn];
int get_num(INT s,INT t)
{
for(int i = ;i < t;++i)
{
s = s >> ;
if(s == )
return ;
}
return s;
}
int main()
{
while(gets(temp))
{
if(temp[] == '#')
break;
int len = strlen(temp),f = ;
for(int i = ;i < len; ++i)
if(temp[i] != ' ')
str[f++] = temp[i];
str[f] = NULL;
len = f;
deque<INT> que1;
deque<char> que2;
//预处理
for(int i = ;i < len-;++i)
if(str[i] == '>' && str[i+] == '>' && str[i+] != '>')
str[i] = str[i+] = '^';
char ss[]; //缓存数字
for(int i = ;i < len;++i)
{
if(str[i] == 'S' || str[i] == 's')
{
i++;
que2.push_front('<');
}
if(str[i] == '>')
{
INT tt = *que1.begin();
que1.pop_front();
que2.pop_front(); //计算完后把'<'弹出来
tt *= tt;
tt %= MOD;
if(*que2.begin() == '^')
{
INT tt2 = *que1.begin();
que1.pop_front();
que1.push_front(get_num(tt2,tt));
que2.pop_front();
}
else que1.push_front(tt);
}
if(str[i] == '^')
{
i++;
que2.push_front('^');
}
if(str[i] >= '' && str[i] <= '')
{
int ll = ;
while(str[i] >= '' && str[i] <= '')
{
ss[ll++] = str[i];
i++;
}
i--;
ss[ll] = NULL;
int tt = atoi(ss);
if(*que2.begin() == '^')
{
int tt2 = *que1.begin();
que1.pop_front();
que2.pop_front();
que1.push_front(get_num(tt2,tt)); //get_num计算tt2 >> tt之后压回到栈中
}
else que1.push_front(tt);
}
}
while(!que2.empty())
{
if(*que2.begin() == '^')
{
que2.pop_front(); //现在只可能剩下^
int tt1 = *que1.begin();
que1.pop_front();
int tt2 = *que1.begin();
que1.pop_front();
que1.push_front(get_num(tt2,tt1));
}
/* else if(*que2.begin() == '>')
{
INT tt = *que1.begin();
que1.pop_front();
tt *= tt;
tt %= MOD;
que1.push_front(tt);
que2.pop_front();
que2.pop_front();
}*/
}
printf("%d\n",*que1.begin());
que2.clear();
que1.clear();
}
return ;
}

HNU 12817 Shipura(表达式求值)的更多相关文章

  1. 表达式求值(noip2015等价表达式)

    题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...

  2. 用Python3实现表达式求值

    一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...

  3. 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值

    一.简介 迷宫求解:类似图的DFS.具体的算法思路可以参考书上的50.51页,不过书上只说了粗略的算法,实现起来还是有很多细节需要注意.大多数只是给了个抽象的名字,甚至参数类型,返回值也没说的很清楚, ...

  4. nyoj305_表达式求值

    表达式求值 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...

  5. 利用栈实现算术表达式求值(Java语言描述)

    利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...

  6. 数据结构--栈的应用(表达式求值 nyoj 35)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...

  7. NOIP2013普及组 T2 表达式求值

    OJ地址:洛谷P1981 CODEVS 3292 正常写法是用栈 #include<iostream> #include<algorithm> #include<cmat ...

  8. NOIP201302表达式求值

    NOIP201302表达式求值 题目描述 Description 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入描述 Input Description 输入仅有一行,为需要你计 ...

  9. OpenJudge计算概论-简单算术表达式求值

    /*===================================== 简单算术表达式求值 总时间限制: 1000ms 内存限制: 65536kB 描述 2位正整数的简单算术运算(只考虑整数运 ...

随机推荐

  1. 第十章 使用MapKit

    本项目是<beginning iOS8 programming with swift>中的项目学习笔记==>全部笔记目录 ------------------------------ ...

  2. sublime文档

    英文http://docs.sublimetext.info/en/latest/index.html 翻译http://feliving.github.io/Sublime-Text-3-Docum ...

  3. [BZOJ 1483][HNOI 2009]梦幻补丁(有序表启发式合并)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1483 分析: 先将不同的颜色的出现位置从小到大用几条链表串起来,然后统计一下答案 对于 ...

  4. chromiun 学习《一》

    众所周知,Chrome是建立在开源的Chromium项目上的. 而且不得不说,学习并分析开源项目的代码对一个程序员的提高确实蛮大的.这篇博文我会记录一下学习过程中我遇到的一些问题,并分享学习中我所参考 ...

  5. python 参数

    摘自:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00137473844933 ...

  6. 输入年月,输出月份有几天(分别用了if——else和switch)

    首先是switch做的 class Program { static void Main(string[] args) {/* 题目要求:请用户输入年份,输入月份,输出该月的天数. 思路:一年中月份的 ...

  7. emberJS

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 用freemarker生产静态页面

    FreeMarker概述 * FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写     * Template + data model = output    ...

  9. artDialog 文档

    artDialog —— 经典.优雅的网页对话框控件. 支持普通与 12 方向气泡状对话框 完善的焦点处理,自动焦点附加与回退 支持 ARIA 标准 面向未来:基于 HTML5 Dialog 的 AP ...

  10. Day1 三级目录

    d_city = { "河南" : {"郑州" : ["二七区","中原区","回族管城区",&qu ...