Cod sursa(job #3188984)

Utilizator TheodosiusOancea Teodor Stefan Theodosius Data 4 ianuarie 2024 11:48:08
Problema Secventa 5 Scor 40
Compilator c-64 Status done
Runda Arhiva de probleme Marime 3.65 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Pair{
    char *first;
    int second;
};
struct Pair *make_pair(char *a,int b){
    struct Pair *x=(struct Pair *)malloc(sizeof(struct Pair));
    x->first=strdup(a);
    x->second=b;
    return x;
};
int make_hash(char *s,int size){
    unsigned long long hash=5813;
    for(int i=0;s[i];i++) hash=hash*33+s[i];
    return (hash%size);
}
int compare(char *a,char *b){
    int i=0,x=0;
    for(;a[i]&&b[i];i++){
        if(b[i]>a[i]) return 0;
        if(b[i]==a[i])x++;
    }
    if(!a[i]&&!b[i]&&x==i)
    return -1;
    if(!a[i]&&b[i])
    return 0;
    return 1;
};
struct Nod{
    struct Pair *val;
    struct Nod *st,*dr;
};
struct Tree{
    struct Nod *top;
};
void addtotree(struct Tree *t,char *s,int val){
    if(!t->top){
        t->top=(struct Nod *)malloc(sizeof(struct Nod));
        t->top->val=make_pair(s,val);
        t->top->st=NULL;
        t->top->dr=NULL;
        return;
    };
    struct Nod *nod=t->top;
    while(nod){
        int x=compare(s,nod->val->first);
        if(x==1){
            if(nod->dr){
                nod=nod->dr;
            }
            else{
                nod->dr=(struct Nod *)malloc(sizeof(struct Nod));
                nod->dr->val=make_pair(s,val);
                nod->dr->dr=NULL;
                nod->dr->st=NULL;
                return;
            }
        }
        if(x==0){
            if(nod->st){
                nod=nod->st;
            }
            else{
                nod->st=(struct Nod *)malloc(sizeof(struct Nod));
                nod->st->val=make_pair(s,val);
                nod->st->dr=NULL;
                nod->st->st=NULL;
                return;
            }
        }
        if(x==-1){
            nod->val->second=val;
            return;
        }
    }
}
struct Nod *findtotree(struct Tree *t,char *s){
    struct Nod *nod=t->top;
    while(nod){
        int x=compare(s,nod->val->first);
        if(x==1)nod=nod->dr;
        if(x==0)nod=nod->st;
        if(x==-1){
            return nod;
        }
    }
    return NULL;
}
struct Map{
    struct Tree **t;
    int size;
};
struct Map *createMap(int size){
    if(size<=0) return NULL;
    struct Map *m=(struct Map *)malloc(sizeof(struct Map));
    m->size=size;
    m->t=(struct Tree **)calloc(size,sizeof(struct Tree *));
    for(int i=0;i<size;i++) m->t[i]=NULL;
    return m;
};
void add(struct Map *m,char *s,int val){
    int hash=make_hash(s,m->size);
    if(!m->t[hash]){
        m->t[hash]=(struct Tree *)malloc(sizeof(struct Tree));
        m->t[hash]->top=NULL;
    };
    addtotree(m->t[hash],s,val);
};
int find(struct Map *m,char *s){
    int hash=make_hash(s,m->size);
    if(!m->t[hash]) return 0;
    struct Nod *nod=findtotree(m->t[hash],s);
    if(nod) return nod->val->second;
    return 0;
};
int n;
int cnt(int l,char **sir){
    struct Map *m=createMap(1000);
    int dr=0,st=0;
    int cnt=0,rasp=0;
    while(dr<n){
        int x=find(m,sir[dr]);
        if(x==0) cnt++;
        add(m,sir[dr],x+1);
        while(cnt>l&&st<=dr){
            int y=find(m,sir[st]);
            if(y==1) cnt--;
            add(m,sir[st],y-1);
            st++;
        }
        if(st>dr) break;
        rasp+=(dr-st);
        dr++;
    }
    return rasp;
}
int main()
{
    freopen("secv5.in","r",stdin);
    freopen("secv5.out","w",stdout);
    int m,k;
    scanf("%d %d %d",&n,&m,&k);
    char **sir=(char **)calloc(n,sizeof(char *));
    for(int i=0;i<n;i++) sir[i]=(char *)malloc(sizeof(char)*20),scanf("%s",sir[i]);
    printf("%d",cnt(k,sir)-cnt(m-1,sir));
    return 0;
}