ZOJ 3826 Hierarchical Notation(2014 Asia Mudanjiang Regional Contest)

题目链接

做了4个小时的大模拟,MLE了,又做了2个小时优化之后TLE了。。。实在不想说什么了

//Hierarchical Notation.cpp
#include 
   
   
    
    
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define DEBUG puts("Here is a BUG");
#define CLEAR(name, init) memset(name, init, sizeof(name))
const double eps = 1e-8;
const int MAXN = (int)4e5 + 5;
using namespace std;

int res[1005];
char part[MAXN];
char notation[MAXN];
map
    
    
     
      > pos;

void out(int x) {
    int brackets_left = 0;
    while(notation[x] && ((notation[x] != ',' && notation[x] != '}') || brackets_left)) {
        brackets_left += notation[x] == '{';
        brackets_left -= notation[x] == '}';
        putchar(notation[x++]);
    }
    puts("");
}
void slove(void) {
    int k = 0;
    CLEAR(part, 0);
    part[k] = '.';
    for(int i = 0; notation[i]; i++) {
        if (notation[i] == '{') {
            while(k > 0 && part[k-1] != '|') part[k--] = '\0';
            if (k) k--;
            part[k++] = '.';
            part[k] = '\0';
        }
        else if (notation[i] == '}') {
            while(k > 0 && part[k-1] != '.') part[k--] = '\0';
            part[k--] = '\0';
            while(k > 0 && part[k-1] != '.') part[k--] = '\0';
            part[k] = '\0';
        }
        else if (notation[i] == ':') {
            if (pos.find(part) != pos.end()) {
                for(int j = 0; j < pos[part].size(); j++) {
                    res[pos[part][j]] = i+1;
                }
            }
            part[k++] = '|';
            part[k] = '\0';
        }
        else if (notation[i] == ',') {
            while(k > 0 && part[k-1] != '.') part[k--] = '\0';
        }
        else {
            part[k++] = notation[i];
            part[k] = '\0';
        }
        // puts(part);
    }
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
    int T; cin >> T;
    while(T--) {
        CLEAR(res, -1);
        pos.clear();
        cin >> notation;
        int n; cin >> n;
        for(int i = 0; i < n; i++) {
            char tmp[MAXN]; tmp[0] = '.';
            scanf("%s", tmp+1);
            pos[tmp].push_back(i + 1);
        }
        slove();
        for(int i = 1; i <= n; i++) {
            if (res[i] == -1) puts("Error!");
            else out(res[i]);
        }
    }
    return 0;
}

    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值