Test instructions: Given two operations, one is to define an array, and the other is to assign a value, so that you find out which step is wrong, there are only two errors, one is the array out of bounds, and the other is to access undefined variables.
Analysis: Originally saw this problem, feel good trouble ah, and then put the past, and now want to come back to do, feel bad to do, did 1 hours ....
Now analysis is the idea, I think I think of more trouble, I first defined two map, respectively, the maximum length and array of arrays, assignment, and then use the vector to save all operations,
In the definition is very simple, directly assign the length to map on the line, the trouble is in this assignment, the first is to divide the two sides of the equal sign, first calculate the right side of the equal sign operation, mainly to observe there is no error in the place,
Special note is that some variables may not be defined, so it is wrong, the first time did not consider WA, both sides to consider, finally to the right to the left, and finally pay attention to the left may not be defined,
Must be a special sentence.
The code is as follows:
#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;//equal position return 0;} int cal (const string &s) {int ans = 0; for (int i = 2; I < s.size (); ++i)//Calculate array length if (s[i]! = '] ') ans = ans * + 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]);//Put the array in the else if (' [' = = SS [i]) t = 0; else if (IsDigit (ss[i])) T = T * + ss[i]-' 0 ';//calculate subscript else if ('] ' = = Ss[i]) {Char ch = character.top ( ); Character.pop (); if (!Maxlength.count (CH)) return false;//This array does not exist if (Maxlength[ch] <= t) return false;//array out of bounds stringst Ream SS; SS << t;//int type to string string tt; SS >> TT; string S1; S1 = ch + tt; if (!mp.count (S1)) return false;//variable not defined 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 * + 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);//computes the position of the equal sign if (t) {string S1 = s.substr (0, T);//equals left string s2 = S.substr (t+1, String::npos);//Equals Right if (!solve (S1, S2)) {OK = true; printf ("%d\n", i+1); Break }//error} else maxlength[s[0]] = cal (s);//define Array} if (!ok) {printf ("0\n"); }} return 0;}
UVa 1596 Bug Hunt (STL stack)