LRU management

LRU management

字典树用来查找值,实现map<string,int>操作

tips:tot必须从一开始QAQ

#include<bits/stdc++.h>
using namespace std;
#define maxn 5000005
int trie[maxn][10];
///字典树主要用来查找
int ed[maxn];
///记录字符串s对应的数据存在的下标
int dat[maxn];
int L[maxn],R[maxn];
///维护一下前驱后继关系
int A[maxn];
///记录一下字典树中指向该下标i的ed坐标,删除的时候要把ed[j]置为0
int tot=1,//tot必须从一开始==
siz,num,s,e;//s,e维护一下头尾节点,便于维护前驱后继
void erase(int t)//传入删除数据的坐标信息
{
    ///int p=1;
    //int t=find(s);
    R[L[t]]=R[t];
    L[R[t]]=L[t];
    ///字典树怎么处理?
    ///不处理QAQ???ed[p]???
    if(t==s)
    {
        s=R[t];
    }
    if(t==e)
    {
        e=L[t];
    }
    dat[t]=R[t]=L[t]=0;
    ed[A[t]]=0;
    ///把字典树中指向t的指针去掉
    siz--;
}
void ins(int t,int dt)///维护下数据,处理下节点关系
{
    dat[t]=dt;
    ///前驱后继维护?t-1?
    ///弄一个头指针,尾指针类似的下标维护
    if(s==0)s=e=t;///头节点为空
    else L[R[e]=t]=e;//尾节点移到t
    R[t]=0;
    e=t;
    siz++;
}
void insert(char *s1,int t)
{
    int n=strlen(s1);
    int p=1;//都从1开始
    for(int i=0; i<n; i++)
    {
        if(!trie[p][s1[i]-'0'])///都是判断trie[p][s1[i]-'0']
        {
            trie[p][s1[i]-'0']=++tot;
        }
        p=trie[p][s1[i]-'0'];
    }
    
    A[t]=p;
    ed[p]=t;
}
int search(char *s)
{
    int n=strlen(s);
    int p=1;
    for(int i=0; i<n; i++)
    {
        if(!trie[p][s[i]-'0'])return false;
        else p=trie[p][s[i]-'0'];
    }
    return ed[p];///维护在数组中信息

}
int T,n,m;
void init()
{
   
    /*while(s){
       erase(s);
   }*/
    //trie[1][0]=trie[1][1]=0;
     for(int i=1; i<=max(tot,num); i++)
    {
        A[i]=0;//dat[i]=0;
        dat[i]=L[i]=R[i]=ed[i]=0;
        for(int j=0; j<10; j++)trie[i][j]=0;
    }
    
    tot=1;siz=0;
    s=e=0;
    num=0;
   
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int op,d;
        char s1[15];
        while(n--)
        {
            scanf("%d%s%d",&op,s1,&d);
            if(op==0)
            {
                int x=search(s1);
               // cout<<x<<" "<<s1<<endl;
                if(x)
                {

                    cout<<dat[x]<<'\n';
                    d=dat[x];
                    erase(x);
                }
                else
                {
                    cout<<d<<'\n';
                }
                num++;
                ins(num,d);
                insert(s1,num);
                //int _x=search(s1);
                //cout<<"SEAR"<<s1<<' '<<_x<<" "<<dat[_x]<<endl;
                //cout<<"INS"<<num<<" "<<s1<<" "<<d<<endl;
                if(siz>m)
                {
                    //puts("MAN");
                   // cout<<"ERA"<<s<<endl;
                    erase(s);
                }
            }
            else
            {
                int x=search(s1);
                if(x==0)
                {
                    //puts("MY");
                    puts("Invalid");
                }
                else if(d==1&&R[x]==0)
                {
                    puts("Invalid");
                }
                else if(d==-1&&L[x]==0)
                {
                    puts("Invalid");
                }
                else
                {
                    if(d==0)cout<<dat[x]<<'\n';
                    else if(d==1)cout<<dat[R[x]]<<'\n';
                    else cout<<dat[L[x]]<<'\n';

                }


            }

        }
    }

}

猜你喜欢

转载自www.cnblogs.com/liulex/p/11248079.html
LRU