QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#791397 | #9551. The Emperor | ucup-team5217# | WA | 0ms | 3844kb | C++23 | 3.3kb | 2024-11-28 18:24:57 | 2024-11-28 18:24:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll =long long;
#define int long long
int fa[2010];
int siz[2010];
const ll M=998244353;
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++) {fa[i]=0;siz[i]=0;}
vector<string> s(n+1);
vector<bool> ex(n+1);
for(int i=0;i<=n;i++) getline(cin, s[i]);
vector<array<int,4>> mp(n+1);
auto getf=[&](string & s,int pz)->pair<int,int> {
int ans=0;
while(pz<(int)s.size()){
if(s[pz]<='9'&&s[pz]>='0') ans=ans*10+s[pz++]-'0';
else break;
}
return {ans,pz};
};
for(int i=1;i<=n;i++){
if(s[i].substr(0,4)=="HALT"){
pair<int,int> w1=getf(s[i],11);
pair<int,int> w2=getf(s[i],w1.second+6);
mp[i]={-1,-1,w1.first,w2.first};
}
else{
pair<int,int> w1=getf(s[i],4);
pair<int,int> w2=getf(s[i],w1.second+6);
pair<int,int> w3=getf(s[i],w2.second+7);
pair<int,int> w4=getf(s[i],w3.second+6);
mp[i]={w1.first,w2.first,w3.first,w4.first};
}
}
vector<array<int,3>> stk;//num,ope,pz
int pz=1;
vector<bool> flag(n+1);
int pre=-1;
ll ans=1;int tot=0;
while (tot<=20){
tot++;
// cerr<<"???"<<tot++<<'\n';
// cerr<<"___\n";
// cerr<<pz<<'\n';
// for(auto [x,y,z]:stk){
// cerr<<x<<' '<<y<<' '<<z<<'\n';
// }
// for(int j=1;j<=n;j++){
// cerr<<fa[j]<<' ';
// }
// cerr<<"___\n";
// cerr<<'\n';
if(flag[pz]) {cout<<"-1\n";return ;}
if(stk.empty()&&mp[pz][0]==-1){
cout<<(ans%M+M)%M<<'\n';
return ;
}
if(!stk.empty()&&stk.back()[0]==mp[pz][0]){
ans=(ans+1)%M;
if(mp[pz][1]==stk.back()[2]){
cout<<"-1\n";return ;
}
if(fa[stk.back()[2]]==0){
fa[stk.back()[2]]=mp[pz][1];
siz[stk.back()[2]]=(ans-mp[pz][1])%M;
}
flag[stk.back()[2]]=0;
stk.pop_back();
pz=mp[pz][1];
continue;
}
int pre=pz;
while(fa[pz]){
ans=(ans+siz[pz])%M;
pz=fa[pz];
if(pz==pre){
cout<<"-1\n";
return ;
}
if(flag[pz]) {cout<<"-1\n";return ;}
if(stk.empty()&&mp[pz][0]==-1){
cout<<(ans%M+M)%M<<'\n';
return ;
}
if(!stk.empty()&&stk.back()[2]==mp[pz][0]){
ans=(ans+1)%M;
if(fa[stk.back()[2]]==0){
fa[stk.back()[2]]=mp[pz][1];
siz[stk.back()[2]]=ans-mp[pz][1];
if(fa[stk.back()[2]]==stk.back()[2]){
cout<<"-1\n";return ;
}
}
pz=mp[pz][3];
stk.pop_back();
continue;
}
else continue;
}
ans=(ans+1)%M;
stk.push_back({mp[pz][2],ans,pz});
flag[pz]=1;
pz=mp[pz][3];
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0);
int _ =1;
// cin>>_;
while(_--) solve();
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3496kb
input:
1 HALT; PUSH 1 GOTO 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3584kb
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: 3844kb
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: 3504kb
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:
result:
wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements