一、题目链接

  http://codeforces.com/gym/100198/problem/G

二、题意

  看样例就能明白,写表达式解析器。

三 、思路

  一看这题目,立马就会想到“后缀表达式”,考虑到这里有变量,在把中缀表达式变为后缀表达式时,把变量替换成常量即可。注意,define 2 3 表示,下面的2用3来替换。和C语言是一样的。

PS:然而,我用Java写的,总是RE在第四个样例,实在找不到哪里抛异常了。各位同行,如果您能找到我的代码的bug,烦请您在评论区留下出bug的样例。谢谢!

四、源代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    static final String digitPattern = "-?\\d+";
    static final File fin = new File("plcool.in");
    static final File fout = new File("plcool.out");
    static String input;
    static ArrayList<String> inExp = new ArrayList<String>();
    static ArrayList<String> postExp = new ArrayList<String>();
    static Map<Character, Integer> prior = new HashMap<Character, Integer>();
    static Map<String, String> define = new HashMap<String, String>();
    static InputStream is = null;
    static PrintStream ps = null;
    static {
        prior.put('^', 3);
        prior.put('*', 2);
        prior.put('/', 2);
        prior.put('%', 2);
        prior.put('+', 1);
        prior.put('-', 1);
        prior.put('(', 0);
        prior.put(')', 0);
    }

    static String findRoot(String key) {
        if(!define.containsKey(key))return key;
        else return findRoot(define.get(key));
    }

    static int power(int x, int y) {
        int res = 1;
        while(y > 0) {
            if((y & 1) == 1)res *= x;
            x = x * x;
            y >>= 1;
        }
        return res;
    }

    static int mod(int x, int y) {
        int sign = ((x >>> 31) ^ (y >>> 31)) == 0 ? 1 : -1;
        return (Math.abs(x) % Math.abs(y)) * sign;
    }

    static void split() {
        inExp.clear();
        int s = 0, t = 0, len = input.length();
        boolean befOper = false;
        for (int i = 0; i < len; ++i) {
            if(Character.isDigit(input.charAt(i)) || (input.charAt(i) == '-' && (befOper || i == 0))) {
                for(s = i++;i < len && Character.isDigit(input.charAt(i)); ++i);
                inExp.add(input.substring(s, i));
                --i;
                befOper = false;
            }
            else if(Character.isLetter(input.charAt(i))) {
                for(s = i;i < len && (Character.isLetter(input.charAt(i))
                        || Character.isDigit(input.charAt(i))); ++i);
                inExp.add(input.substring(s, i));
                --i;
            }
            else if(prior.containsKey(input.charAt(i))) {
                inExp.add(input.charAt(i) + "");
                befOper = true;
            }
        }
        //inExp.forEach(e -> ps.print(e + " "));
        for (int i = 0, sz = inExp.size(); i < sz; ++i) {
            String now = inExp.get(i);
            if (define.containsKey(now)) {
                String root = findRoot(now);
                if(!root.matches(digitPattern))root = "0";
                inExp.set(i, root);
            }
            else if(!prior.containsKey(now.charAt(0)) && !now.matches(digitPattern))inExp.set(i, "0");
        }
        //inExp.forEach(e -> ps.print(e + " "));
    }

    static Stack<Character> opers = new Stack<Character>();
    static void toPost() {
        opers.clear();
        postExp.clear();
        for (String s : inExp) {
            if (s.matches(digitPattern))
                postExp.add(s);
            else {
                if (opers.isEmpty())
                    opers.push(s.charAt(0));
                else {
                    char top = opers.peek();
                    char c = s.charAt(0);
                    if (s.equals(")")){
                        while (!opers.isEmpty() && opers.peek() != '(') {
                            postExp.add(opers.pop() + "");
                        }
                        if (!opers.isEmpty() && opers.peek() == '(')
                            opers.pop();
                    }
                    else if((c != '(' && c != '^' && prior.get(c) <= prior.get(top))){
                        for(top = opers.peek();prior.get(c) <= prior.get(top);) {
                            postExp.add(opers.pop() + "");
                            if(!opers.isEmpty())top = opers.peek();
                            else break;
                        }
                        opers.push(c);
                    }
                    else
                        opers.push(c);
                }
            }
        }
        while(!opers.isEmpty()) {
            char c = opers.pop();
            if(c == ')')continue;
            else postExp.add(c + "");
        }
        //postExp.forEach(e -> ps.print(e + " "));
    }

    static Stack<Integer> digits = new Stack<Integer>();
    static int calcResult() {
        digits.clear();
        for(String s : postExp) {
            if(s.matches(digitPattern))digits.push(Integer.parseInt(s));
            else {
                char c = s.charAt(0);
                int last1 = digits.pop(), last2 = digits.pop();
                switch (c) {
                case '-':{
                    digits.push(last2 - last1);
                    break;
                }
                case '+':{
                    digits.push(last2 + last1);
                    break;
                }
                case '*':{
                    digits.push(last2 * last1);
                    break;
                }
                case '/':{
                    digits.push(last2 / last1);
                    break;
                }
                case '%':{
                    digits.push(mod(last2, last1));
                    break;
                }
                case '^':{
                    digits.push(power(last2, last1));
                    break;
                }
                }
            }
        }
        return digits.pop();
    }

    static String[] parts;
    public static void main(String[] args) throws Exception {
//        is = new FileInputStream(fin);
//        ps = new PrintStream(new FileOutputStream(fout));
        is = System.in;
        ps = System.out;
        Scanner scanner = new Scanner(is);
        while (scanner.hasNextLine()) {
            input = scanner.nextLine().trim().toLowerCase();
            if (input.charAt(0) == 'p') {
                input = input.replaceAll(" ", "");
                input = input.substring(5);
                split();
                toPost();
                int res = calcResult();
                ps.println(res);
            }

            else {
                parts = input.split(" ");
                if (define.containsKey(parts[1]))
                    continue;
                else {
                    String root1 = findRoot(parts[1]);
                    String root2 = findRoot(parts[2]);
                    if(!root1.equals(root2))define.put(parts[1], parts[2]);
                }
            }
        }
        scanner.close();
    }
}

