In this problem, we consider a simple programming language that has only declarations of onedimensional integer arrays and assignment statements. The problem is to find a bug in the given program.
The syntax of this language is given in BNF as follows:
⟨program⟩ ::= ⟨declaration⟩|⟨program⟩⟨declaration⟩|⟨program⟩⟨assignment⟩
⟨declaration⟩ ::= ⟨array name⟩ [⟨number⟩]⟨new line⟩
⟨assignment⟩ ::= ⟨array name⟩ [⟨expression⟩]= ⟨expression⟩⟨newline⟩
⟨expression⟩ ::= ⟨number⟩|⟨array name⟩ [⟨expression⟩]
⟨number⟩ ::= ⟨digit⟩|⟨digit positive⟩⟨digit string⟩
⟨digit string⟩ ::= ⟨digit⟩|⟨digit⟩⟨digit string⟩
⟨digit positive⟩ ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
⟨digit⟩ ::= 0 | ⟨digit positive⟩
⟨array name⟩ ::= a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
where ⟨new line⟩ denotes a new line character (LF).
Characters used in a program are alphabetical letters, decimal digits, =, [, ] and new line characters. No other characters appear in a program.
A declaration declares an array and specifies its length. Valid indices of an array of length n are integers between 0 and n − 1, inclusive. Note that the array names are case sensitive, i.e. array a and array A are different arrays. The initial value of each element in the declared array is undefined.
For example, array a of length 10 and array b of length 5 are declared respectively as follows.
a[10]
b[5]
An expression evaluates to a non-negative integer. A ⟨number⟩ is interpreted as a decimal integer. An ⟨array name⟩ [⟨expression⟩] evaluates to the value of the ⟨expression⟩-th element of the array. An assignment assigns the value denoted by the right hand side to the array element specified by the left hand side.
Examples of assignments are as follows.
a[0]=3
a[1]=0
a[2]=a[a[1]]
a[a[0]]=a[1]
A program is executed from the first line, line by line. You can assume that an array is declared once and only once before any of its element is assigned or referred to.
Given a program, you are requested to find the following bugs.
• An index of an array is invalid.
• An array element that has not been assigned before is referred to in an assignment as an index of array or as the value to be assigned.
You can assume that other bugs, such as syntax errors, do not appear. You can also assume that integers represented by ⟨number⟩s are between 0 and 231 − 1 (= 2147483647), inclusive.
Input
The input consists of multiple datasets followed by a line which contains only a single ‘.’ (period). Each dataset consists of a program also followed by a line which contains only a single ‘.’ (period). A program does not exceed 1000 lines. Any line does not exceed 80 characters excluding a new line character.
Output
For each program in the input, you should answer the line number of the assignment in which the first bug appears. The line numbers start with 1 for each program. If the program does not have a bug, you should answer zero. The output should not contain extra characters such as spaces.
Sample Input
a[3]
a[0]=a[1]
.
x[1]
x[0]=x[0]
.
a[0]
a[0]=1
.
b[2]
b[0]=2
b[1]=b[b[0]]
b[0]=b[1]
.
g[2]
G[10]
g[0]=0
g[1]=G[0]
.
a[2147483647]
a[0]=1
B[2]
B[a[0]]=2
a[B[a[0]]]=3
a[2147483646]=a[2]
.
.
Sample Output
2
2
2
3
4
0
题目类型:简单字符处理+栈模拟
算法分析:由于表达式可能存在嵌套的情况,所以需要使用栈来模拟计算。对于每个赋值语句'='左右两边的表达式都要分别计算并需要考虑不同的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
/************************************************** filename :i.cpp author :maksyuki created time :2018/1/24 9:34:10 last modified :2018/1/24 11:55:58 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 666; map<int, int> ma[maxn], mb; void init() { for(int i = 0; i < maxn; i++) ma[i].clear(); mb.clear(); } PII getKey(const string &s, int pp, bool ff) { stack<char> sta; int len = s.size(), vv = 0, cc = 0; int cnt = 0; bool is_valid = true; for(int i = pp; i < len; i++) { if(s[i] == '=') break; if(s[i] == ']') { int tt = 1; vv = cc = 0; while(sta.top() != '[') { vv += tt * (sta.top() - '0'); tt *= 10; sta.pop(); } sta.pop(); cc = sta.top(); sta.pop(); if(!ff) { if(ma[cc].count(vv)) { int ttv = ma[cc][vv]; //DB(ttv); cnt--; vector<char> ans; do { ans.emplace_back((char) (ttv % 10 + '0')); ttv /= 10; }while(ttv); for(int i = ans.size() - 1; i >= 0; i--) sta.push(ans[i]); } else { is_valid = false; break; } } } else { sta.push(s[i]); if(isalpha(s[i])) cnt++; } } if(is_valid || (!is_valid && cnt == 1)) return PII(cc, vv); return PII(0, -1); } int getValue(const string &s) { int len = s.size(), pos = -1; for(int i = 0; i < len; i++) if(s[i] == '=') { pos = i; break; } if(s[pos+1] >= '0' && s[pos+1] <= '9') { int tv = 0; for(int i = pos + 1; i < len; i++) { tv = tv * 10 + (s[i] - '0'); } return tv; } else { PII vvv = getKey(s, pos + 1, 0); if(ma[vvv.first].count(vvv.second)) return ma[vvv.first][vvv.second]; return -1; } } int main() { #ifdef LOCAL CFF; //CFO; #endif string s; while(cin >> s) { if(s == ".") break; init(); int pos = 0, id = 1; do { if(s == ".") break; if(s.find('=') == string::npos) { PII vv = getKey(s, 0, 1); //DB(vv.first), DB(vv.second); if(vv.second != -1) mb[vv.first] = vv.second; } else { PII va = getKey(s, 0, 0); int vb = getValue(s); //DB(va.first), DB(va.second), DB(mb[va.first]); //DB(vb); if(va.second >= 0 && va.second <= mb[va.first] - 1 && vb != -1) ma[va.first][va.second] = vb; else { if(!pos) pos = id; } } id++; }while(cin >> s); cout << pos << endl; } return 0; } |
- « 上一篇:uva230
- uva1597:下一篇 »