题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量。
析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回来做一下,感觉也不好做,做了1个多小时。。。。。
现在分析一下是思路,我觉得我想的比较麻烦,我首先定义了两个map,分别是数组的最大长度和数组的,赋值情况,然后用向量把所有操作存起来,
在定义时很简单,直接把长度赋给map就行,麻烦就是在这个赋值时,首先是把等号两边的分开,先计算等号右边的操作,主要是观察有没有出错的地方,
特别注意的是有的变量可能没定义,这样就错了,第一次没考虑WA了,两边都要考虑,最后要把右边赋值给左边,最后还要注意左边可能没有定义,
一定要特判一下。
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <stack>
#include <string>
#include <queue>
#include <vector>
#include <cctype>
#include <sstream>
using namespace std;
map<string, int> mp;
map<char, int> maxlength;
vector<string> v;
int contain(const string &s){
for(int i = 0; i < s.size(); ++i)
if('=' == s[i]) return i;//等号位置
return 0;
}
int cal(const string &s){
int ans = 0;
for(int i = 2; i < s.size(); ++i)//计算数组长度
if(s[i] != ']') ans = ans * 10 + s[i] - '0';
return ans;
}
bool solve(string s, string ss){
stack<char> character;
int t = 0;
for(int i = 0; i < ss.size(); ++i){
if(isalpha(ss[i])) character.push(ss[i]);//把数组放进去
else if('[' == ss[i]) t = 0;
else if(isdigit(ss[i])) t = t * 10 + ss[i] - '0';//计算下标
else if(']' == ss[i]){
char ch = character.top(); character.pop();
if(!maxlength.count(ch)) return false;//这个数组不存在
if(maxlength[ch] <= t) return false;//数组越界
stringstream ss; ss << t;//把int型转string
string tt; ss >> tt;
string s1; s1 = ch + tt;
if(!mp.count(s1)) return false;//变量没定义
t = mp[s1];
}
}
int ans = t; t = 0;
while(!character.empty()) character.pop();
for(int i = 0; i < s.size(); ++i){
if(isalpha(s[i])) character.push(s[i]);
else if('[' == s[i]) t = 0;
else if(isdigit(s[i])) t = t * 10 + s[i] - '0';
else if(']' == s[i]){
char ch = character.top(); character.pop();
stringstream ss; ss << t;
string tt; ss >> tt;
string s1; s1 = ch + tt;
if(!maxlength.count(ch)) return false;
if(maxlength[ch] <= t) return false;
if(i == s.size()-1){ if(!maxlength.count(s1[0])) return false; mp[s1] = ans; break; }
if(!mp.count(s1)) return false;
t = mp[s1];
}
}
return true;
}
int main(){
// freopen("in.txt", "r", stdin);
string s;
while(cin >> s && s != "."){
mp.clear();
maxlength.clear();
v.clear();
v.push_back(s);
while(cin >> s && s != ".") v.push_back(s);
bool ok = false;
for(int i = 0; i < v.size(); ++i){
s = v[i];
int t = contain(s);//计算等号的位置
if(t){
string s1 = s.substr(0, t);//等号左边
string s2 = s.substr(t+1, string::npos);//等号右边
if(!solve(s1, s2)){ ok = true; printf("%d\n", i+1); break; }//出错了
}
else maxlength[s[0]] = cal(s);//定义数组
}
if(!ok){ printf("0\n"); }
}
return 0;
}
浙公网安备 33010602011771号