CFGym 100198G 题解的更多相关文章

  1. CFGym 101490J 题解

    一.题目链接 http://codeforces.com/gym/101490 二.题面 三.题意 给你n个点,代表学生所在位置,n个点,代表老师所在位置.每个学生分配一个老师.让你找出一个最小的学生 ...

  2. CFGym 101490E 题解

    一.题目链接 http://codeforces.com/gym/101490 二.题面 三.题意 给你一个图,n个点,m条边,一个x,从顶点1走到顶点n.假设从顶点1走到顶点n的最短路为d,x代表你 ...

  3. CFGym 101161I 题解

    一.题目链接 http://codeforces.com/gym/101161/problem/I 二.题意 给定一棵树,一个初始的省会城市,若干个询问,0表示修改省会城市,1表示查询去省会必须经过指 ...

  4. CFGym 101194L 题解

    一.题目链接 http://codeforces.com/gym/101194/problem/L 二.题意 有4个队伍,要打6场比赛(刚好每两个队伍都能相互比一次),若A和B比赛有3种结果: A赢B ...

  5. CFGym 101194D 题解

    一.题目链接 http://codeforces.com/gym/101194/problem/D 二.题意 给定一个数字n和一个数字k,一个n个整数的序列,让你在里面找尽可能多的长度为k的符合“要求 ...

  6. CFGym 101505I 题解

    一.题目链接 http://codeforces.com/gym/101505 二.题意 这题其实主要就是题意,理解题意后,就是水题了.我想了下,主要原因就是这几点: 1.题意太过英文化,很多句子不能 ...

  7. CFGym 100211J 题解

    一.题目 二.题意 给定一个字母表(最多也就是英文小写字母的前10个字母),一个交换表,两个字符串,判断字符串A能否通过交换表的交换方式变成字符串B. 三.思路 1.一开始,比赛时,我半模拟半记忆化地 ...

  8. [CF-GYM]Abu Tahun Mod problem题解

    前言 这道题比较简单,但我还是想了好一会 题意简述 Abu Tahun很喜欢回文. 一个数组若是回文的,那么它从前往后读和从后往前读都是一样的,比如数组\(\left\{1\right\},\left ...

  9. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

随机推荐

  1. QtTcp_资料

    1.百度搜索关键字“Qt TCP” 2. 2.1.Qt学习之路_5(Qt TCP的初步使用) http://www.cnblogs.com/tornadomeet/archive/2012/06/30 ...

  2. Missing artifact com.github.pagehelper:pagehelper:jar:3.4.2-fix的解决方法

    使用pagehelper.3.4.2.jar时报错,应该是无法从网络上下载该jar. 我的解决方案是: 从网络上下载一个pagehelper.3.4.2.jar包,然后复制到.m2目录中 如我的目录是 ...

  3. Spring学习10- bean的生命周期(阿里面试题目两次面试均提到)

    找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...

  4. Use appropriate component from RM component palette

    --------------------------- 错误 --------------------------- Opened report contains the following non- ...

  5. CSS用法

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  6. springboot---没有配置数据库启动报错

    If you want an embedded database please put a supported one on the classpath. If you have database s ...

  7. navicat链接mysql 8 出现 2015 authentication plugin 'caching_sha2_password' 错误

    使用mysql自带的 MySQL 8.0 Command Line Client - Unicode 登录, 然后使用命令: alter user 'root'@'localhost' identif ...

  8. C#中IEnumerable和IEnumerator区别

    IEnumerator:是一个真正的集合访问器,提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型.IEnumerable: ...

  9. 关于EPoll的个人理解

    1.epoll 是I/o多路复用的一种解决方案,对比select的优点有: a.支持打开最大的文件描述符(可高达百万) b.效率并不随着描述符的增多而线性下降.select每次是轮询,所以描述符越多效 ...

  10. HDU 1198

    http://acm.hdu.edu.cn/showproblem.php?pid=1198 裸并查集,主要工作在根据题目给出关系构图 #include <iostream> #inclu ...