ChampionCyan @ 2024-04-05 15:00:22
题目
#include <unordered_map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
typedef vector < int > vi;
unordered_map < string, vi > mem;
void end();
int digit(string);
string inside(string);
string name(string);
int value(string);
int main() {
string a, b;
while (cin >> a >> b) {
if (a == "int") {
vi m;
int len = value(inside(b));
for (int i = 1; i <= len; i++)
m.push_back(0);
mem.insert(make_pair(name(b), m));
} else if (a == "cout")
printf("%d\n", value(b));
else {
if (value(inside(a)) >= mem[name(a)].size())
end();
vi v = mem[name(a)];
int vb = value(b);
string va = name(a);
mem.erase(va);
v[value(inside(a))] = vb;
mem.insert(make_pair(va, v));
}
}
exit(0);
}
void end() {
puts("-1");
exit(0);
}
int digit(string s) {
int a = (int)s.length(), ret = 0;
for (int i = 0; i < a; i++)
if (s[i] < '0' || s[i] > '9')
return -1;
else
ret = ret * 10 + s[i] - '0';
return ret;
}
string inside(string s) {
int l = 0, r = int(s.length()) - 2;
string ss = "";
while (s[l] != '[')
l++;
while (l < r)
ss += s[++l];
return ss;
}
string name(string s) {
int i = 0;
string ss = "";
while (s[i] != '[')
ss += s[i++];
return ss;
}
int value(string s) {
if (digit(s) >= 0)
return digit(s);
int v = value(inside(s));
if (mem[name(s)].size() <= v)
end();
return v;
}
by MOSS_550W @ 2024-04-05 15:50:53
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
char s[100][100],a[100],b[100],t[100];
int l[100],p[100][100],y,w,ww,h,hh;
int ys(string r,int x,int yy){
if (r[x]>='0' && r[x]<='9')
{
int qq=0;
for (int i=x;i<=yy;i++)
qq=qq*10+r[i]-'0';
return qq;
}
int ce,e;
char m[100];
for (int i=0;i<100;i++)
m[i]='\0';
for (int i=x;r[i]!='[';i++,ce=i)
m[i-x]=r[i];
for (int i=0;i<y;i++)
if (!strcmp(m,s[i])){e=i;break;}
int j=ys(r,ce+1,yy-1);
if (j==-1 || j>=l[e])return -1;
return p[e][j];
}
int main()
{
while(scanf("%s%s",a,b)==2)
{
if (!strcmp(a,"int"))
{
for (int i=0;b[i]!='[';i++,w=i)
s[y][i]=b[i];h=ys(b,w+1,strlen(b)-2);
if (h==-1){printf("-1");return 0;}
l[y++]=h;
}
else if (!strcmp(a,"cout"))
{
h=ys(b,0,strlen(b)-1);
if (h==-1){printf("-1");return 0;}
printf("%d\n",h);
}
else
{
for (int i=0;i<100;i++)
t[i]='\0';
for (int i=0;a[i]!='[';i++,w=i)
t[i]=a[i];
for (int i=0;i<y;i++)
if (!strcmp(t,s[i])){ww=i;break;}
h=ys(a,w+1,strlen(a)-2);
if (h==-1 || h>=l[ww]){printf("-1");return 0;}
hh=ys(b,0,strlen(b)-1);
if (hh==-1){printf("-1");return 0;}
p[ww][h]=hh;
}
}
return 0;
}
by MOSS_550W @ 2024-04-05 15:52:10
这个如果出错了我关注你
by ChampionCyan @ 2024-04-05 16:10:39
此贴结,以后的任何答案悬赏作废,value函数的return v
应该为return mem[name(s)][v]
by ChampionCyan @ 2024-04-06 20:24:07
已解决,此贴结(自己调的),以后的答案均无效(悬赏无效)
by ChampionCyan @ 2024-04-06 20:25:47
@MOSS_550W
你的解法是对的,但我自己已经调出来了,但关注照给