HDU 4594 Script Z

这是一道让人困扰的模拟题,难度在于某些奇怪的测试用例。通过大神代码理解并AC。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一道恶心的模拟,写了一整晚上也不知道哪里错,借助大神的代码AC了

这是Hint :The problem itself is easy of course. But some of the test cases of this problem are strange and annoying. Be careful!

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<sstream>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
int dx[]= {0,1,1,1,0,-1,-1,-1};
int dy[]= {1,1,0,-1,-1,-1,0,1};
int gcd (const LL &a, const LL &b) {return b==0?a:gcd (b,a%b);}
LL LLpow (LL a, LL b) {
    if (b==0) {return 1;}
    LL t=LLpow (a,b/2);
    if (b%2) {return t*t*a;}
    return t*t;
}
using namespace std;
int T,t,n,m;
map<string,string> match[4];
char str[1010],str2[1010],s1[1010],s2[1010];
int type1,type2,Err,Over;
bool Has_Equal() {
    int i,len=strlen (str);
    bool flag=0,flag2=0;
    for (i=0; i<len; i++) {
        if (str[i]==34) {flag2=1;}
        if (str[i]=='=' && flag2==0) {
            flag=true;
            str[i]=' ';
        }
        if (str[i]==9) {str[i]=32;}
    }
    return flag;
}
bool kong() {
    int i,len=strlen (str);
    for (i=0; i<len; i++)
        if (! (str[i]==' ' || str[i]==9) ) {
            return false;
        }
    return true;
}
void solve() {
    int i,len=strlen (str);
    m=0;
    for (i=0; i<len; i++)
        if (str[i]==34) {break;}
    m=i;
    for (i=m; i<len; i++)
        if (! (str[i]==' ' || str[i]==9) ) {str[m++]=str[i];}
    str[m]='\0';
}
int main() {
    string string1,string2;
    scanf ("%d",&T);
    for (t=1; t<=T; t++) {
        if (t!=1) {puts ("");}
        scanf ("%d",&n); getchar();
        for (int i=0; i<=3; i++) {
            match[i].clear();
        }
        Err=1; Over=0;/**Init*/
        while (n--) {
            gets (str);
            solve();
            if (Over==1) {continue;}
            if (kong() ) {continue;}
            if (Has_Equal() ) {
                sscanf (str,"%s %s",s1,s2);
                if (s1[0]=='$') {type1=0;}
                else {type1=1;}
                if (s2[0]!=34) {type2=0;}
                else {type2=1;}
                if (type1==0) {
                    match[0].erase (s1);
                    match[1].erase (s1);
                    match[type2][s1]=s2;
                } else {
                    if (match[2].find (s1) ==match[2].end() && match[3].find (s1) ==match[3].end() ) {
                        match[2+type2][s1]=s2;
                    } else if (Err==1) {
                        printf ("WARNING: Constant %s Already Defined!\n",s1);
                    }
                }
            } else {
                sscanf (str,"%s %s",s1,s2);
                if (s1[0]=='P' && s1[1]=='a') { //Panic
                    printf ("Script was KILLED.\n");
                    Over=1;
                } else if (s1[0]=='E' && s2[1]=='N') { //Errmsg ON
                    Err=1;
                } else if (s1[0]=='E' && s2[1]=='F') { //Errmsg OFF
                    Err=0;
                } else if (s1[0]=='P') { //Print
                    if (s2[0]=='$') {
                        if (match[0].find (s2) !=match[0].end() ) {
                            string1=match[0][s2];
                            cout<<string1<<endl;
                        } else if (match[1].find (s2) !=match[1].end() ) {
                            string1=match[1][s2];
                            string2=string1.substr (1,string1.length()-2);
                            cout<<string2<<endl;
                        } else {
                            printf ("NULL\n");
                            if (Err==1) {
                                printf ("NOTICE: Undefined Variable %s.\n",s2);
                            }
                        }
                    } else {
                        if (match[2].find (s2) !=match[2].end() ) {
                            string1=match[2][s2];
                            cout<<string1<<endl;
                        } else if (match[3].find (s2) !=match[3].end() ) {
                            string1=match[3][s2];
                            string2=string1.substr (1,string1.length()-2);
                            cout<<string2<<endl;
                        } else {
                            printf ("%s\n",s2);
                            if (Err==1) {
                                printf ("NOTICE: Undefined Constant %s.\n",s2);
                            }
                        }
                    }
                } else if (s1[0]=='D') { //Dump
                    if (s2[0]=='$') {
                        if (match[0].find (s2) !=match[0].end() ) {
                            string1=match[0][s2];
                            printf ("integer: ");
                            cout<<string1<<endl;
                        } else if (match[1].find (s2) !=match[1].end() ) {
                            string1=match[1][s2];
                            printf ("string: ");
                            cout<<string1<<endl;
                        } else {
                            printf ("NULL\n");
                            if (Err==1) {
                                printf ("NOTICE: Undefined Variable %s.\n",s2);
                            }
                        }
                    } else {
                        if (match[2].find (s2) !=match[2].end() ) {
                            string1=match[2][s2];
                            printf ("integer: ");
                            cout<<string1<<endl;
                        } else if (match[3].find (s2) !=match[3].end() ) {
                            string1=match[3][s2];
                            printf ("string: ");
                            cout<<string1<<endl;
                        } else {
                            printf ("string: %c%s%c\n",34,s2,34);
                            if (Err==1) {
                                printf ("NOTICE: Undefined Constant %s.\n",s2);
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值