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;
}
相关推荐
Minecraft红客几秒前
C++制作迷宫第一版
c++·游戏·电脑·娱乐
雪域迷影3 分钟前
Windows11中VS2026使用C++ 现代化json库nlohmann的3种方式
开发语言·c++·json
羑悻的小杀马特6 分钟前
LRU Cache:高频访问数据的“智能保鲜舱”与经典淘汰艺术
c++·后端·lru cache·热点数据与冷数据
zephyr056 分钟前
C++ STL string 用法详解与示例
开发语言·c++
郝学胜-神的一滴7 分钟前
Linux线程的共享资源与非共享资源详解
linux·服务器·开发语言·c++·程序人生·设计模式
郝学胜-神的一滴8 分钟前
Linux进程与线程的区别:从内存三级映射角度深入解析
linux·服务器·c++·程序人生
默凉10 分钟前
c++使用http发送图像
开发语言·c++·http
不爱吃糖的程序媛13 分钟前
OpenHarmony PC 第三方 C/C++ 库适配完整指南
c语言·c++·harmonyos
雪域迷影13 分钟前
nlohmann::json库对象和json结构体转换的新方式
c++·json·nlohmann_json库
浔川python社18 分钟前
C++小程序编写系列(2)
c++·算法·图论