Cod sursa(job #2088147)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 14 decembrie 2017 19:58:57
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.94 kb
#include <bits/stdc++.h>
#define MAXN 1000000
#define MOD1 666013
#define BUF_SIZE 1 << 14
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi,*fo;
inline char nextch(){
    if(pbuf==BUF_SIZE){
        fread(buf, BUF_SIZE, 1, fi);
        pbuf=0;
    }
    return buf[pbuf++];
}
inline int nextnum(){
    int a = 0;
    char c = nextch();
    while(!isdigit(c))
        c = nextch();
    while(isdigit(c)){
        a = a * 10 + c - '0';
        c = nextch();
    }
    return a;
}
class Hash{
    public:
    int k = 0, next[MAXN+1], *lista;
    unsigned int val[MAXN+1];
    Hash() {
        lista = new int[MOD1];
        memset(lista, 0x00, MOD1*sizeof(int));
    }
    inline void insert(int ind, int element){
        k++;
        val[k] = element;
        next[k] = lista[ind];
        lista[ind] = k;
    }
    inline int find(int ind, int element){
        int p = lista[ind];
        while(p != 0 && val[p] != element)
            p = next[p];
        if(p != 0)
            return p;
        return 0;
    }
    void clear(){
        k = 0;
        memset(lista, 0x00, MOD1 * sizeof(int));
    }
} H;

unsigned int r1[1 + MAXN];
unsigned int r2[1 + MAXN];
int main(){
    fi=fopen("twosets.in","r");
    fo=fopen("twosets.out","w");

    int t = nextnum();
    for(int z = 1; z <= t; z++){
        int c1 = 0;
        int ind = 0;
        char c = nextch();
        while(!isdigit(c) && c != 'i' && c != 'd' && c != 't') c = nextch();
        while(isdigit(c) || c == 'i' || c == 'd' || c == 't'){
            if(c == 'i'){
                c = nextch();
                c = c - '0';
                ind++;
                r1[ind] = r1[ind - 1] * 2 + c;
                while(r1[ind] >= MOD1)
                    r1[ind] -= MOD1;
                r2[ind] = r2[ind - 1] * 2 + c;
            }
            else if(c == 'd')
                ind--;
            else if(c == 't'){
                H.insert(r1[ind], r2[ind]);
                c1++;
            }
            c = nextch();
        }

        int c2 = 0;
        int ok = 1;
        ind = 0;
        c = nextch();
        while(!isdigit(c) && c != 'i' && c != 'd' && c != 't') c = nextch();
        while(isdigit(c) || c == 'i' || c == 'd' || c == 't'){
            if(c == 'i'){
                c = nextch();
                c = c - '0';
                ind++;
                r1[ind] = r1[ind - 1] * 10 + c;
                while(r1[ind] >= MOD1)
                    r1[ind] -= MOD1;
                r2[ind] = r2[ind - 1] * 10 + c;
            }
            else if(c == 'd')
                ind--;
            else if(c == 't'){
                if(!H.find(r1[ind], r2[ind]))
                    ok = 0;
                c2++;
            }
            c = nextch();
        }
        if(c1 == c2 && ok) fprintf(fo,"1\n");
        else fprintf(fo,"0\n");
        H.clear();
    }

    fclose(fi);
    fclose(fo);
    return 0;
}