夏夜、

心若平似镜、何题不AC。

URAL 1992 CVS 可持久化链栈

http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html

看这篇的链表部分的介绍应该就能理解“可持久化”了

动态分配内存的会T,只能用静态

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;

struct node {
    int p;
    int next;
};
const int maxn = 500005;
node nd[maxn * 2];
int ha[maxn], hb[maxn];
int c;
int cnt;
void add(int &head, int p) {
    nd[cnt].p = p;
    nd[cnt].next = head;
    head = cnt++;
}
int main() {
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF) {
        c = 1;
        cnt = 0;
        ha[c] = -1; hb[c] = -1;
        for(int i = 1; i <= n; i++) {
            char op[20]; scanf("%s", op);
            if(strcmp(op, "learn") == 0) {
                int id, p;
                scanf("%d%d", &id, &p);
                add(ha[id], p);
                hb[id] = -1;
            } else if(strcmp(op, "rollback") == 0) {
                int id;
                scanf("%d", &id);
                add(hb[id], nd[ha[id]].p);
                ha[id] = nd[ha[id]].next;
            } else if(strcmp(op, "clone") == 0) {
                int id;
                scanf("%d", &id);
                c++;
                ha[c] = ha[id];
                hb[c] = hb[id];
            } else if(strcmp(op, "check") == 0) {
                int id;
                scanf("%d", &id);
                if(ha[id] == -1) {
                    printf("basic\n");
                } else printf("%d\n", nd[ha[id]].p);
            } else if(strcmp(op, "relearn") == 0) {
                int id;
                scanf("%d", &id);
                add(ha[id], nd[hb[id]].p);
                hb[id] = nd[hb[id]].next;
            }
        }
        return 0;
    }
    return 0;
}
View Code

 

posted on 2014-07-29 10:17  BMan、  阅读(376)  评论(0编辑  收藏  举报

导航