标签:namespace ack assign var 解析失败 clear second ble getline
题目链接:https://vjudge.net/problem/UVA-1596
题目翻译摘自《算法禁赛入门经典》
输入并模拟执行一段程序,输出第一个bug所在的行。每行程序有两种可能:
赋值语句可能会出现两种bug:
程序不超过1000行,每行不超过80个字符且所有常数均为小于 231 的非负整数。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 5 #define Rep(i,n) for (int i = 0; i < (n); ++i) 6 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 12 13 #define pr(x) cout << #x << " = " << x << " " 14 #define prln(x) cout << #x << " = " << x << endl 15 16 #define LOWBIT(x) ((x)&(-x)) 17 18 #define ALL(x) x.begin(),x.end() 19 #define INS(x) inserter(x,x.begin()) 20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end()) 21 22 #define ms0(a) memset(a,0,sizeof(a)) 23 #define msI(a) memset(a,inf,sizeof(a)) 24 #define msM(a) memset(a,-1,sizeof(a)) 25 26 #define MP make_pair 27 #define PB push_back 28 #define ft first 29 #define sd second 30 31 template<typename T1, typename T2> 32 istream &operator>>(istream &in, pair<T1, T2> &p) { 33 in >> p.first >> p.second; 34 return in; 35 } 36 37 template<typename T> 38 istream &operator>>(istream &in, vector<T> &v) { 39 for (auto &x: v) 40 in >> x; 41 return in; 42 } 43 44 template<typename T1, typename T2> 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 46 out << "[" << p.first << ", " << p.second << "]" << "\n"; 47 return out; 48 } 49 50 inline int gc(){ 51 static const int BUF = 1e7; 52 static char buf[BUF], *bg = buf + BUF, *ed = bg; 53 54 if(bg == ed) fread(bg = buf, 1, BUF, stdin); 55 return *bg++; 56 } 57 58 inline int ri(){ 59 int x = 0, f = 1, c = gc(); 60 for(; c<48||c>57; f = c==‘-‘?-1:f, c=gc()); 61 for(; c>47&&c<58; x = x*10 + c - 48, c=gc()); 62 return x*f; 63 } 64 65 template<class T> 66 inline string toString(T x) { 67 ostringstream sout; 68 sout << x; 69 return sout.str(); 70 } 71 72 inline int toInt(string s) { 73 int v; 74 istringstream sin(s); 75 sin >> v; 76 return v; 77 } 78 79 //min <= aim <= max 80 template<typename T> 81 inline bool BETWEEN(const T aim, const T min, const T max) { 82 return min <= aim && aim <= max; 83 } 84 85 typedef long long LL; 86 typedef unsigned long long uLL; 87 typedef pair< double, double > PDD; 88 typedef pair< int, int > PII; 89 typedef pair< int, PII > PIPII; 90 typedef pair< string, int > PSI; 91 typedef pair< int, PSI > PIPSI; 92 typedef set< int > SI; 93 typedef set< PII > SPII; 94 typedef vector< int > VI; 95 typedef vector< double > VD; 96 typedef vector< VI > VVI; 97 typedef vector< SI > VSI; 98 typedef vector< PII > VPII; 99 typedef map< int, int > MII; 100 typedef map< int, string > MIS; 101 typedef map< int, PII > MIPII; 102 typedef map< PII, int > MPIII; 103 typedef map< string, int > MSI; 104 typedef map< string, string > MSS; 105 typedef map< PII, string > MPIIS; 106 typedef map< PII, PII > MPIIPII; 107 typedef multimap< int, int > MMII; 108 typedef multimap< string, int > MMSI; 109 //typedef unordered_map< int, int > uMII; 110 typedef pair< LL, LL > PLL; 111 typedef vector< LL > VL; 112 typedef vector< VL > VVL; 113 typedef priority_queue< int > PQIMax; 114 typedef priority_queue< int, VI, greater< int > > PQIMin; 115 const double EPS = 1e-8; 116 const LL inf = 0x7fffffff; 117 const LL infLL = 0x7fffffffffffffffLL; 118 const LL mod = 1e9 + 7; 119 const int maxN = 1e4 + 7; 120 const LL ONE = 1; 121 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 122 const LL oddBits = 0x5555555555555555; 123 124 struct Var{ 125 string name; 126 int bios; 127 128 bool operator< (const Var &x) const { 129 return name < x.name || name == x.name && bios < x.bios; 130 } 131 }; 132 133 string tmp; 134 MSI arr_len; // 记录数组名和对应长度 135 map< Var, int > vars; // 记录初始化的变量和对应值 136 int ans; 137 138 bool isOut(Var x) { 139 return x.bios >= arr_len[x.name]; 140 } 141 142 bool isAssign(Var x) { 143 return vars.find(x) != vars.end(); 144 } 145 146 // 解析成功返回true,如果有嵌套,那么可能解析失败,此时返回false 147 bool analysis(string s, Var &x) { 148 x.name = s.substr(0, s.find("[")); 149 s = s.substr(s.find("[") + 1); 150 s.pop_back(); 151 152 if(s.back() == ‘]‘) { 153 Var y; 154 if(analysis(s, y) && !isOut(y), isAssign(y)) x.bios = vars[y]; 155 else return false; 156 } 157 else x.bios = toInt(s); 158 return true; 159 } 160 161 int main(){ 162 //freopen("MyOutput.txt","w",stdout); 163 //freopen("input.txt","r",stdin); 164 //INIT(); 165 while(getline(cin, tmp)) { 166 if(tmp == ".") break; 167 arr_len.clear(); 168 vars.clear(); 169 ans = 0; 170 171 do { 172 if(tmp == ".") break; 173 ++ans; 174 Var x, y; 175 176 if(tmp.find("=") == string::npos) { // 数组声明 177 if(analysis(tmp, x)) arr_len[x.name] = x.bios; 178 else break; 179 } 180 else { // 赋值 181 string X = tmp.substr(0, tmp.find("=")); 182 string Y = tmp.substr(tmp.find("=") + 1); 183 184 if(analysis(X, x) && !isOut(x)) { 185 if(isdigit(Y[0])) vars[x] = toInt(Y); 186 else { 187 if(!analysis(Y, y) || isOut(y) || !isAssign(y)) break; 188 vars[x] = vars[y]; 189 } 190 } 191 else break; 192 } 193 }while(getline(cin, tmp)); 194 195 if(tmp == ".") ans = 0; 196 197 do { if(tmp == ".") break; }while(getline(cin, tmp)); // 处理多余的输入 198 199 cout << ans << endl; 200 } 201 return 0; 202 }
标签:namespace ack assign var 解析失败 clear second ble getline
原文地址:https://www.cnblogs.com/zaq19970105/p/11073581.html