QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#783677 | #9551. The Emperor | UESTC_OldEastWest | WA | 0ms | 4168kb | C++20 | 2.5kb | 2024-11-26 11:20:18 | 2024-11-26 11:20:19 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define maxn 2005
using namespace std;
const ll mod=998244353;
struct intruction
{
int op;
int x1,y1;
int x2,y2;
int jmp;
ll val;
intruction()
{
op=x1=y1=x2=y2=jmp=0;
val=0;
}
}ins[maxn];
int n;
int vis[maxn];
pair<int,ll>stk[maxn];
void print(int m)
{
printf("op=%d ",ins[m].op);
if(ins[m].op==1)
printf("x1=%d y1=%d x2=%d y2=%d\n",ins[m].x1,ins[m].y1,ins[m].x2,ins[m].y2);
else
printf("x2=%d y2=%d\n",ins[m].x2,ins[m].y2);
}
std::string getword(std::string s,int n,int &p)
{
std::string ret;
while(p<n&&(s[p]<'A'||s[p]>'Z')&&(s[p]<'0'||s[p]>'9'))
p++;
while(p<n&&s[p]!=' '&&s[p]!=';'&&s[p]!='\n'&&s[p]!=EOF)
ret.push_back(s[p++]);
p++;
return ret;
}
void read(int m)
{
int num=0;
std::string s,t;
std::getline(cin,s);
while(s[0]<'A'||s[0]>'Z')
std::getline(cin,s);
int n=s.size(),p=0;
while(p<n)
{
num++;
t=getword(s,n,p);
if(num==1)
{
if(t[0]=='P')
ins[m].op=1;
else ins[m].op=2;
}
else if(ins[m].op==1)
{
if(num==2)
ins[m].x1=stoi(t);
if(num==4)
ins[m].y1=stoi(t);
if(num==6)
ins[m].x2=stoi(t);
if(num==8)
ins[m].y2=stoi(t);
}
else if(ins[m].op==2)
{
if(num==3)
ins[m].x2=stoi(t);
if(num==5)
ins[m].y2=stoi(t);
}
}
}
ll Push(int &top,int &p)
{
if(vis[p])
{
printf("-1");
exit(0);
}
ll ret=0;
if(ins[p].jmp)
{
if(!top)
ret=ins[p].val;
else
stk[top].second=(stk[top].second+ins[p].val)%mod;
p=ins[p].jmp;
}
else
{
vis[p]=1;
stk[++top]={p,1};
p=ins[p].y2;
}
return ret;
}
ll Pop(int &top,int &p)
{
vis[p]=0;
ll ret=0;
ins[stk[top].first].jmp=p=ins[p].y1;
ins[stk[top].first].val=ret=(stk[top].second+1)%mod;
top--;
if(top)
{
stk[top].second=(stk[top].second+ret)%mod;
ret=0;
}
return ret;
}
void solve()
{
int top=0,p=1,lst[2]={0,0};
ll ans=0;
while(1)
{
if(p==lst[0]&&p==lst[1])
{
printf("-1");
return;
}
else
{
lst[1]=lst[0];
lst[0]=p;
}
switch (ins[p].op)
{
case 1:
{
if(!top||ins[stk[top].first].x2!=ins[p].x1)
ans=(ans+Push(top,p))%mod;
else//pop
ans=(ans+Pop(top,p))%mod;
break;
}
case 2:
{
if(!top)
{
ans++;
printf("%lld",ans);
return;
}
else
ans=(ans+Push(top,p))%mod;
break;
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
read(i);
// for(int i=1;i<=n;++i)
// print(i);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4168kb
input:
1 HALT; PUSH 1 GOTO 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 4164kb
input:
5 POP 1 GOTO 2; PUSH 1 GOTO 2 HALT; PUSH 1 GOTO 3 POP 1 GOTO 4; PUSH 2 GOTO 4 POP 1 GOTO 2; PUSH 2 GOTO 4 HALT; PUSH 99 GOTO 4
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3908kb
input:
1 POP 1 GOTO 1; PUSH 1 GOTO 1
output:
-1
result:
ok 1 number(s): "-1"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3872kb
input:
61 POP 62 GOTO 61; PUSH 30 GOTO 60 POP 1 GOTO 3; PUSH 62 GOTO 61 POP 2 GOTO 61; PUSH 62 GOTO 61 POP 4 GOTO 7; PUSH 2 GOTO 61 POP 62 GOTO 61; PUSH 3 GOTO 4 POP 62 GOTO 61; PUSH 3 GOTO 5 POP 5 GOTO 10; PUSH 3 GOTO 6 POP 62 GOTO 61; PUSH 4 GOTO 7 POP 62 GOTO 61; PUSH 4 GOTO 8 POP 6 GOTO 12; PUSH 4 GOTO...
output:
-1
result:
wrong answer 1st numbers differ - expected: '150994941', found: '-1'