Cod sursa(job #1317016)

Utilizator geniucosOncescu Costin geniucos Data 14 ianuarie 2015 14:20:10
Problema Nums Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.46 kb
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>

using namespace std;

int M, Nr, aib[100009], pos[100009], al_k_lea[100009];

string sir[100009];
char actual[100009];

struct nod
{
    int cod;
    vector < pair < nod*, char > > urm;
    nod ()
    {
        cod = 0;
        urm.clear();
    }
}*R[100009];

void dfs (nod *&R, int deep, int L)
{
    if (R == 0) return ;

    if (deep == L)
    {
        Nr ++;
        al_k_lea[Nr] = R->cod;
        pos[R->cod] = Nr;
        return;
    }

    for (vector < pair < nod*, char > > :: iterator it = R->urm.begin(); it != R->urm.end(); it ++)
        dfs (it->first, deep + 1, L);
}

void U (int poz)
{
    while (poz <= M)
    {
        aib[poz] ++;
        poz += (poz - (poz & (poz - 1)));
    }
}

void move (char p, nod *&R)
{
    for (vector < pair < nod*, char > > :: iterator it = R->urm.begin(); it != R->urm.end(); it ++)
    {
        if (it->second == p)
        {
            R = it->first;
            return;
        }

        if (it->second > p)
        {
            nod *new_nod = new nod;
            R->urm.insert (it, make_pair (new_nod, p));
            R = new_nod;
            return;
        }

    }
    nod *new_nod = new nod;
    R->urm.insert (R->urm.end(), make_pair (new_nod, p));
    R = new_nod;
}

int main()
{
freopen ("nums.in", "r", stdin);
freopen ("nums.out", "w", stdout);

scanf ("%d\n", &M);
for (int i=1; i<=M; i++)
{
    int tip;
    scanf ("%d ", &tip);

    if (tip == 0)
    {
        int x;
        scanf ("%d\n", &x);
        continue;
    }

    gets (actual + 1);
    sir[i] = actual + 1;

    int L = strlen (actual + 1);
    if (R[L] == 0) R[L] = new nod;

    nod *q = R[L];
    for (int j=1; j<=L; j++)
        move (actual[j] - '0', q);
    if (q->cod) ;
    else q->cod = i;
}

for (int i=1; i<=100000; i++)
    if (R[i] != 0)
        dfs (R[i], 0, i);

freopen ("nums.in", "r", stdin);
scanf ("%d\n", &M);
for (int i=1; i<=M; i++)
{
    int tip;
    scanf ("%d ", &tip);
    if (tip == 0)
    {
        int x, ras = 0;
        scanf ("%d\n", &x);
        for (int j=16; j>=0; j--)
            if (ras + (1<<j) <= M && aib[ras + (1<<j)] < x )
                x -= aib[ras + (1<<j)], ras |= 1<<j;
        printf ("%s\n", sir [ al_k_lea[ras + 1] ] . c_str());
        continue;
    }

    gets (actual + 1);
    if (pos[i])
        U (pos[i]);
}

return 0;
}