1596 - Bug Hunt (UVA)

题目链接如下:

https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=448&page=show_problem&problem=4471

我的代码如下:

cpp 复制代码
#include <iostream>
#include <string>
#include <map>
// #define debug

struct arr{
    int len;
    std::map<int, int> mp;
};
std::string s;
int bug;
std::map<std::string, arr> arrMp;

bool validJudge(std::string &str, int &value){
    if (str.find("[") == std::string::npos){
        value = std::stoi(str);
        return true;
    }
    int left = str.find("[");
    int right = str.rfind("]");
    int v;
    std::string temp1, temp2;
    temp1 = str.substr(left + 1, right - left - 1);
    temp2 = str.substr(0, left);
    if (!validJudge(temp1, v) || arrMp[temp2].len <= v || arrMp[temp2].mp.find(v) == arrMp[temp2].mp.end()){
        return false;
    }
    value = arrMp[temp2].mp[v];
    return true;
}

bool judge(std::string &str){
    int left, right, v1, v2;
    std::string temp, temp1, temp2;
    if (str.find("=") == std::string::npos){
        left = str.find("[");
        right = str.rfind("]");
        temp = str.substr(left + 1, right - left - 1);
        if (!validJudge(temp, v1)){
            return false;
        }
        arrMp[str.substr(0, left)].len = v1;
        return true;
    }
    int equLoc = str.find("=");
    left = str.find("[");
    right = str.rfind("]", equLoc);
    temp1 = str.substr(left + 1, right - left - 1);
    temp2 = str.substr(equLoc + 1);
    if (!validJudge(temp1, v1) || v1 >= arrMp[str.substr(0, left)].len || !validJudge(temp2, v2)){
        return false;
    }
    temp = str.substr(0, left);
    arrMp[temp].mp[v1] = v2;
    return true;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (getline(std::cin, s) && s != "."){
        bug = 0;
        int i = 0;
        arrMp.clear();
        do {
            if (bug) continue;
            ++i;
            if (!judge(s)){
                bug = i;
            }
        } while (getline(std::cin, s) && s != ".");
        printf("%d\n", bug);
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
眠りたいです1 小时前
Mysql常用内置函数,复合查询及内外连接
linux·数据库·c++·mysql
笑鸿的学习笔记2 小时前
qt-C++语法笔记之Stretch与Spacer的关系分析
c++·笔记·qt
hardStudy_h2 小时前
C++——内联函数与Lambda表达式
开发语言·jvm·c++
ZZZS05163 小时前
stack栈练习
c++·笔记·学习·算法·动态规划
位东风3 小时前
【c++学习记录】状态模式,实现一个登陆功能
c++·学习·状态模式
雷羿 LexChien5 小时前
C++内存泄漏排查
开发语言·c++
嘉小华6 小时前
CMake 完全指南:第一章 - 构建的烦恼 - 为什么需要CMake?
c++
Feliz Da Vida6 小时前
[代码学习] c++ 通过H矩阵快速生成图像对应的mask
c++·学习
无聊的小坏坏7 小时前
单调栈通关指南:从力扣 84 到力扣 42
c++·算法·leetcode
YOLO大师7 小时前
华为OD机试 2025B卷 - 小明减肥(C++&Python&JAVA&JS&C语言)
c++·python·华为od·华为od机试·华为od2025b卷·华为机试2025b卷·华为od机试2025b卷