UVa1596 - Bug Hunt

#include<iostream>
#include<string>
#include<sstream>
#include<map>
using namespace std;
string s0, s, s1, s2, left_value,right_value;
int bug;
map<string, unsigned int> a;
map<string, string> value;
string get_value(string x, string arr){
    unsigned int t = 0;
    string y, z;
    if(x.find("[") == string::npos){
        stringstream ss(x);
        ss >> t;
        if(arr != s0 && t >= a[arr]) bug = 1;
        return x;
    }
    y = x.substr(0, x.find("["));
    z = x.substr(x.find("[")+1, x.find_last_of("]") - 2);
    z = get_value(z, y);
    if(bug) return s0;
    x = y + "[" + z + "]";
    if( !value.count(x) ) bug = 1;
    return value[x];
}
void get_arr()
{
    string x, y;
    unsigned int z;
    x = s.substr(0, s.find("["));
    y = s.substr(s.find("[")+1, s.find_last_of("]"));
    y = get_value(y, s0);
    stringstream ss(y);
    ss >> z;
    a[x] = z;
    return;

}
int main()
{
    int flag = 0, cnt = 1;
    a[s0] = 0;
    string arr, indx;
    while( cin >> s ){
        if(s != ".") flag = 1;
        else if(!flag) break;
        else {
            if(!bug) cout << "0" << endl;
            a.clear();
            value.clear();
            left_value = right_value = s0;
            cnt = 1;
            bug = flag = 0;
            continue;
        }
        if(bug) continue;
        if(s.find("=") == string::npos) get_arr();
        else{
            s1 = s.substr(0, s.find("="));
            s2 = s.substr(s.find("=")+1);
            arr = s1.substr(0, s1.find("["));
            indx = s1.substr(s1.find("[")+1, s1.find_last_of("]") - 2);
            indx = get_value(indx, arr);
            stringstream ss(indx);
            unsigned int t;
            ss >> t;
            if(t >= a[arr]) bug = 1;
            left_value = arr + "[" + indx + "]";
            right_value = get_value(s2, s0);
            if(bug){
                cout << cnt << endl;
                continue;
            }
            value[left_value] = right_value;
            left_value = right_value = s0;
        }
        cnt++;
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值