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;
}
相关推荐
C++ 老炮儿的技术栈28 分钟前
GCC编译时无法向/tmp 目录写入临时汇编文件,因为设备空间不足,解决
linux·运维·开发语言·汇编·c++·git·qt
橘颂TA35 分钟前
【笔试】算法的暴力美学——牛客 NC213140 :除2!
c++·算法·结构与算法
wsoz1 小时前
Leetcode普通数组-day5、6
c++·算法·leetcode·数组
favour_you___1 小时前
2026_4_8算法练习题
数据结构·c++·算法
SccTsAxR2 小时前
算法基石:手撕离散化、递归与分治
c++·经验分享·笔记·算法
Q741_1472 小时前
每日一题 力扣 3655. 区间乘法查询后的异或 II 模拟 分治 乘法差分法 快速幂 C++ 题解
c++·算法·leetcode·模拟·快速幂·分治·差分法
夏乌_Wx2 小时前
剑指offer | 2.4数据结构相关题目
数据结构·c++·算法·剑指offer·c/c++
米啦啦.2 小时前
C+类的友元与静态成员函数,类模板
c++·友元·类模板
超绝振刀怪2 小时前
【C++可变模板参数】
开发语言·c++·可变模板参数
minji...3 小时前
Linux 线程同步与互斥(二) 线程同步,条件变量,pthread_cond_init/wait/signal/broadcast
linux·运维·开发语言·jvm·数据结构·c++