【链接】 我是链接,点我呀:)
【题意】
【题解】
map模拟 map【代码】
#include <bits/stdc++.h>
using namespace std;
string s;
map <string, int> mmap;
map <pair<string, int> ,int> mmap2;
int cur = 0;
bool ok;
int gl(int l, int r)
{
return r - l + 1;
}
pair <string, string> cl(string s)
{
string val = "", temp = "";
int now = 0;
while (s[now] != '[') val += s[now++];
int len = s.size();
temp = s.substr(now + 1, gl(now + 1, len - 2));
return make_pair(val, temp);
}
int get_num(string s)
{
if (isdigit(s[0]))
{
int x = 0, lens = s.size();
for (int i = 0; i < lens; i++)
x = x * 10 + s[i] - '0';
return x;
}
string val = "";
int len = s.size(),l;
for (int i = 0; i < len; i++)
{
if (s[i] == '[')
{
l = i;
break;
}
val += s[i];
}
int nex = get_num(s.substr(l + 1, gl(l + 1, len - 2)) );
if (nex >= mmap[val] || nex < 0)
{
ok = false;
return -1;
}
else
{
if (mmap2.find(make_pair(val, nex)) != mmap2.end())
{
return mmap2[make_pair(val, nex)];
}
else
{
ok = false;
return -1;
}
}
}
int main()
{
//freopen("F:\rush.txt", "r", stdin);
while (cin >> s && s[0] != '.')
{
mmap.clear();
mmap2.clear();
ok = true;
cur = 1;
while (s[0] != '.')
{
if (!ok)
{
cin >> s;
continue;
}
int fi = s.find('=', 0);
if (fi != -1)// a=b
{
string s1 = s.substr(0, fi);
string s2 = s.substr(fi + 1);
pair <string, string> temp = cl(s1);
s1 = temp.second;
string a = temp.first;
int x = get_num(s1), y = get_num(s2);
if (!ok || x < 0 || x >= mmap[a])
{
ok = 0;
cout << cur << endl;
continue;
}
mmap2[make_pair(a, x)] = y;
}
else
{
pair <string, string> temp = cl(s);
mmap[temp.first] = get_num(temp.second);
}
cur++;
cin >> s;
}
if (ok) cout << 0 << endl;
}
return 0;
}